R绘制圆环图

哈喽!继小编给大家介绍完韦恩图系列的绘图工具后,今天咱们来看看R如何绘制圆环图,小编将使用Rggplot2包来进行操作,赶快码起来吧!

数据格式长这样(以下作图数据如果有小伙伴需要的话可以扫描文章底部二维码进群以获得链接)
R绘制圆环图
具体操作如下:

1.先把示例数据按数值高低排序,并选取特定作图子集;

#加载"reshape2"包
library(reshape2)

#读取数据,按总丰度大小排序,并将 top10 外的类群合并为“Others”
data <- read.delim('phylum_table.txt', sep = '\t', stringsAsFactors = FALSE, check.names = FALSE)
data <- data[order(rowSums(data[-1])), ]
data$Taxonomy <- factor(data$Taxonomy, levels = c('Others', as.vector(data$Taxonomy[-which(data$Taxonomy == 'Others')])))
sample <- melt(data, id = 'Taxonomy')

#挑选其中的一个样本子集,并按丰度大小排序
sample1 <- subset(sample, variable == 'A1')
sample1 <- sample1[order(sample1$value), ]
sample1$Taxonomy <- factor(sample1$Taxonomy, levels = c('Others', as.vector(sample1$Taxonomy[-which(sample1$Taxonomy == 'Others')])))

2.加载ggplot2包,使用只包含一个样本的细菌丰度数据子集“A1”,绘制圆环图,展示该样本中各细菌类别丰度组成;

#加载 ggplot2包
library(ggplot2)
#自定义颜色
color=c( "#3C5488B2","#00A087B2", 
         "#F39B7FB2","#91D1C2B2", 
         "#8491B4B2", "#DC0000B2", 
         "#7E6148B2","yellow", 
         "darkolivegreen1","cyan1", "lightskyblue", 
         "darkgreen", "khaki2", 
         "firebrick", "brown1", "darkorange1", 
           "darksalmon", 
         "darkgoldenrod1", "darkseagreen","darkorchid")
##以A1为例作图
#先得到柱形图
p <- ggplot(sample1, aes(x = Taxonomy, y = value, fill = Taxonomy)) + 
  geom_bar(stat = 'identity', width = 1) +
  scale_fill_manual(values = color[1:length(unique(sample1$Taxonomy))]) +
  theme(panel.grid = element_blank(), panel.background = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), plot.title = element_text(hjust = 0.5)) +
  labs(x = '', y = '')+
  guides(fill=guide_legend(title=NULL))

#再经极坐标系变换得到圆环图
p + coord_polar(theta = 'y')

作图如下:
R绘制圆环图
3.使用包含所有样本的作图数据 “sample”,绘制圆环图,展示示例数据*计7个样本中各细菌类别的丰度组成水平;

##以 sample 为例
#先得到柱形图
p <- ggplot(sample, aes(variable, value, fill = Taxonomy)) +
  geom_bar(stat = 'identity', width = 0.8) +
  scale_fill_manual(values = color[1:length(unique(sample1$Taxonomy))]) +
  labs(x = '', y = '') +
  theme(panel.grid = element_blank(), panel.background = element_blank(), axis.text.x = element_blank())+
  guides(fill=guide_legend(title=NULL))
#再经极坐标变换得到圆环图
p + coord_polar(theta = 'y')

R绘制圆环图
好啦,今天的图画完啦,小伙伴想了解更多绘图软件,可关注“作图帮”公众号,或微信扫码下方二维码进群,在线为您解答科研绘图相关问题!

R绘制圆环图

上一篇:ggplot2(7) 定位


下一篇:Java帝国的诞生