2019-08-06用ggplot2绘制两侧柱状图

作者: iColors | 来源:发表于2019-08-06 10:20 被阅读1次
    1、首先构建一个虚拟的数据
    df <-data.frame(gene=paste("gene",c(1:10)),exp=c(1,-1,2,-2,2,-3,4,5,-5,6))
    
    2、绘制基本图形
    library(ggplot2)
    ggplot(df,aes(x = gene,
               y=exp,
               fill = gene,
               label = exp))+
        geom_col(show.legend = FALSE) +
        xlab('Gene Name') +
        ggtitle('Expression level of significant changed genes') 
    
    image.png

    3、不好看😝,调整一下坐标轴

    ggplot(df,aes(x = gene,
               y=exp,
               fill = gene,
               label = exp))+
        geom_col(show.legend = FALSE) +
        coord_flip() +
        xlab('Gene Name') +
        ggtitle('Expression level of significant changed genes') 
    
    image.png
    4、好多了,再改改排列顺序
    ggplot(df,aes(x = reorder(gene,exp),y=exp,
               fill = gene,
               label = exp))+
        geom_col(show.legend = FALSE) +
        coord_flip() +
        xlab('Gene Name') +
        ggtitle('Expression level of significant changed genes') 
    
    image.png
    5、还差点什么,对了,把数字标注到柱子上
    ggplot(df,aes(x = reorder(gene,exp),y=exp,
               fill = gene,
               label = exp))+
        geom_col(show.legend = FALSE) +
        coord_flip() +
        geom_text(nudge_x = 0.1) +
        xlab('Gene Name') +
        ggtitle('Expression level of significant changed genes')
    
    image.png
    6、最后用ggtheme美颜一下,齐活!
    library(ggthemes)
    ggplot(df,aes(x = reorder(gene,exp),y=exp,
               fill = gene,
               label = exp))+
        geom_col(show.legend = FALSE) +
        coord_flip() +
        geom_text(nudge_x = 0.1) +
        xlab('Gene Name') +
        ggtitle('Expression level of significant changed genes')+
        theme_clean()
    
    image.png

    相关文章

      网友评论

        本文标题:2019-08-06用ggplot2绘制两侧柱状图

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