千家信息网

如何利用ggplot2绘制箱线图并进行分面处理

发表于:2025-01-31 作者:千家信息网编辑
千家信息网最后更新 2025年01月31日,这篇文章主要为大家展示了"如何利用ggplot2绘制箱线图并进行分面处理",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"如何利用ggplot2绘制箱线图并进
千家信息网最后更新 2025年01月31日如何利用ggplot2绘制箱线图并进行分面处理

这篇文章主要为大家展示了"如何利用ggplot2绘制箱线图并进行分面处理",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"如何利用ggplot2绘制箱线图并进行分面处理"这篇文章吧。

利用ggplot2绘制箱线图,并进行分面处理。

加载相应的R包与数据设置

library('ggplot2')########################画图library('reshape2')########################meltlibrary('grid')  ########################分面A =rep( c("A","B","C","D"),each=2)B = c(6.332968,9.368328,6.674348,4.127901,5.192845,6.652865,7.829350,6.995062)C = c(5.367671,7.286253,5.217053,3.875520,6.679444,6.127819,5.091166,7.942029)D = c(5.171107,6.232718,5.320568,4.924498,7.140883,4.228142,5.793514,6.347785)E = c(5.533754,6.152393,6.113618,4.960935,5.959568,5.078903,4.871103,5.223206)F = rep(c("sample1","sample2"),len=4)dat = data.frame(A,B,C,D,E)names(dat)[1] = c("type")names(dat)[2:5]=Fdat = melt(dat,variable.name="Sample",value.name = "Num")head(dat)

绘制基本的箱线图

##################按照类型进行统计P_box=ggplot(data=dat,aes(x=type,y=Num)) +  geom_boxplot(fill="grey",width=0.4,notch=F)+  ##########填充颜色fill, 宽,中位线缺刻notch=T  labs(x="type",y="Num")+  theme(plot.title = element_text(size = 25,face = "bold", vjust = 0.5, hjust = 0.5),        legend.title = element_blank(),        legend.text = element_text(size = 18, face = "bold"),        legend.position = 'right',        legend.key.size=unit(0.8,'cm'),        axis.ticks.x=element_blank(),###########取消x轴刻度线        axis.text.x=element_text(size = 10,face = "bold", vjust = 0.5, hjust = 0.5),############刻度标签文字大小等设置        axis.text.y=element_text(size = 10,face = "bold", vjust = 0.5, hjust = 0.5),        axis.title.x = element_text(size = 20,face = "bold", vjust = 0.5, hjust = 0.5),        axis.title.y = element_text(size = 20,face = "bold", vjust = 0.5, hjust = 0.5),######取消默认的背景颜色方框等        panel.background = element_rect(fill = "transparent",colour = "black"),         panel.grid.minor = element_blank(),         panel.grid.major = element_blank(),        plot.background = element_rect(fill = "transparent",colour = "black"))print(P_box)

利用样品进行分面

# 基于样品进行分面P_box=ggplot(data=dat,aes(x=type,y=Num)) +  geom_boxplot(fill="grey",width=0.4,notch=F)+  labs(x="type",y="Num")+  theme(plot.title = element_text(size = 25,face = "bold", vjust = 0.5, hjust = 0.5),        legend.title = element_blank(),        legend.text = element_text(size = 18, face = "bold"),        legend.position = 'right',        legend.key.size=unit(0.8,'cm'),        axis.ticks.x=element_blank(),        axis.text.x=element_text(size = 10,face = "bold", vjust = 0.5, hjust = 0.5),        axis.text.y=element_text(size = 10,face = "bold", vjust = 0.5, hjust = 0.5),        axis.title.x = element_text(size = 20,face = "bold", vjust = 0.5, hjust = 0.5),        axis.title.y = element_text(size = 20,face = "bold", vjust = 0.5, hjust = 0.5),        panel.background = element_rect(fill = "transparent",colour = "black"),         panel.grid.minor = element_blank(),         panel.grid.major = element_blank(),        plot.background = element_rect(fill = "transparent",colour = "black"))+  facet_grid(.~Sample)   #########分面print(P_box)

以上是"如何利用ggplot2绘制箱线图并进行分面处理"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

0