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))