2012-08-16 16 views
10

aspect = 1で自由にスケーリングできますが、各パネルのx/yの範囲は同じです。以下の例では、bのx倍率は(-0.04,0.04)である必要があります。自由なスケールですが、x/yのパネル単位で同じ範囲

編集:追加の格子バージョン

library(ggplot2) 
d = data.frame(x=rnorm(100),group=c("A","B")) 
d$y = d$x+rnorm(100,0,0.5) 
d[d$group=="B","x"]=d[d$group=="B","x"]/100 
d[d$group=="B","y"]=d[d$group=="B","y"]/60 
qplot(x,y,data=d,asp=1) + facet_wrap(~group,scale="free") 

require(lattice) 
xyplot(y~x|group, data=d,aspect=1,scales=list(relation="free"), 
    prepanel=function(x,y){ 
    lims = c(min(x,y), max(x,y)) 
    list(xlim=lims,ylim=lims) 
    }) 

in each panel, the x and y range should be the same

+1

+1良い質問。私は周りを見回しましたが、あなたが要求したとおりに動作させることができませんでした。 – Andrie

+4

@アントリー:羊毛麻痺者のように、私はまずそれが私の党派の無知だと思った。あなたのコメントの後、私はスレッド[https://groups.google.com/forum/?fromgroups#!topic/ggplot2/cDzL_yHew0I[1-25]]を見つけました。これはggplot2で簡単ではないことを示しています。 –

+0

私はその否定的なものを逃したように見える:http://stackoverflow.com/questions/7905828/multi-panel-plots-in-ggplot2-scales-that-are-free-and-equal?rq=1 –

答えて

6

ggplot2の最新バージョンが内部gtableを使用すると、あなたは非常に簡単にタスクのこの種の操作を行うことができます

d = data.frame(x=rnorm(100),group=c("A","B")) 
d$y = d$x+rnorm(100,0,0.5) 
d[d$group=="B","x"]=d[d$group=="B","x"]/100 
d[d$group=="B","y"]=d[d$group=="B","y"]/60 

# create plots for each level of group 
p <- lapply(levels(d$group), 
    function(i) { 
    dat <- subset(d, group == i) 
    lim <- range(c(dat$x, dat$y)) 
    ggplot_gtable(ggplot_build(qplot(x,y,data=dat,asp=1) + 
     facet_wrap(~group,scale="free") + 
     coord_equal() + 
     xlim(lim) + ylim(lim))) 
    }) 

# tweaking margins 
p[[1]] <- p[[1]][, -6] 
p[[2]] <- p[[2]][, -(1:2)] 

# draw it 
grid.newpage() 
grid.draw(cbind(p[[1]], p[[2]], size = "first")) 

enter image description here

+0

ありがとう、@ kohske、しかし、プレパネルを使用している格子のソリューションは私にとってはるかに合理化されているようです。 –

+0

+1、私はDieterに同意する必要がありますが、ggplotの強みは通常、低レベルの詳細が必要ないという事実です。 –

関連する問題