(本案例涉及分面,修改图例,统计分析,添加p值,修改X,Y轴标签)
1. 读取数据:
rm(list = ls())
data = read.cdv("data.csv")
结果:出师不利,
使用data.table包试试~
结果:成功,提前将计数资料转变为因子,便于统计分析
library(data.table)
data = fread("4T1_3T3CM.csv")
data$time = as.factor(data$time)
data$group = as.factor(data$group)
2. 分组:
my_comparisons <- list(c('A','B'),c('B','C'))
#本案例有四个分组,在此仅选择A与B和B与C比较。根据个人需要,可以进行调整。
统计分析基于ggpubr包, 需要用到compare_means()或stat_compare_means()函数,两者差别如下:
>本案例作图仅用stat_compare_means()函数,更多细节详见:Add P-values and Significance Levels to ggplots - Articles - STHDA
###3. 重点:作图+统计分析
library(ggplot2)
library(ggpubr)
plot1 = ggplot(data, aes(group,Ptgs2, color = group)) +
geom_boxplot()+
geom_point()+
结果:初步得到一个数据。
但不是我想要的结果。我设定了三个时间段:12h,24h,48h。接下来将数据按照不同时间分页显示。
plot = ggplot(data, aes(group,Ptgs2,color = group)) +
geom_boxplot()+
geom_point()+
facet_wrap(~time)
结果:结果还算满意,有待进步
我想修改X轴,Y轴的标签以及添加图的title。网页支持:GGPlot Axis Labels: Improve Your Graphs in 2 Minutes - Datanovia
plot = ggplot(data, aes(group,Ptgs2,color = group)) +
geom_boxplot()+
geom_point()+
facet_wrap(~time)+
labs(title ="Ptgs2",x="groups", y="gene expression level")
结果:
太棒了,一步到位。
除了labs()函数,还可以用xlab(), ylab()函数单独对X轴,Y轴标签进行修改~
进一步统计分析:
plot = ggplot(data, aes(group,Ptgs2,color = group)) +
geom_boxplot()+
geom_point()+
facet_wrap(~time)+
labs(title ="Ptgs2",x="groups", y="gene expression level")+
stat_compare_means(method = "anova", label.y = 4.5)+
stat_compare_means(comparisons = my_comparisons, method = "t.test")
结果:
常用的单因素统计方法如下,可根据实际需要进行选择:
进一步调整,修改图例的内容,具体化各组,不想用ABCD来表示, 需要用到scale_color_discrete()函数:
plot = ggplot(data, aes(group,Ptgs2,color = group))+
geom_boxplot()+
geom_point()+
facet_wrap(~time)+
labs(title ="Ptgs2",x="groups", y="gene expression level")+
scale_color_discrete(name="groups", labels = c("control1", "control2", "treat1", "treat2"))+
stat_compare_means(method = "anova", label.y = 4.5)+
stat_compare_means(comparisons = my_comparisons, method = "t.test")
结果:
关于图例的修改,需要具体情况具体分析,
如果图示分组是通过color参数进行设定的,图例的修改需要用scale_color_discrete()函数进行修改~
如果图示分组是通过fill参数进行设定的,图例的修改则选用scale_fill_discrete()函数进行修改~
plot = ggplot(data, aes(group,Ptgs2,fill= group))+
geom_boxplot()+
geom_point()+
facet_wrap(~time)+
labs(title ="Ptgs2",x="groups", y="gene expression level")+
scale_fill_discrete(name="groups", labels = c("control1", "control2", "treat1", "treat2"))+
stat_compare_means(method = "anova", label.y = 4.5)+
stat_compare_means(comparisons = my_comparisons, method = "t.test")
结果:
也可以对X轴的分组进行修改,使用scale_x_discrete()函数:
plot = ggplot(data, aes(group,Ptgs2,color= group))+
geom_boxplot()+
geom_point()+
facet_wrap(~time)+
labs(title ="Ptgs2",x="groups", y="gene expression level")+
scale_x_discrete(name="groups", labels = c("control1", "control2", "treat1", "treat2"))+
stat_compare_means(method = "anova", label.y = 4.5)+
stat_compare_means(comparisons = my_comparisons, method = "t.test")
结果:
完整代码:
rm(list = ls())
library(data.table)
data = fread("4T1_3T3CM.csv")
data$time = as.factor(data$time)
data$group = as.factor(data$group)
my_comparisons <- list(c('A','B'),c('B','C'))
library(ggplot2)
library(ggpubr)
plot = ggplot(data, aes(group,Ptgs2,color = group))+
geom_boxplot()+
geom_point()+
facet_wrap(~time)+
labs(title ="Ptgs2",x="groups", y="gene expression level")+
scale_color_discrete(name="groups", labels = c("control1", "control2", "treat1", "treat2"))+
stat_compare_means(method = "anova", label.y = 4.5)+
stat_compare_means(comparisons = my_comparisons, method = "t.test")
首次在简书上分享学习经历~作为生信小白~之前也没有过多地接触R。本文可能存在错误,希望有大佬可以及时点出 ~
另外此前接受过生信技能树的课程培训,为我的生信基础和R语言基础提供了奠基,在此特别感谢生信技能树。在生信学习的路上,我特别感谢lina zhu 对我的帮助,本人朋友+堂妹,她毕业于香港城市大学生物信息学专业。在她的指点下,我的生信技能有了极大的提升~
时间:20210602