
小杜的生信笔记
V1
2023/03/14阅读:15主题:雁栖湖
跟着NC学作图 | 柱状图新画法 (环状柱状图)
是小杜好友的可以直接和小杜索要源代码和实例数据,我希望的是:交流(Communication)!!欢迎来扰!!不要客气,要不你的加友费浪费了哦!> 是小杜好友的可以直接和小杜索要源代码和实例数据,我希望的是:交流(Communication)!!欢迎来扰!!不要客气,要不你的加友费浪费了哦!

本教程绘制的图形是来自NC
期刊的图形,我不知道叫什么图形,但是仔细一看就是的属于“环状柱状图”,那就是这样叫吧! 看着还算是比较新颖的,那就学一下吧!!
1绘图
加载R包
library(ggplot2)
加载数据

绘制基础图形
p <- ggplot(data, aes(x=as.factor(id), y=est, fill=group)) + # Note that id is a factor. If x is numeric, there is some space between the first bar
geom_bar(aes(x=as.factor(id), y=est, fill=group), stat="identity", alpha=1) +
geom_errorbar(aes(ymin = est-se, ymax = est+se),width = 0.2,position = position_dodge(.9), size = .5, alpha=1)

增加一个val
p2 <- p +
geom_segment(data=grid_data, aes(x = end, y = 1, xend = start, yend = 1), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
geom_segment(data=grid_data, aes(x = end, y = 0.8, xend = start, yend = 0.8), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
geom_segment(data=grid_data, aes(x = end, y = 0.6, xend = start, yend = 0.6), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
geom_segment(data=grid_data, aes(x = end, y = 0.4, xend = start, yend = 0.4), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
geom_segment(data=grid_data, aes(x = end, y = 0.2, xend = start, yend = 0.2), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
geom_segment(data=grid_data, aes(x = end, y = 0, xend = start, yend = 0), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE )

在val中添加数字
p3 <- p2 +
annotate("text", x = rep(max(data$id),6), y = c(0, 0.2, 0.4, 0.6, 0.8, 1),
label = c("0", "0.2", "0.4", "0.6", "0.8", "1") , color="black", size=4 , angle=0, hjust=1)

将y轴进行延伸
p4 <- p3+
geom_bar(aes(x=as.factor(id), y=est, fill=group), stat="identity", alpha=0.5) +
ylim(-1,3)

进行美化
p5 <- p4 +
theme_minimal() +
theme(
legend.position = "right",
legend.title = element_blank(),
legend.text = element_text(size=12),
axis.text = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank())

变成环状图形
使用coord_polar()
函数进行转换
p6 <- p5 + coord_polar()

颜色美化
p7 <- p6 + scale_fill_manual(breaks = c("Intellectual disabilities",
"Communication disorders",
"ASD",
"ADHD",
"Specific learning disorders",
"Dyslexia-related phenotypes",
"Dysgraphia-related phenotypes",
"Dyscalculia-related phenotypes",
"Motor disorders"),
values = c("#184e77","#1e6091","#1a759f",
"#168aad","#34a0a4","#52b69a",
"#76c893","#99d98c","#b5e48c"))+
ggtitle("Heritability of sub-categories of Neurodevelopmental Disorders")

标注映射到柱子上
p8 <- p7 +
geom_text(data=label_data, aes(x=id, y= est+se+0.2, label=pheno, hjust=hjust),
color="black", alpha=1, size=4, angle= label_data$angle, inherit.aes = FALSE )

完整代码
ggplot(data, aes(x=as.factor(id), y=est, fill=group)) + # Note that id is a factor. If x is numeric, there is some space between the first bar
geom_bar(aes(x=as.factor(id), y=est, fill=group), stat="identity", alpha=1) +
geom_errorbar(aes(ymin = est-se, ymax = est+se),width = 0.2,position = position_dodge(.9), size = .5, alpha=1)+
# 添加 val
geom_segment(data=grid_data, aes(x = end, y = 1, xend = start, yend = 1), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
geom_segment(data=grid_data, aes(x = end, y = 0.8, xend = start, yend = 0.8), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
geom_segment(data=grid_data, aes(x = end, y = 0.6, xend = start, yend = 0.6), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
geom_segment(data=grid_data, aes(x = end, y = 0.4, xend = start, yend = 0.4), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
geom_segment(data=grid_data, aes(x = end, y = 0.2, xend = start, yend = 0.2), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
geom_segment(data=grid_data, aes(x = end, y = 0, xend = start, yend = 0), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
# 添加val中的数字
annotate("text", x = rep(max(data$id),6), y = c(0, 0.2, 0.4, 0.6, 0.8, 1),
label = c("0", "0.2", "0.4", "0.6", "0.8", "1") , color="black", size=4 , angle=0, hjust=1) +
geom_bar(aes(x=as.factor(id), y=est, fill=group), stat="identity", alpha=0.5) +
ylim(-1,3) +
theme_minimal() +
theme(
legend.position = "right",
legend.title = element_blank(),
legend.text = element_text(size=12),
axis.text = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank()) +
## 变环
coord_polar() +
## 柱子标注
scale_fill_manual(breaks = c("Intellectual disabilities",
"Communication disorders",
"ASD",
"ADHD",
"Specific learning disorders",
"Dyslexia-related phenotypes",
"Dysgraphia-related phenotypes",
"Dyscalculia-related phenotypes",
"Motor disorders"),
## 柱子颜色
values = c("#184e77","#1e6091","#1a759f",
"#168aad","#34a0a4","#52b69a",
"#76c893","#99d98c","#b5e48c"))+
ggtitle("Heritability of sub-categories of Neurodevelopmental Disorders")+
## 将标注映射到柱子上
geom_text(data=label_data, aes(x=id, y= est+se+0.2, label=pheno, hjust=hjust),
color="black", alpha=1, size=4, angle= label_data$angle, inherit.aes = FALSE )
ENDING !
往期文章: 1. 最全WGCNA教程(替换数据即可出全部结果与图形)
2. 精美图形绘制教程
话说公众号需要标星,这样公众号的内容你才不会错过。那么,我们也动手标一下吧。
![]()
小杜的生信筆記 ,主要发表或收录生物信息学的教程,以及基于R的分析和可视化(包括数据分析,图形绘制等);分享感兴趣的文献和学习资料!!
作者介绍

小杜的生信笔记
V1
公众号、知乎等平台同名!!