I hope that this example will be clear. I would like to have stacked bars where the middle bar spans '0', for it represents a neutral value. This is used with a Likert scale. For reproducibility am using the diamonds data set.
The following example is close enough to my use case and demonstrates the difficulty I'm having getting the "good" or "positive" data to be in the correct order (so that neutral is closest to 0).
Here is my code:
require(tidyverse)
diamonds_new <- diamonds %>%
mutate(quality = fct_recode(cut, "Very poor" = "Fair", "Poor" = "Good", "Neutral" = "Very Good", "Good" = "Premium", "Excellent" = "Ideal")) %>%
select(color, clarity, quality) %>%
group_by(color, clarity, quality) %>% count()
diamonds_bad <-
diamonds_new %>% filter(quality %in% c("Very poor", "Poor", "Neutral")) %>%
mutate(n = ifelse(quality == "Neutral", -n/2, -n))
diamonds_good <-
diamonds_new %>% filter(quality %in% c("Neutral", "Good", "Excellent")) %>%
mutate(n = ifelse(quality == "Neutral", n/2, n)) # %>%
# arrange(color, clarity, desc(quality)) # this doesn't seem to make a difference
ggplot() + geom_col(data = diamonds_bad, aes(x=color, y = n, fill = quality)) +
geom_col(data = diamonds_good, aes(x=color, y = n, fill = quality)) +
facet_grid(. ~ clarity, scales = "free") +
coord_flip()
I have also tried using
scale_fill_manual()
but haven't found a way for that to work, either.
I believe that this is more complicated than some existing examples that don't have the complication of negative values or of needing to span 0
. Using the current version of ggplot, what am I missing?
Also, am I correct that the positive and negative set need to be split or, at least, that it is easier to do so?