具有多个x变量的面板图

时间:2014-01-31 17:36:22

标签: r ggplot2 lattice

我想制作一个类似下面生成的图(格子或ggplot2),其中每个面板都有一个不同的x变量。下面的解决方案使用reshape作为我不喜欢的解决方案,因为它需要复制用于其他美学映射的其他列(在此示例中,f用作颜色)。有没有办法告诉格子(或ggplot2)在每个面板中绘制一个不同的x变量,它不需要f变量的不必要的,内存效率低的重复(在重塑d时创建)?

require(ggplot2)
require(lattice)
require(reshape2)

n <- 100
d <- data.frame(y=rnorm(n), f=as.factor(rbinom(n, size=1, prob=.3)), replicate(5, rnorm(n)))

d.m <- melt(d, id.var=c("y","f"))

xyplot(y~value| variable, group=f, data=d.m)
ggplot(data=d.m, aes(x=value, y=y, colour=f)) + geom_point() + facet_wrap(~variable)

了解f

melt中的数据是如何重复的
           y f variable       value
1   0.5812506 0       X1 -0.08260787
2  -1.1241157 0       X1 -0.24778013
3   1.2121779 0       X1 -0.75744705
4  -0.4249275 1       X1 -1.23016030
5   0.1224656 0       X1  0.72092066
6  -0.3897643 1       X1 -0.74259349
7  -1.2860360 1       X1 -1.32834543
8   0.8537305 0       X1  0.64800540
9  -1.0921726 1       X1 -0.89841339
10  1.2463329 0       X1  1.25437668
> d.m[n+(1:10),]
             y f variable      value
101  0.5812506 0       X2  0.7030727
102 -1.1241157 0       X2 -0.5555425
103  1.2121779 0       X2 -0.6258554
104 -0.4249275 1       X2  0.6733429
105  0.1224656 0       X2 -0.1000167
106 -0.3897643 1       X2  0.4812312
107 -1.2860360 1       X2 -0.9201268
108  0.8537305 0       X2  0.2198029
109 -1.0921726 1       X2  0.4488353
110  1.2463329 0       X2 -0.2140737
> d.m[2*n+(1:10),]
             y f variable       value
201  0.5812506 0       X3  1.74131617
202 -1.1241157 0       X3  1.70265539
203  1.2121779 0       X3  0.68513441
204 -0.4249275 1       X3 -0.04074366
205  0.1224656 0       X3  1.16006226
206 -0.3897643 1       X3 -1.65739811
207 -1.2860360 1       X3  0.19553116
208  0.8537305 0       X3 -0.80369612
209 -1.0921726 1       X3  0.39121351
210  1.2463329 0       X3 -1.01627690

1 个答案:

答案 0 :(得分:1)

使用gridExtra包可能是一种解决方案。使用以下代码:

# required libraries
require(ggplot2)
require(gridExtra)

# creating the different plots & one empty plot
p1 <- ggplot(data=d, aes(x=X1, y=y, colour=f)) + 
  geom_point() + ggtitle("X1") + xlim(-4,4) +
  theme_bw() +
  theme(
    axis.title.x = element_blank(),
    axis.text.x = element_blank(),
    axis.ticks.x = element_blank()
  )

p2 <- ggplot(data=d, aes(x=X2, y=y, colour=f)) + 
  geom_point() + ggtitle("X2") + xlim(-4,4) +
  theme_bw() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    axis.title.x = element_blank(),
    axis.text.x = element_blank(),
    axis.ticks.x = element_blank()
  )

p3 <- ggplot(data=d, aes(x=X3, y=y, colour=f)) + 
  geom_point() + ggtitle("X3") + xlim(-4,4) +
  theme_bw() +
  theme(
    axis.title.x = element_blank(),
    axis.text.x = element_blank(),
    axis.ticks.x = element_blank()
  )

p4 <- ggplot(data=d, aes(x=X4, y=y, colour=f)) + 
  geom_point() + ggtitle("X4") + xlim(-4,4) +
  theme_bw() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    axis.title.x = element_blank(),
    axis.text.x = element_blank(),
    axis.ticks.x = element_blank()
  )

p5 <- ggplot(data=d, aes(x=X5, y=y, colour=f)) + 
  geom_point() + ggtitle("X5") + xlim(-4,4) +
  theme_bw() +
  theme(
    axis.title.x = element_blank()
  )

p6 <- ggplot(data=d, aes(x=X5, y=y)) + 
  geom_point(size=0) + ggtitle("") + xlim(-4,4) +
  theme_bw() +
  theme(
    panel.border = element_blank(),
    panel.grid = element_blank(),
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    axis.title.x = element_blank()
  )

# function to extract the legend (borrowed from: https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs )
g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}

legend <- g_legend(p1)
lwidth <- sum(legend$width)

# combining the plots with gridExtra
grid.arrange(arrangeGrob(p1 + theme(legend.position="none"),
                         p2 + theme(legend.position="none"),
                         p3 + theme(legend.position="none"),
                         p4 + theme(legend.position="none"),
                         p5 + theme(legend.position="none"),
                         p6 + theme(legend.position="none"), ncol=2
                         ), legend, widths=unit.c(unit(1, "npc") - lwidth, lwidth), nrow=1)

您将获得以下结果: enter image description here

这是你要找的吗?