如何获取ggplot2以在翻转的条形图中显示计数?

问题描述 投票:1回答:1

我有以下示例数据集:

row gene    Type    PercID  Count
1   AAC     MS  0   0
71  AAC     NDA 99.66   17
2   ABC superfamily ATP binding cassette transporter    MS  0   0
72  ABC superfamily ATP binding cassette transporter    NDA 98.5    7
3   acetyltransferase   MS  0   0
73  acetyltransferase   NDA 100 12
4   AcrA    MS  94.6    6
74  AcrA    NDA 0   0
5   AcrB    MS  96  11
75  AcrB    NDA 0   0

我添加了行列,以使唯一的行成为对我所要求的关于唯一的行名的错误的响应。我将零转换为NA,以使我的色标仅显示有效值(在这种情况下为90-100)。

我正在使用此代码创建剧情:

library(ggplot2)
library(viridis)

mydata = read.csv("Mydata.csv", quote = "", fileEncoding = "UTF-8-BOM")

mydata2 = mydata
mydata2[, 4:5][mydata2[, 4:5] == 0] <- NA

ggplot(mydata2, aes(x = gene, y = Count, fill = PercID))+
geom_bar(position = position_fill(reverse = TRUE), stat = 'identity')+
facet_wrap(~ Type)+
coord_flip()+
scale_x_discrete(limits = rev(levels(mydata2$gene)))+
scale_fill_viridis(discrete = FALSE, name = "Percent Identity", option = 'plasma')+
theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank())

这会产生一个图,所有条形都相同长度:enter image description here

我希望我的柱线能反映数据中Count列的实际计数,因此它们的长度可变。

我已经尝试删除stat = 'identity',但是这给了我这个错误消息Error: stat_count() can only have an x or y aesthetic.,我也尝试删除了y = Count,但是随后出现一个错误,我需要一个美感。

如何获取绘图以显示反映Count值的钢筋长度?

r ggplot2 geom-bar
1个回答
1
投票

不太清楚您想要的图是什么,在这种情况下,每个方面的每个基因只有一个值,因此无需调整位置。

所以也许像这样:

ggplot(mydata2, aes(x = gene, y = Count, fill = PercID))+
geom_bar(stat = 'identity')+
facet_wrap(~ Type)+
coord_flip()+
scale_x_discrete(limits = rev(levels(mydata2$gene)))+
scale_fill_viridis(discrete = FALSE, name = "Percent Identity", option = 'plasma')+
theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank())

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.