Home > Net >  ggplot2 barchart grouped by two variables and then stacked by two more
ggplot2 barchart grouped by two variables and then stacked by two more

Time:07-28

I just can't quite get this plot working. I have one more variable than the examples I can find on here. I have a dataset like so:

df <- data.frame(
   Type=sample(c('A', 'B', 'C', 'D'), 30, T),
   Geno=rep(LETTERS[1:3], 10), 
   Count=sample(1:5, 30, T), 
   subcount=sample(1:2, 30, T))

And I want to make a barchart where each Type is on the X axis and Count is one the y, but then in addition to the x axis being grouped by Type, I also want it grouped by Geno. Then I want subcount to be stacked on top of Count. So that the color of each count and subcount is specified by Geno but they are not the same color.

I can get it stacked by count and subcount and grouped by Type but not also geno, or I can get it grouped by geno and Type but not stacked by subcount..... Grouped but not stacked:

library(ggplot2)
ggplot(df, aes(y=Count, x=Type, fill=Geno)) 
    geom_bar(stat="identity", position="dodge")

An attempt where its stacked but not grouped:

ggplot(df, aes(fill=subcount, y=Count, x=Type)) 
    geom_bar(position="stack", stat="identity")

Thanks in advance for any help. Here is a small example of what I'm looking for (that should be geno 3 not a second geno 2 fyi):

enter image description here

CodePudding user response:

library(tidyverse); library(ggplot2)
df %>% 
  pivot_longer(c(Count, subcount)) %>% # Reshape your dataframe to get one column of factor with levels "Count", "subcount"
  group_by(Type, Geno) %>%             # Compute the sum of 'Count' and 'subcount' value
  mutate(cum_tot = cumsum(value)) %>% 
  ggplot(aes(Type, cum_tot, fill = Geno))   
  geom_col(data = . %>% filter(name=="Count"), position = position_dodge(width = 0.9), alpha = 1)  
  geom_col(data = . %>% filter(name=="subcount"), position = position_dodge(width = 0.9), alpha = 0.4)  
  geom_tile(aes(y=NA_integer_, alpha = factor(name)))

enter image description here

Inspired by this answer

As you can see the legend is not perfect regarding the Count/subcount variable, but you can try to play with the alpha argument of geom_tile

  • Related