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

手机游戏开发者 2024-10-1 12:07:59 53 0 来自 中国
有小搭档发来图,想让实现一下,是一个KEGG分析的柱状图,图的特点是笔墨标签颜色与柱子颜色一样,实在这个图也就这么一个特点,其他的柱状图的特性都没有什么,很平凡的ggplot做法,最闭幕果如下:
起首读入数据,示例数据使用的是之前的文章:复现《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()我们先不急着实现笔墨标签,先举行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))接下来就是最后一步了,将笔墨标签颜色设置为和柱子一样就完成了。我这里是分为了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")) 4.png 还不错,其他的细节题目这里就不再赘述了,自行调解吧!
更多出色内容请关注我的公众号=------《KS科研分享与服务》----------------------------
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-10-18 16:53, Processed in 0.194612 second(s), 35 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表