Подтвердить что ты не робот

Свободный масштаб, но тот же диапазон для каждой панели в x/y

Я пытаюсь получить бесплатное масштабирование с помощью aspect = 1, но тот же диапазон в x/y на каждой панели. В приведенном ниже примере это означает, что x-масштабирование в b должно быть (-0,04,0,04).

Отредактировано: добавлена ​​версия решетки

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

4b9b3361

Ответ 1

Поскольку последняя версия 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