R语言ggplot2画Graphpad风格柱状图
2024-11-07
结果图预览
第一张柱状图 含Bar ,显著性标记
数据含有均值,方差和重复数,用于显著性判断和标记
代码如下:
library(ggplot2)
library(ggsignif)
# Graphpad风格 绘图 柱状图
# 去掉右上边框,坐标轴贴紧,上部多留10%空间,或者根据Y轴最大值调整
# 刻度线,轴文本,轴标题,字体改为Arial 或者 serif
# 轴标题 位移合适
# 去掉填充,柱子border加粗为2, 显示有缺口,保存为jpg有渐变,保存为PDF正常
# 加Errorbar 颜色,上下,宽度稍大,加粗
# 标记两组的显著性差异,width=0.35, size=1,y_positon=9
df <- read.csv("IL-1.csv",sep = ",")
df$sample <- factor(df$sample,levels = c("Mock","placebo","M15","R40","R40_M15"))
df$SD
df
ggplot(df,aes(x=sample,y=mean))+
geom_col(aes(color=sample),alpha=0,width = 0.6,size=1)+
scale_y_continuous(expand = c(0,0),limits = c(0,10))+
theme_classic()+
theme(axis.ticks.length = unit(.25, "cm"),axis.line = element_line(linewidth = 0.8),
axis.ticks = element_line(linewidth = 0.8,),
axis.text = element_text(size = 16,color = "black"),
# axis.title = element_text(size = 25),
axis.title.x = element_text(vjust = 0,size=20),
axis.title.y = element_text(vjust = 0,size=20),
axis.text.x = element_text(angle = 45,vjust = 1,hjust = 1),
plot.margin = margin(1,1,1,1, unit = "cm"),text = element_text(family="Arial"))+
geom_point()+
geom_errorbar(aes(ymax=mean+SD,ymin=mean-SD),width = 0.35,size=1)+
geom_signif(data=df,stat="signif",position="identity",
comparisons=list(c("Mock","R40_M15")),map_signif_level = TRUE,annotations="***")+
geom_signif(data=df,stat="signif",position="identity",
comparisons=list(c("placebo","R40_M15")),map_signif_level = TRUE,annotations="***",y_position = 9)+
labs(
x="Group", # x轴名称
y="Relative mRNA fold change", # y轴名称
title="CRISPR ISG Top20" # 标题名称
)+
geom_vline(xintercept = 0,linewidth=1)
"IL-1.csv"内容如下:
sample,mean,SD,N
placebo,5.82,0.470749302,3
R40,7.13,1.188359736,3
M15,4.09,0.347895726,3
R40_M15,2.32,0.386775064,3
Mock,1,0,3
第二张柱状图 含Bar,显著性标记,数据点
数据含有三个重复的原始数据,代码会计算均值和方差,用于显著性判断和标记
代码如下:
df1 <- read.csv("ccl2.csv",
header = TRUE)
df1
# df1$sample <- factor(df$sample,levels = c("placebo","R40","M15","R40_M15","Mock"))
ldf <- split(df1,f = df1$sample)
ldf
for (i in 1:length(ldf)) {
ldf[[i]]$mean = rep(mean(ldf[[i]]$count),length(ldf[[i]]$count))
ldf[[i]]$SD = rep(sd(ldf[[i]]$count),length(ldf[[i]]$count))
}
dftt= ldf[[1]]
for (i in 2:length(ldf)) {
dftt <- rbind(dftt,ldf[[i]])
}
dftt
# dftt$sample <- factor(df$sample,levels = c("placebo","R40","M15","R40_M15","Mock"))
ggplot(dftt,aes(x=sample,y=count))+
geom_bar(aes(x=sample,y=count,color=sample),stat = "summary",fun=median,alpha=0,width = 0.6,size=2)+
geom_jitter(width = 0.15,height=2,size=5,color="black",shape=16)+
geom_errorbar(aes(ymax=mean+SD,ymin=mean-SD),width = 0.35,size=1)+
scale_y_continuous(expand = c(0,0),limits = c(0,max(dftt$count)+0.2*max(dftt$count)))+
theme_classic()+
theme(axis.ticks.length = unit(.25, "cm"),axis.line = element_line(linewidth = 0.8),
axis.ticks = element_line(linewidth = 0.8,),
axis.text = element_text(size = 20,color="black"),
axis.title = element_text(size = 25,color = "black"),
axis.title.x = element_text(vjust = -2),
axis.title.y = element_text(vjust = 3),
axis.text.x = element_text(angle = 45,vjust = 1,hjust = 1),
plot.title = element_text(hjust=0.5,size=20),
plot.margin = margin(1,1,1,1, unit = "cm"),text = element_text(family="Arial"))+
geom_signif(data=dftt,stat="signif",position="identity",
comparisons=list(c("Mock","R40_M15")),map_signif_level = TRUE,annotations="***",size=1,textsize=12)+
scale_x_discrete(limits = factor(c("placebo","R40","M15","R40_M15","Mock")))+
labs(
x="Group", # x轴名称
y="Related mRNA fold change", # y轴名称
title=paste0("IL1","——表达") # 标题名称
)
"ccl2.csv"文件内容如下:
sample,count
placebo,884.87
placebo,520
placebo,430
R40,693.05
R40,375
R40,431
M15,271.09
M15,237
M15,327
R40_M15,149.18
R40_M15,201
R40_M15,102
Mock,1
Mock,1
Mock,1