手把手演示R语言绘制多个基因表达值小提琴图
白介素2的读书笔记,分享临床科研干货,一起见证时间的力量
数据准备
#set your work directory
data<-read.csv(file = "data.csv",header = T,sep = ",")
colnames(data)<-c("gene",colnames(data)[-1])
test<-data[1:5,1:5]
View(test)
###
data<-aggregate(data[,2:ncol(data)],by=list(data$gene),FUN = mean,na.rm=T)##重复基因求平均值
genename<-data$Group.1
rownames(data)<-genename
data<-data[,-1]
sampleID<-colnames(data)
data<-apply(data, 1, as.numeric)
rownames(data)<-sampleID
##分组信息构造
group<-c(rep("N",14),rep("DCIS",9),rep("IBC",9),rep("NS",14),rep("DCISS",11),rep("IBCS",9))
length(group)==dim(data)[[1]]##确认信息匹配## [1] TRUE
data<-as.data.frame(data)
data$group<-group
table(data$group)##
## DCIS DCISS IBC IBCS N NS
## 9 11 9 9 14 14data[1:5,1:5]##行为sample名 列为gene
## ACVR1B CXCR4 IL11 INHBA LTB
## N1 3.812931 3.669153 2.922135 2.916415 3.072535
## N2 2.968409 4.270815 2.988273 3.068690 2.757790
## N3 3.806364 4.327977 2.811002 2.546459 2.897796
## N4 3.621140 3.698909 2.815539 3.278493 2.785017
## N5 3.812910 4.214652 2.814199 2.800704 2.916013
整合小提琴图
# http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/
library(ggpubr)## Loading required package: ggplot2
## Loading required package: magrittr
my_comparisons <- list(c("IBC", "DCIS"), c("N", "DCIS"), c("IBCS", "DCISS"),c("NS" ,"DCISS"))##分组设定
#my_comparisons <- list( c("IBC", "DCIS"), c("N", "DCIS") )##分组设定
e<-data %>%
#dplyr::filter(group %in% c("IBC","N","DCIS")) %>%
ggviolin(x = "group", y = c(colnames(data)[1:6]), fill = "group",
combine = T,
#palette = c("#00AFBB", "#E7B800", "#FC4E07"),##
ylab="Normalized Expression",
add = "boxplot", add.params = list(fill = "white"))
e+stat_compare_means(method = "t.test",
#label = "p.signif",##星号设置
comparisons = my_comparisons)
#theme_gray(base_size = 14)##background
ggsave("all_violin.pdf", width = 10, height = 8)
小提琴图DCIS VS. IBC/N
library(ggpubr)
my_comparisons <- list( c("IBC", "DCIS"), c("N", "DCIS") )##分组设定
e<-data %>%
dplyr::filter(group %in% c("IBC","N","DCIS")) %>% #筛选行
ggviolin(x = "group", y = c(colnames(data)[1:6]), fill = "group",
combine = T,
#palette = c("#00AFBB", "#E7B800", "#FC4E07"),##颜色设置
ylab="Normalized Expression",
add = "boxplot", add.params = list(fill = "white"))
e+stat_compare_means(method = "t.test",
#label = "p.signif",##星号设置
comparisons = my_comparisons)
#theme_gray(base_size = 14)##background
ggsave("group1_violin.pdf", width = 10, height = 8)
小提琴图DCISS VS. IBCS/NS
library(ggpubr)
my_comparisons <- list(c("IBCS","DCISS"),c("NS","DCISS"))##分组设定
e<-data %>%
dplyr::filter(group %in% c("IBCS","NS","DCISS")) %>%
ggviolin(x = "group", y = c(colnames(data)[1:6]), fill = "group",
combine = T,
#palette = c("#00AFBB", "#E7B800", "#FC4E07"),##
ylab="Normalized Expression",
add = "boxplot", add.params = list(fill = "white"))
e+stat_compare_means(comparisons = my_comparisons)
ggsave("group2_violin.pdf", width = 10, height = 8)
箱线图
my_comparisons <- list(c("IBC", "DCIS"), c("N", "DCIS"), c("IBCS", "DCISS"),c("NS" ,"DCISS"))##分组设定
e<-data %>%
#dplyr::filter(group %in% c("IBCS","NS","DCISS")) %>%
ggboxplot(x = "group", y = c(colnames(data)[1:6]), fill = "group",
combine = T,
#palette = c("#00AFBB", "#E7B800", "#FC4E07"),##
ylab="Normalized Expression")
e+stat_compare_means(comparisons = my_comparisons)
ggsave("group2_boxplot.pdf", width = 10, height = 8)