R多个绘图与ggplot

时间:2014-11-06 17:02:22

标签: r layout plot ggplot2 figure

我需要生成一个类似于this的数字:

enter image description here

基本上我使用ggplot分别生成了3个单独的图,我想在一个图中将它们组合起来。我想要的安排与上图完全相同。我已经尝试过layout(),但我不认为它适用于ggplot,或者我做错了什么,

另外,我想在每个情节的左上角放置一个标签a,b,c。

提前致谢

1 个答案:

答案 0 :(得分:1)

在@ Henrik的链接中有一些很好的答案,但这是使用用户定义的函数multiplot的另一个解决方案,可以在this webpage找到:

library(ggplot2)
##
p1 <- ggplot(
  data=mtcars,
  aes(x=wt))+
  geom_histogram(binwidth=.5)+
  ggtitle("(A) Histogram of wt")+
  ylab("Frequency")
##
p2 <- ggplot(
  data=mtcars,
  aes(x=mpg))+
  geom_histogram(binwidth=5)+
  ggtitle("(B) Histogram of mpg")+
  ylab("Frequency")
##
p3 <- ggplot(
  data=mtcars,
  aes(x=disp))+
  geom_histogram(binwidth=50)+
  ggtitle("(C) Histogram of disp")+
  ylab("Frequency")
##
> multiplot(p1,p2,p3,layout=matrix(c(1,2,1,3),nrow=2))

enter image description here

相关问题