查看原文
其他

R绘图模板——箱线图+热图组合!

王志山 科研后花园 2023-09-08
 

点击上方

“科研后花园”

关注我们


代码如下:

1、加载所需R包:

#加载包library(ggplot2) # Create Elegant Data Visualisations Using the Grammar of Graphicslibrary(ggpubr) # 'ggplot2' Based Publication Ready Plotslibrary(ggsignif) # Significance Brackets for 'ggplot2'library(reshape2) # Flexibly Reshape Data: A Reboot of the Reshape Package

2、数据:

df1 <- PlantGrowthdf2 <- data.frame(sample=c("A","B","C","D","E","F"), a=c(5,4,5,1,2.5,3), b=c(2,1,2.5,1.5,1.7,2.5), c=c(7,5,4,3,6,8))df2 <- melt(df2,id.vars=c("sample"))

3、绘图:

p1 <- ggplot(df1,aes(x=group,y=weight))+ stat_boxplot(aes(color=group),geom = "errorbar", width=0.1,size=0.8)+#添加误差线,注意位置,放到最后则这条先不会被箱体覆盖 geom_boxplot(aes(color=group), size = 0.8, outlier.color="white")+#异常点去除 theme_bw()+ theme(panel.grid = element_blank(), #背景 axis.line=element_line(),#坐标轴的线设为显示 legend.position="none",#图例位置 axis.text=element_text(color='#003b64',size=12), legend.text = element_text(color='#003b64',size=12), axis.title= element_text(size=12), axis.text.x=element_text(angle = 45,vjust = 1,hjust = 1))+ scale_color_manual(values=c("#ffc000","#80c97f","#a68dc8"))+#指定颜色 geom_jitter(width = 0.2, color = "#ff4c4c", size = 3, alpha= 0.7)+#添加抖动点 geom_signif(comparisons = list(c("ctrl","trt1"), c("ctrl","trt2"), c("trt1","trt2")), map_signif_level = T, #显示星号 test = t.test, y_position = c(7,8,7.5), size=0.8)+ labs(x=NULL,y=NULL)p1
p2 <- ggplot(df2,aes(x=sample,y=variable))+ geom_tile(aes(fill=value))+ theme_minimal()+ theme(panel.grid = element_blank(), axis.text=element_text(color='#003b64',size=12), legend.text = element_text(color='#003b64',size=12), axis.title= element_text(size=12))+ labs(x=NULL,y=NULL)+ scale_fill_gradient(name="Value", low = "white",high = "red")p2
#拼图cowplot::plot_grid(p1,p2,ncol = 2)


温馨提示

如果你喜欢本文,请分享到朋友圈,想要获得更多信息,请关注我。






您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存