Search code examples
rggplot2geom-bar

How can I get ggplot2 to display the counts in my flipped bar plot?


I have the following example dataset:

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

I added the row column to make unique rows in response to an error I got about row names required to be unique. I converted zeros to NA in order to get my color scale to only show valid values (in this case 90-100).

I am using this code to create the plot:

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())

This produces a plot with all bars being the same length: enter image description here

I want my bars to reflect the actual counts from the Count column in my data so they would be of variable length.

I have tried removing stat = 'identity' however that gives me this error message Error: stat_count() can only have an x or y aesthetic. I also tried removing the y = Count but then I get an error that I need a y aesthetic.

How can I get my plot to display bar lengths that reflect the Count value?


Solution

  • Not very sure what is your intended plot, and in this case, every gene per facet has only one value, so there's no need to tweak position.

    So maybe like this:

    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