跟着NC学作图 | 柱状图新画法 (环状柱状图)


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

绘图

加载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教程(替换数据即可出全部结果与图形)

WGCNA分析 | 全流程分析代码 | 代码一

WGCNA分析 | 全流程分析代码 | 代码二

WGCNA分析 | 全流程代码分享 | 代码三

2. 精美图形绘制教程

精美图形绘制教程


话说公众号需要标星,这样公众号的内容你才不会错过。那么,我们也动手标一下吧。


小杜的生信筆記 ,主要发表或收录生物信息学的教程,以及基于R的分析和可视化(包括数据分析,图形绘制等);分享感兴趣的文献和学习资料!!

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小杜的生信筆記

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值