R 语言图形中插入子图

rm(list=ls())
library(dplyr)
library(ggplot2)
library(openxlsx)
library(grid)
library(viridis)
library(cowplot)
library(ggpubr)
library(showtext)
showtext.auto(enable = TRUE)
font.add('SimSun', 'simsun.ttc')
# 前期的数据处理,这里注意糖的质量应该只保存第一位有效数字,后面的可以直接忽略

p1 <- ggplot(data1, aes(x = testdata1)) + geom_histogram(binwidth = 100, fill = "lightblue", colour = "black")+
  ggtitle(label ="test1")+
  theme(plot.title = element_text(lineheight=.8, size=10, face="bold",family = 'SimSun',hjust = 0.5)) +
  theme(legend.title=element_text(face="bold",size=8)) + 
  theme(legend.text=element_text(face="italic",size=9))+
  xlab("Mass") + ylab("count") +
  theme(axis.title.x =element_text(face="italic",size=10), axis.title.y=element_text(face="italic",size=10))+
  theme(axis.text.x = element_text(face="bold", size=8))+
  theme(axis.text.y = element_text(face="bold", size=8))

p2 <- ggplot(data2, aes(x = testdata2)) + geom_histogram(binwidth = 10, fill = "lightblue", colour = "black")+
  theme(plot.title = element_text(lineheight=.8, size=20, face="bold",family = 'SimSun',hjust = 0.5)) +
  theme(legend.title=element_text(face="bold",size=8)) + 
  theme(legend.text=element_text(face="italic",size=9))+
  xlab("Mass") + ylab("count") +
  theme(axis.title.x =element_text(face="italic",size=8), axis.title.y=element_text(face="italic",size=8))+
  theme(axis.text.x = element_text(face="bold", size=8))+
  theme(axis.text.y = element_text(face="bold", size=8))

p3 <- ggplot(data3, aes(x = testdata2[1:670])) + geom_histogram(binwidth = 2, fill = "lightblue", colour = "black")+
  ggtitle(label ="test2")+
  theme(plot.title = element_text(lineheight=.8, size=10, face="bold",hjust = 0.5)) +
  theme(legend.title=element_text(face="bold",size=8)) + 
  theme(legend.text=element_text(face="italic",size=9))+
  xlab("Mass") + ylab("count") +
  theme(axis.title.x =element_text(face="italic",size=10), axis.title.y=element_text(face="italic",size=10))+
  theme(axis.text.x = element_text(face="bold", size=8))+
  theme(axis.text.y = element_text(face="bold", size=8))

p4 <- ggdraw(p3) + draw_plot(p2,0.3,0.25,0.5,0.7)
ggarrange(p1,p4,ncol=1,nrow=2,labels=c("A","B"))


  

  

原文地址:https://www.cnblogs.com/lmj-sky/p/11111177.html