r - Panel plot with multiple x variables -


i'd make plot 1 produced below (in either lattice or ggplot2) each panel has different x variable. below solution uses reshape solution don't because require duplicating other columns used other aesthetic mapping (in example, f used color). there way tell lattice (or ggplot2) plot different x variable in each panel doesn't require unnecessary, memory-inefficient, duplication of f variable (as created when reshaping 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) 

see how data in f gets duplicated result of 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 

using gridextra package might solution. following code:

# required libraries require(ggplot2) require(gridextra)  # creating different plots & 1 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 extract 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 plots 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) 

you following result: enter image description here

is you're looking for?


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -