美文网首页
R绘图合并

R绘图合并

作者: 茶苯海 | 来源:发表于2018-06-17 11:15 被阅读20次

gridExtra包的grid.arrange函数

library(Hmisc)
library(ggplot2)
library(gridExtra)
set.seed(7)
x <- sort(rnorm(1000,10,100))[26:975]
y <- x * 500 + rnorm(950,5000,20000)
df <- data.frame(x = x,y = y,cuts = factor(cut2(x,g=5)),resid = resid(lm(y ~ x)))

scatterP1 <- ggplot(df,aes(x = x,y = y)) + 
  geom_point(aes(colour = cuts,fill = cuts),shape = 1,show.legend = FALSE) + 
  geom_smooth(method = lm,level = 0.99)

plot_left <- ggplot(df,aes(x = y,fill = cuts)) + 
  geom_density(alpha = .5,show.legend = FALSE) + coord_flip() + scale_y_reverse()

plot_right <- ggplot(df,aes(x = resid,fill = cuts)) + geom_density(alpha = .5,show.legend = FALSE) + coord_flip()

grid.arrange(plot_left,scatterP1,plot_right,ncol = 3,nrow = 1,widths = c(1,2,1))
Rplotmul.png

相关文章

网友评论

      本文标题:R绘图合并

      本文链接:https://www.haomeiwen.com/subject/cwfweftx.html