美文网首页
跟着NC学作图 | 柱状图新画法 (环状柱状图)

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

作者: 小杜的生信筆記 | 来源:发表于2023-03-14 10:05 被阅读0次

    本教程绘制的图形是来自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 ) 
    

    ENDING !


    往期文章:
    1. 最全WGCNA教程(替换数据即可出全部结果与图形)

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

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

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

    2. 精美图形绘制教程

    精美图形绘制教程

    相关文章

      网友评论

          本文标题:跟着NC学作图 | 柱状图新画法 (环状柱状图)

          本文链接:https://www.haomeiwen.com/subject/vfchrdtx.html