經常遇到‘一個循環繪製每一個差異基因在腫瘤和正常的表達差異’和‘需要在一張圖中展示多個差異基因在腫瘤和正常組的表達分佈’需求。如下列兩張圖所示:
- 首選應得到data如下列所示:
然後循環作圖:
list <- names(data)[2:11]
list
plot_list = list()
for (i in 1:3) {
p = ggboxplot(data, x='group', y= as.character(list[i]),
bxp.errorbar = T, color = 'group',
outlier.shape = NA, add = "point",
palette =c("#00AFBB","#FC4E07") )+stat_compare_means()
plot_list[[i]] = p }
# Another option: create pdf where each page is a separate plot.
pdf("plots.pdf")
for (i in 1:10) {print(plot_list[[i]]) }
dev.off()
- 將寬數據data轉換成長數據data1
#=======================================================
#一張圖
#=======================================================
library(reshape2)
library(tidyr)
colnames(data)
names(data)[2]
names(data)[11]
#使用data數據框中非subtype的第一個列名和最後一個列名
data1 <- gather(data, gene, count, BCL11A:ZCCHC7 )
head(data1)
p <- ggboxplot(data1, x = "gene", y = "count",size=0.5, bxp.errorbar = T,
color = "group", palette =c( "#FC4E07", "#00AFBB"),
add.params = list(size=0.5), outlier.shape = NA,
add = "point") +
stat_compare_means(aes(group = group), label = "p.format")
p
pdf(file = 'expr_subtype.pdf', height = 4,width = 14)
p
dev.off()