查看原文
其他

R绘图模板——柱状图+散点图+折线图+显著性!

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

点击上方

“科研后花园”

关注我们


代码如下:

1、加载R包:

library(ggplot2) # Create Elegant Data Visualisations Using the Grammar of Graphicslibrary(reshape2) # Flexibly Reshape Data: A Reboot of the Reshape Packagelibrary(tidyr) # Tidy Messy Datalibrary(dplyr) # A Grammar of Data Manipulationlibrary(ggsignif) # Significance Brackets for 'ggplot2'

2、加载绘图数据并进行数据处理:

#数据——ggplot自带的ToothGrowth数据df <- ToothGrowthdf$dose <- as.factor(df$dose)data <- df#计算均值及标准差df1 <- data%>% group_by(dose)%>% summarise(mean= mean(len), sd= sd(len))

3、绘图:

ggplot()+ geom_bar(df1,mapping=aes(x=dose,y=mean), fill = "white", size = 1.5,color = c("#d20962","#f47721","#7ac143"),position="dodge", stat="identity",width = 0.6)+ geom_errorbar(df1,mapping=aes(x = dose,ymin = mean-sd, ymax = mean+sd), width = 0.3,color = c("#d20962","#f47721","#7ac143"), size=1.5)+ geom_jitter(df, mapping=aes(x=dose,y=len,fill = dose,color = dose,shape = dose), size = 2.5,width = 0.2,alpha=0.9)+ geom_line(df1,mapping=aes(x=dose,y=mean,group=1), size=1,color="#00aee6")+ geom_point(df1,mapping=aes(x=dose,y=mean),color="black",size=3,shape=8)+ scale_color_manual(values = c("#d20962","#f47721","#7ac143"))+ geom_signif(df,mapping=aes(x=dose,y=len), comparisons = list(c("0.5", "1"), c("1","2"), c("0.5","2")), map_signif_level=T, tip_length=c(0,0,0,0,0,0), y_position = c(35,40,45), size=1, textsize = 7, test = "t.test")+ scale_y_continuous(expand = c(0, 0), limit = c(0, 50))+ theme_classic(base_line_size = 1)+ theme(panel.grid=element_blank(), axis.text=element_text(color='black',size=13,face = "bold"), axis.title.y = element_text(color='black',size=15,face = "bold"), legend.text = element_text(color='black',size=13,face = "bold"), legend.title = element_blank(), legend.position = "none")+ labs(x=NULL,y="Value")

温馨提示

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






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

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