一、一面多图的实现方法:
- 输入:
p1 <- ggplot(rd,aes(x=x))+geom_histogram(binwidth=.3,colour="black", fill="blue")+
theme(axis.title.y = element_text(size=12,face="bold"),axis.title.x =
element_text(size=12,face="bold"))+labs(y="Frequency")+theme_bw()
p2 <- ggplot(rd,aes(x=y))+geom_histogram(colour="black", fill="red",
breaks=seq(0,50,by=0.5))+ theme(axis.title.y =
element_text(size=12,face="bold"),axis.title.x =
element_text(size=12,face="bold"))+
scale_x_continuous(limits=c(0,40))+labs(y="Frequency")+theme_bw()
- 输入:
library(gridExtra)
grid.arrange(p1,p2,nrow=1)
- 结果:
- 输入:
library(easyGgplot2)
ggplot2.multiplot(p1,p2, cols=1)
- 结果:
- 输入:
library(Rmisc)
multiplot(p1,p2,cols=2)
- 结果:
- 输入:
install.packages("cowplot")
library(cowplot)
p3 <- ggplot(rd,aes(x=y))+geom_histogram(colour="black", fill="red",
breaks=seq(0,50,by=0.5))+theme(axis.title.y =
element_text(size=12,face="bold"),axis.title.x =
element_text(size=12,face="bold"))+
scale_x_continuous(limits=c(0,40))+labs(y="Frequency")
p3
- 结果:
- 输入:
plot_grid(p1, NULL, NULL,p3, labels = c("A", "", "", "B"),ncol = 2)
- 结果:
二、子图嵌入主图的实现方法:
- 输入:
library(grid)
vp <- viewport(width = 0.3, height = 0.4, x = 0.65,y = 0.9,just=c("left","top"))
print(p1)
print(p2,vp=vp)
- 结果:
- 输入:
install.packages("viridis")
library(viridis)
ggdraw() +draw_plot(p1,0,0,1,1)+
draw_plot(p3,0.1,0.5,0.3,0.4)
- 结果:
若需要此书者,请转发文章后后台留言