使用ggplot2绘制菌群组成堆叠图

堆叠图主要看不同菌群在该样本中的相对比例,把每个样本的情况或者每个处理组的情况展示出来,可以大致看出样本的重复性好坏和不同处理间的某些菌群变化趋势。

1.门水平的堆叠图
###设置工作目录
getwd()
path1 = ("C:/Users/Administrator/Desktop/R_work/03_irradiation_gutMicro/00_visualization")
setwd(path1)
list.files()
############门水平组成堆叠图#####################################
####读取门水平数据
phylum <- read.delim('phylum_table.txt', row.names = 1, sep = '\t', stringsAsFactors = FALSE, check.names = FALSE)

#将门水平进行排序
phylum$sum <- rowSums(phylum)
phylum <- phylum[order(phylum$sum, decreasing = TRUE), ]
phylum
#选取相对丰度
phylum_top10 <- phylum[1:3, -ncol(phylum)]
phylum_top10['Others', ] <- 100 - colSums(phylum_top10)
phylum_top10

####ggplot2堆叠图
library(reshape2)   
library(ggplot2)    

#调整数据布局
phylum_top10$Taxonomy <- factor(rownames(phylum_top10), levels = rev(rownames(phylum_top10)))
phylum_top10 <- melt(phylum_top10, id = 'Taxonomy')

#添加分组信息
group <- read.delim('group.txt', sep = '\t', stringsAsFactors = FALSE)
names(group)[1] <- 'variable'
phylum_top10 <- merge(phylum_top10, group, by = 'variable')

#因子排序,对样品排序,对分组排序
phylum_top10$variable <- factor(phylum_top10$variable, 
                                levels = c("UN1_1", "UN1_2",    "UN1_3",    "UN1_4",    
                                           "UN1_5", "UN1_6",    "IR1_1",    "IR1_2",
                                           "IR1_3", "IR1_4",    "IR1_5",  "IR1_6",
                                           "UN7_1", "UN7_2",    "UN7_3",    "UN7_4",    
                                           "UN7_5", "UN7_6",    "IR7_1",    "IR7_2",
                                           "IR7_3", "IR7_4",    "IR7_5",    "IR7_6",
                                           "UN14_1",    "UN14_2",   "UN14_3",   "UN14_4",
                                           "UN14_5",    "UN14_6",   "IR14_1",   "IR14_2",
                                           "IR14_3",    "IR14_4",   "IR14_5",   "IR14_6"), ordered = TRUE)
phylum_top10$group <- factor(phylum_top10$group,
                                levels = c("Unirradiated","Irradiated"),ordered = TRUE)
phylum_top10

#作图
#颜色不够用,设置更多梯度变化的颜色
#colourCount = length(unique(phylum_top10$Taxonomy))
#getPalette = colorRampPalette(brewer.pal(9, "Set1"))
p <- ggplot(phylum_top10, aes(variable,  value, fill = Taxonomy)) +
  geom_col(position = "stack", width = 0.8) +
  facet_wrap(~group, scales = 'free_x', ncol = 2) +
  scale_fill_manual(values =  rev(c( "#E64B35B2", "#4DBBD5B2",
                                    "#3C5488B2","yellow"))) +
  labs(x = '', y = 'Relative Abundance(%)', fill = "Phylum") +
  theme(panel.grid = element_blank(),
        panel.background = element_rect(color = '#636363', 
                                        fill = 'transparent'),
        strip.text = element_text(size = 20),
        strip.text.x = element_text(size = 20, colour = "black", 
                                    face = "bold"),
        strip.background = element_rect(color="grey95", 
                                        fill="grey95",
                                        size=1.5, 
                                        linetype="solid"),
        legend.title = element_text(size = 15,face = "bold",
                                    vjust = 1, hjust = 0), 
        legend.text = element_text(size = 12),
        legend.key.size=unit(.5,'cm'),
        axis.title.x = element_text(size = 16, vjust = 0.5, 
                                    hjust = 0.5),
        axis.title.y = element_text(size = 15, vjust = 0.5, 
                                    hjust = 0.5),
        axis.text.x = element_text(angle = 90, size = 10,
                                   vjust = 0.5, hjust = 0.5),
        axis.text.y = element_text(size = 15,vjust = 0.5, 
                                   hjust = 0.5),
        legend.position = "bottom") + 
  guides(fill=guide_legend(nrow=1,reverse=TRUE)) 
 
p 

2.科水平的堆叠图

和门水平的代码基本一样,这样重复的代码越来越多,经常是在满眼的代码中寻找参数去修改调整,眼要瞎,逼迫我使用function()和一些循环函数如for,lapply等,这次还是重复代码跑一遍,下次一定要简化工作!

