bar_errorbar_point
今天我们复现一幅2022年8月发表在Cell
上的一副柱状图
,配色
挺好看的。
❝「Title:」Targeted suppression of human IBD-associated gut microbiota commensals by phage consortia for treatment of intestinal inflammation
「DOI:」10.1016/j.cell.2022.07.003
❞
本期图片
figure
❝图为不同分组之间微生物Shannon指数差异。
❞
结果展示
result1
示例数据和代码领取
点赞
、在看
本文,分享至朋友圈集赞20个
并保留30分钟
,截图发至微信mzbj0002
领取。
「木舟笔记2022年度VIP可免费领取」。
木舟笔记2022年度VIP企划
「权益:」
- 「2022」年度木舟笔记所有推文示例数据及代码(「在VIP群里实时更新」)。
data+code - 木舟笔记「科研交流群」。
- 「半价」购买
跟着Cell学作图系列合集
(免费教程+代码领取)|跟着Cell学作图系列合集。
「收费:」
「99¥/人」。可添加微信:mzbj0002
转账,或直接在文末打赏。
绘制
# Load package library(ggplot2) library(tidyverse) library(plyr) library(ggbeeswarm) library(ggsignif) # create data data <- data.frame( Ctrl = abs(rnorm(100,4.5,0.3)), CD = abs(rnorm(100,4,0.3)), UC =abs(rnorm(100,4.2,0.5)) ) head(data) # wide to long dt_long <- gather(data,group,value) head(dt_long) str(dt_long) dt_long$group <- factor(dt_long$group,levels = c('Ctrl','CD','UC')) #+++++++++++++++++++++++++ # Function to calculate the mean and the standard deviation for each group #+++++++++++++++++++++++++ # data : a data frame # varname : the name of a column containing the variable to be summariezed # groupnames : vector of column names to be used as grouping variables data_summary <- function(data, varname, groupnames){ summary_func <- function(x, col){ c(mean = mean(x[[col]], na.rm=TRUE), sd = sd(x[[col]], na.rm=TRUE)) } data_sum<-ddply(data, groupnames, .fun=summary_func, varname) data_sum <- rename(data_sum, c("mean" = varname)) return(data_sum) } dt_sum <- data_summary(dt_long, varname="value",groupnames="group") head(dt_sum) # draw ggplot() + geom_bar(dt_sum,mapping = aes(x=group, y=value, fill=group), stat="identity",width=.6,alpha = 0.5,position=position_dodge()) + geom_errorbar(dt_sum,mapping = aes(x=group, y=value,ymin=value-sd, ymax=value+sd), width=.4,position=position_dodge(.8)) + geom_beeswarm(dt_long,mapping = aes(x=group, y=value, fill=group), shape = 21,color = 'black',size = 3.5,cex = 1.2,stroke = 0.6)+ geom_signif(dt_long,mapping = aes(x=group, y=value), comparisons = list(c("Ctrl","CD"),c("Ctrl","UC")), test = "t.test", step_increase = 0.2, tip_length = 0, textsize = 6, size = 1, map_signif_level = T)+ scale_fill_manual(values=c('#73bbaf','#d15e67','#6c43a6'))+ scale_y_continuous(expand = c(0,0),limits = c(0,6.5))+ labs(x = "", y = "Shannon index")+ theme_classic()+ theme(axis.line.x=element_line(color="black",size=0.8), axis.line.y=element_line(color="black",size=0.8), axis.ticks.x=element_line(color="black",size=0.8), axis.ticks.y=element_line(color="black",size=0.8), axis.text.x = element_text(color="black",size=14), axis.title.y=element_text(color="black",size=14)) ggsave('bar_errorbar_point.pdf',width = 5,height = 6)
result1