Hi,
I am trying to order a stacked bar chart as shown in the example: Creating a barplot with ordered bars
I cant for the life of me get it to work. Could someone point out where I'm going wrong?
library(tidyverse)
region = sample(c('EU', 'AP', 'GU', 'PS', 'EL'), 20, replace = TRUE)
ctry = sample(c("wp", "vw", "ct", "vj", "yk", "uu", "xo", "av", "za", "zj", "lz", "vs", "fq"), 20, replace = TRUE)
items = sample(1:20)
mydf <- data.frame(region, ctry, items) %>%
mutate(ctry = factor(ctry, levels = unique(ctry)))
sorted_df_hard <- mydf %>%
count(ctry, region) %>%
arrange(ctry, -n) %>%
mutate(ctry = factor(ctry, levels = unique(ctry)))
# of course, this is even worse
ggplot(sorted_df_hard, aes(x = ctry, y = n, fill = region)) +
geom_bar(stat="identity") +
coord_flip()
The end result should have the ctry
on the x-axis and the y axis should be counts with the region being the colour. It should sort it highest to lowest number of items