1

I'm trying to create a stacked bar chart, but I've run into an issue where ggplot2 automatically stacks the values in my chart based on magnitude rather than keeping the stack order the same between bars.

Dataset: https://drive.google.com/file/d/1lriQafJRtGrNC6OD2FnHx9Rt-BT9ReVm/view?usp=sharing

Code:

library(viridis)
library(ggplot2)
ggplot(rank_df, 
  aes(y = Rank, x = Kinase, fill = Database)) +
  geom_bar(stat="identity") +
  coord_flip() + theme_minimal() + 
  scale_fill_viridis(discrete = T, option = "turbo") 

enter image description here

lsoch
  • 17
  • 2

1 Answers1

0

Check out this page for some help How to control ordering of stacked bar chart using identity on ggplot2

In short, you might need to change the order of levels by using a relevel function.

amanwebb
  • 380
  • 2
  • 9
  • I reordered the levels but that didn't fix the problem. – lsoch Aug 31 '22 at 04:59
  • The problem seems to be that no matter what, the stacking order is determined by the size of 'Rank', i.e. whatever Database has the highest rank for each Kinase is stacked first. I've added a picture to the original post showing what the output looks like. – lsoch Aug 31 '22 at 04:59