I\'ve been asked to produce a stacked bar chart with bars and values that are stacked and ordered in a precise manner.
In this case \"A3\" on the left, \"A2\" in th
You already have plotOrder
, but you need to apply it to reorder x
rather than fill
...
stackedBarPlot <-
ggplot(data) +
aes(x = reorder(ValueName,-plotOrder),
y = Percent,
fill = Response) +
geom_bar(position = "fill", stat = "identity") +
coord_flip()
With a little pre-calculation of the order, this is not so hard:
library(magrittr)
library(dplyr)
o <- d %>% filter(Response == "A1") %>% arrange(Percent) %>% extract2("ValueName")
d %>%
mutate(ValueName = factor(ValueName, o)) %>%
ggplot() +
aes(x = ValueName, y = Percent, fill = reorder(Response, plotOrder)) +
geom_bar(position = "fill", stat = "identity") +
coord_flip()