绘图导航
  • 基础绘图
  • 高级绘图
  • 微生物
  • 转录组

R语言|绘制Donut chart

 二维码 5
作者:图图来源:图图云平台

R语言绘制甜甜圈图

看到挺多小伙伴画这个图了,甜甜圈图,小编也试着画了一画,大家可以瞅瞅。


library(ggplot2)

library(dplyr)

#数据

df <- read.csv("otu_table3.csv", header = T, check.names=F)

hsize <- 3 #调整圆环的刻度

df <- df %>%

  mutate(x = hsize)




M01单个样本所含细菌的丰度可视化(也可以画其他样本的)

#绘图

p1<-ggplot(df, aes(x = hsize, y =M01 , fill =Id)) +

  geom_col(width= 1,color = "black") + #边界颜色

  coord_polar(theta = "y") +

  scale_fill_manual(values = c("#74A9CF", "#0570B0","#3C5488B2","#00A087B2", "#F39B7FB2","#91D1C2B2", "#8491B4B2","#FFF7FB", "#D0D1E6")) +

  xlim(c(0.2, hsize + 0.5))

p1

#添加标签

p2<-ggplot(df, aes(x = hsize, y = M01, fill = Id)) +

  geom_col() +

  geom_label(aes(label = M01),

             position = position_stack(vjust = 0.5),

             show.legend = FALSE) +

  coord_polar(theta = "y") +

  scale_fill_manual(values = c("#74A9CF", "#0570B0","#3C5488B2","#00A087B2", "#F39B7FB2","#91D1C2B2", "#8491B4B2","#FFF7FB", "#D0D1E6")) +

  xlim(c(0.2, hsize + 0.5))+

  guides(fill = guide_legend(title = "M01")) #修改标签

p2

两个样本一起

#绘制多个样本

p3<-ggplot(df) +

  geom_col(aes(x = 2, y = M01, fill = Id), width= 1,color = "black") +

  geom_col(aes(x = 3, y = M09, fill = Id), width= 1,color = "black")+

  coord_polar("y") +

  scale_fill_manual(values = c("#74A9CF", "#0570B0","#3C5488B2","#00A087B2", "#F39B7FB2","#91D1C2B2", "#8491B4B2","#FFF7FB", "#D0D1E6")) +

  xlim(c(0.2, hsize + 0.5))+

  guides(fill = guide_legend(title = "otu")) #修改标签

p3

“作图帮”微信公众号同步更新

作图帮二维码.jpg


可添加下方微信进入【生信作图交流群】,群内免费分享绘图代码与示例数据

公司二维码.png



推荐使用谷歌浏览器点击下载