绘制子母图
母图绘制
library(ggplot2)
library(grid)
p1 <- ggplot(iris, aes(Sepal.Length,Sepal.Width, fill = Species))+
geom_point(size = 4, shape = 21, color = 'black')+
theme_light()+
xlim(4,10) #留出一些空间,给子图使用
p1
子图绘制
p2 <- ggplot(iris, aes(Sepal.Length,Sepal.Width, fill = Species))+
geom_boxplot(color = 'black')+
theme_light()+
ggtitle("Submian:Box plot")
p2
viewport()函数,设置子图中心位置以及长宽
subvp <- viewport(x = 0.78,y = 0.38, width = 0.4, height = 0.5)
p1
print(p2, vp = subvp)
注:后续优化,legend参数。