柱状图|GO、KEGG|标签与柱状图颜色对应

有小伙伴发来图,想让实现一下,是一个KEGG分析的柱状图,图的特点是文字标签颜色与柱子颜色一样,其实这个图也就这么一个特点,其他的柱状图的特征都没有什么,很普通的ggplot做法,最终效果如下:

image.png

首先读入数据,示例数据使用的是之前的文章:复现《nature communications》图表(四):ggplot画多组富集气泡图


setwd("E:/")
A <- read.csv("GO.csv", header = T)
library(ggplot2)
library(forcats)
A$Description <- as.factor(A$Description)
A$Description <- fct_inorder(A$Description)

先做一个普通的柱状图。只需要两个参数,一个是gene count,一个是富集的GO或者KEGG terms。

ggplot(A)+
  geom_bar(aes(Description, Count),stat = "identity")+
  coord_flip()

image.png

我们先不急着实现文字标签,先进行ggplot基础的修饰。


ggplot(A,aes(Description, Count))+
  geom_bar(aes(fill=Cluster),stat = "identity")+
  geom_text(aes(label=Count, y=Count+5),size=3)+
  coord_flip()+
  labs(x='',y='Gene count', title = 'GO enrichment of cluster')+
  scale_fill_manual(values = c('#852f88',
                               '#eb990c',
                               '#0f8096'))+
  theme_bw()+
  theme(panel.grid = element_blank(),
        legend.position = 'none',
        axis.ticks.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 10))

image.png

接下来就是最后一步了,将文字标签颜色设置为和柱子一样就完成了。我这里是分为了3组,将三组标签赋予对应的颜色即可。


table(A$Cluster)
#Cluster1 Cluster2 Cluster3 
#13       12       13 
col <- c(rep("#852f88",13),rep("#eb990c",12),rep("#0f8096",13))

ggplot(A,aes(Description, Count))+
  geom_bar(aes(fill=Cluster),stat = "identity")+
  geom_text(aes(label=Count, y=Count+5),size=3)+
  coord_flip()+
  labs(x='',y='Gene count', title = 'GO enrichment of cluster')+
  scale_fill_manual(values = c('#852f88',
                               '#eb990c',
                               '#0f8096'))+
  theme_bw()+
  theme(panel.grid = element_blank(),
        legend.position = 'none',
        axis.ticks.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 10),
        axis.text.y = element_text(size=rel(0.85),colour =col),
        plot.margin=unit(x=c(top.mar=0.2,right.mar=0.2,
                             bottom.mar=0.2,left.mar=0.2),
                         units="inches"))

image.png

还不错,其他的细节问题这里就不再赘述了,自行调整吧!

更多精彩内容请关注我的公众号=------《KS科研分享与服务》----------------------------

  • 2
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值