与ggplot2并排绘图 [英] Side-by-side plots with ggplot2

查看:151
本文介绍了与ggplot2并排绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 ggplot2软件包并排放置两个图,即做相当于 par(mfrow = c(1,2))



例如,我想拥有以下

  x < -  rnorm(100)
eps < - rnorm(100,0,2)
qplot(x,3 * x + eps)
qplot(x,2 * x + eps)


我是否需要将它们放在同一个data.frame中?

  qplot(displ,hwy,data = mpg,facets =。〜year)+ geom_smooth()




函数<$ c 解决方案

中的$ c> grid.arrange() gridExtra 包会组合多个图;这是你如何把两个并排。
$ b $ pre $ require(gridExtra)
plot1 < - qplot(1)
plot2 < - qplot( 1)
grid.arrange(plot1,plot2,ncol = 2)

这很有用当两个绘图不是基于相同的数据时,例如,如果您想绘制不使用reshape()的不同变量。

这会将输出绘制为副作用。要将副作用打印到文件,请指定设备驱动程序(例如 pdf png 等),例如

  pdf(foo.pdf)
grid.arrange(plot1,plot2)
dev。 off()

或使用 arrangeGrob()结合 ggsave()

  ggsave(foo。 pdf,arrangeGrob(plot1,plot2))

这相当于使用 par(mfrow = c(1,2))。这不仅节省了安排数据的时间,而且当您需要两个不相似的地块时也是必要的。




附录:Using Facets < h3>

分面有助于为不同组制作类似的图表。下面的许多答案都指出了这一点,但我想用相当于上述图表的例子来强调这种方法。

  mydata < -  data.frame(myGroup = c('a','b'),myX = c(1 ,1))

qplot(data = mydata,
x = myX,
facets =〜myGroup)

ggplot(data = mydata)+
geom_bar(aes(myX))+
facet_wrap(〜myGroup)






更新



plot_grid 函数在 cowplot 值得一试替代 grid.arrange 。请参阅下面的@ claus-wilke和答案 / 199217>这个小插曲的等效方法;但该功能允许根据这个小插曲对绘图位置和尺寸进行更精细的控制。

I would like to place two plots side by side using the ggplot2 package, i.e. do the equivalent of par(mfrow=c(1,2)).

For example, I would like to have the following two plots show side-by-side with the same scale.

x <- rnorm(100)
eps <- rnorm(100,0,.2)
qplot(x,3*x+eps)
qplot(x,2*x+eps)

Do I need to put them in the same data.frame?

qplot(displ, hwy, data=mpg, facets = . ~ year) + geom_smooth()

解决方案

Any ggplots side-by-side (or n plots on a grid)

The function grid.arrange() in the gridExtra package will combine multiple plots; this is how you put two side by side.

require(gridExtra)
plot1 <- qplot(1)
plot2 <- qplot(1)
grid.arrange(plot1, plot2, ncol=2)

This is useful when the two plots are not based on the same data, for example if you want to plot different variables without using reshape().

This will plot the output as a side effect. To print the side effect to a file, specify a device driver (such as pdf, png, etc), e.g.

pdf("foo.pdf")
grid.arrange(plot1, plot2)
dev.off()

or, use arrangeGrob() in combination with ggsave(),

ggsave("foo.pdf", arrangeGrob(plot1, plot2))

This is the equivalent of making two distinct plots using par(mfrow = c(1,2)). This not only saves time arranging data, it is necessary when you want two dissimilar plots.


Appendix: Using Facets

Facets are helpful for making similar plots for different groups. This is pointed out below in many answers below, but I want to highlight this approach with examples equivalent to the above plots.

mydata <- data.frame(myGroup = c('a', 'b'), myX = c(1,1))

qplot(data = mydata, 
    x = myX, 
    facets = ~myGroup)

ggplot(data = mydata) + 
    geom_bar(aes(myX)) + 
    facet_wrap(~myGroup)


Update

the plot_grid function in the cowplot is worth checking out as an alternative to grid.arrange. See the answer by @claus-wilke below and this vignette for an equivalent approach; but the function allows finer controls on plot location and size, based on this vignette.

这篇关于与ggplot2并排绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