############科水平组成堆叠图#####################################
####读取科水平数据

family <- read.delim('family_table.txt', row.names = 1, sep = '\t', stringsAsFactors = FALSE, check.names = FALSE)

#将门水平进行排序
family$sum <- rowSums(family)
family <- family[order(family$sum, decreasing = TRUE), ]
family
#选取相对丰度
family_top20 <- family[1:21, -ncol(family)] 
family_top20
#排序后数据导出成csv文件
write.csv(family_top20, 'family_top20.csv', quote = FALSE)


####ggplot2堆叠图
library(reshape2)   
library(ggplot2)    

#调整数据布局
family_top20$Taxonomy <- factor(rownames(family_top20), 
                                levels = rev(rownames(family_top20)))
family_top20 <- melt(family_top20, id = 'Taxonomy')

#添加分组信息
group <- read.delim('group.txt', sep = '\t', stringsAsFactors = FALSE)
names(group)[1] <- 'variable'
family_top20 <- merge(family_top20, group, by = 'variable')

#因子排序,对样品排序,对分组排序
family_top20$variable <- factor(family_top20$variable, 
                                levels = c("UN1_1", "UN1_2",    "UN1_3",    "UN1_4",    
                                           "UN1_5", "UN1_6",    "IR1_1",    "IR1_2",
                                           "IR1_3", "IR1_4",    "IR1_5",  "IR1_6",
                                           "UN7_1", "UN7_2",    "UN7_3",    "UN7_4",    
                                           "UN7_5", "UN7_6",    "IR7_1",    "IR7_2",
                                           "IR7_3", "IR7_4",    "IR7_5",    "IR7_6",
                                           "UN14_1",    "UN14_2",   "UN14_3",   "UN14_4",
                                           "UN14_5",    "UN14_6",   "IR14_1",   "IR14_2",
                                           "IR14_3",    "IR14_4",   "IR14_5",   "IR14_6"), ordered = TRUE)
family_top20$group <- factor(family_top20$group,
                             levels = c("Unirradiated","Irradiated"),ordered = TRUE)
family_top20

#作图
#颜色不够用,设置更多梯度变化的颜色
#colourCount = length(unique(family_top20$Taxonomy))
#getPalette = colorRampPalette(brewer.pal(9, "Set1"))
p1 <- ggplot(family_top20, aes(variable,  value, fill = Taxonomy)) +
  geom_col(position = 'stack', width = 0.8) +
  facet_wrap(~group, scales = 'free_x', ncol = 2) +
  scale_fill_manual(values =  rev(c( "#3C5488B2","#00A087B2", 
                                    "#F39B7FB2","#91D1C2B2", 
                                    "#8491B4B2", "#DC0000B2", 
                                    "#7E6148B2","yellow", 
                                    "darkolivegreen1", "lightskyblue", 
                                  "darkgreen", "deeppink", "khaki2", 
                                  "firebrick", "brown1", "darkorange1", 
                                  "cyan1", "royalblue4", "darksalmon", 
                                  "darkgoldenrod1", "darkseagreen", "darkorchid"))) +
  labs(x = '', y = 'Relative Abundance(%)', fill = "Family") +
  theme(panel.grid = element_blank(),
        panel.background = element_rect(color = '#636363', 
                                        fill = 'transparent'),
        strip.text = element_text(size = 20),
        strip.text.x = element_text(size = 20, colour = "black", face = "bold"),
        strip.background = element_rect(color="grey95", 
                                        fill="grey95",
                                        size=1.5, 
                                        linetype="solid"),
        legend.title = element_text(size = 15,face = "bold",
                                    vjust = 1, hjust = 0),
        legend.text = element_text(size = 11),
        legend.key.size=unit(.4,'cm'),
        axis.title.x = element_text(size = 16, vjust = 0.5, 
                                    hjust = 0.5),
        axis.title.y = element_text(size = 15, vjust = 0.5, 
                                    hjust = 0.5),
        axis.text.x = element_text(angle = 90, size = 10,
                                   vjust = 0.5, hjust = 0.5),
        axis.text.y = element_text(size = 15,vjust = 0.5, 
                                   hjust = 0.5),
        legend.position="bottom") + 
  guides(fill=guide_legend(nrow=6,reverse=TRUE))


p1

3.拼图
#拼图
library(patchwork)
p/p1

堆叠图的配色比较重要,颜色不好看起来很乱很躁,谷歌出来的一套配色觉得还行。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,324评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,303评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,192评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,555评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,569评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,566评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,927评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,583评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,827评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,590评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,669评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,365评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,941评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,928评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,159评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,880评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,399评论 2 342