基因组数据绘图ggplot2绘图农药

R可视化:圆环图

2023-04-06  本文已影响0人  生信学习者2

圆环图相比饼图更好看一些(个人感觉)

数据准备

# 加载R包
library("ggplot2") 
library("dplyr") 

# 数据准备
plotdata <- data.frame(
  class = c("1st", "2nd", "3rd", "Crew"),
  n = c(325, 285, 706, 885),
  prop = c(14.8, 12.9, 32.1, 40.2)
)

# 按照分组排序添加标签的位置信息
plotdata <- plotdata %>%
  arrange(desc(class)) %>%
  mutate(lab.ypos = cumsum(prop) - 0.5*prop) %>% # 标签放置在中心位置
  mutate(num_prop = paste0("n = ", n, "\n", 
                           paste0(prop, "%")))

饼图

mycols <- c("#0073C2FF", "#EFC000FF", "#868686FF", "#CD534CFF")
ggplot(plotdata, aes(x = "", y = prop, fill = class)) +
  geom_bar(width = 1, stat = "identity", color = "white") +
  coord_polar("y", start = 0)+
  geom_text(aes(y = lab.ypos, label = prop), color = "white")+
  scale_fill_manual(values = mycols) +
  theme_void()

圆环图

p <- ggplot(plotdata, aes(x = 2, y = prop, fill = class)) +
  geom_bar(stat = "identity", color = "white") +
  coord_polar(theta = "y", start = 0)+
  geom_text(aes(y = lab.ypos, label = num_prop), color = "white")+
  scale_fill_manual(values = mycols) +
  theme_void()+
  xlim(0.5, 2.5)
p

尝试把legend放到中间圆环过,但是没有成功,大家有什么好办法吗?

p + annotate("text", x = 1, y = 1, label = "donut chart")

Reference

上一篇下一篇

猜你喜欢

热点阅读