I'm trying to make a diverging stacked bar chart as described in this article
My data is here, three columns. variable
is the tag for the question that was asked. value
is the response on a Likert scale. n
is the count, already made positive or negative depending on which side of the scale it's supposed to appear on. I have two neutral categories to make neutral be centered about 0.
final = structure(list(variable = c("Optimism", "Optimism", "Optimism",
"Market Insight", "Market Insight", "Market Insight", "Market Insight",
"Courage", "Courage", "Courage", "Courage", "Adaptability", "Adaptability",
"Adaptability", "Adaptability", "Bias for Action", "Bias for Action",
"Bias for Action", "Optimism", "Optimism", "Market Insight",
"Market Insight", "Courage", "Courage", "Adaptability", "Adaptability",
"Bias for Action", "Bias for Action"), value = structure(c(6L,
5L, 1L, 6L, 5L, 1L, 2L, 6L, 5L, 1L, 2L, 6L, 5L, 1L, 2L, 6L, 5L,
1L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L), .Label = c("Strongly Disagree",
"Disagree", "Disagree Neutral", "Agree Neutral", "Agree", "Strongly Agree"
), class = c("ordered", "factor")), n = c(36, 110, -1, 22, 84,
-1, -3, 35, 107, -1, -3, 53, 103, -1, -2, 42, 100, -1, 15, -14,
30, -29, 16, -15, 10, -10, 16, -16)), .Names = c("variable",
"value", "n"), row.names = c(NA, -28L), class = "data.frame")
Note that value
is an ordered factor already, the order is correct, moving from Strongly Disagree to Strongly Agree.
class(final$value)
#> [1] "ordered" "factor"
levels(final$value)
#> [1] "Strongly Disagree" "Disagree" "Disagree Neutral" "Agree Neutral"
#> [5] "Agree" "Strongly Agree"
I make the ggplot as such
ggplot(data = final, aes(y = n, x = variable, fill = value)) +
geom_col() +
coord_flip() +
scale_fill_manual(values = c("Strongly Disagree" = "#CA0020",
"Disagree" = "#F4A582",
"Disagree Neutral" = "#C0C0C0",
"Agree Neutral" = "#C0C0C0",
"Agree" = "#92C5DE",
"Strongly Agree" = "#0571B0"))
In the generated plot the legend is sorted correctly, the negatives are sorted correctly, but the positives are sorted in the wrong order. What is causing the mismatch in sorting order and how do I fix it?