I'm plotting the results of a state assessment of student learning. I have a stacked bar plot that shows the percent of students categorized into the test's 5 different proficiency levels-- c("Did Not Yet Meet", "Partially Met", "Approached", "Met","Exceeded" ). Percents of the three categories that did not meet the benchmark ("Met") are computed as negative percents so that the following plot is displayed.
All is well when plotting as a dodged bar, but when when they are stacked, the "Exceeded" category is plotted before the "Met" category. The legend displays in the correct order, but the plot does not. ggplot seems to be ordering the bar with negative values in the reverse order of those with positive values.
I have tried the kludge of mis-ordering my factor (setting factor 5 level as preceding factor 4 level) to get the bars to stack in the proper sequence. Of course, that works, but it puts my legend out of order.
This will be dynamically generated in shiny (it's a tiny part of a very large app) with options for stacking/dodging, slicing data with facets and filters, etc., so manually fabricating a legend will make for messy code.
Anyone know of a way to bypass ggplot's need to reverse stacking order when moving from negative to positive values? ...or of any way to make this work? Many thanks.
Here is a reprex
library(tidyverse)
create datafile ---------------------------------------------------------
ProficiencyLevel <- c("Did Not Yet Meet", "Partially Met", "Approached", "Did Not Yet Meet", "Partially Met", "Approached", "Did Not Yet Meet", "Partially Met", "Approached", "Did Not Yet Meet", "Partially Met", "Approached", "Met", "Exceeded", "Met", "Exceeded", "Met", "Exceeded" , "Met", "Exceeded" )
categoryPercent <- c(-0.10466272, -0.15630047, -0.25162533, -0.10447221, -0.16393284, -0.26625820, -0.09994016, -0.15219629, -0.25163375, -0.09522626, -0.14517377, -0.24564893, 0.38606153, 0.10134995, 0.38090988, 0.08442686, 0.39767804, 0.09855177, 0.40330958, 0.11064147)
EndYear <- c(2015, 2015, 2015, 2016, 2016, 2016, 2017, 2017, 2017, 2018, 2018, 2018, 2015, 2015, 2016, 2016, 2017, 2017, 2018, 2018)
testSummary <- tibble(ProficiencyLevel = ProficiencyLevel,
percentUpDn = categoryPercent,
EndYear = EndYear) %>%
mutate(ProficiencyLevel = factor(ProficiencyLevel,
levels = c("Did Not Yet Meet", "Partially Met", "Approached", "Met", "Exceeded"),
labels = c("1.Did Not Yet Meet", "2.Partially Met", "3.Approached", "4.Met", "5.Exceeded")))
Locking colors to factor values -----------------------------------------
colors5prof <- c("#C55859", "#ff7f0e", "#E7CD6A", "#2ca02c", "#1f77b4" )
names(colors5prof) <- c("1.Did Not Yet Meet", "2.Partially Met", "3.Approached", "4.Met", "5.Exceeded")
Plotting ----------------------------------------------------------------
ggplot()+
geom_bar(data = testSummary, aes(y=percentUpDn, x=EndYear, fill=ProficiencyLevel), stat="identity", alpha =.8)+
labs(title = "CMAS ELA: District",
y= "Proficiency Level",
x = "End Year")+
theme_minimal()+
theme(axis.ticks.y = element_line(size =1),
panel.grid = element_blank(),
plot.title = element_text(size = 25, color = "darkgrey"),
axis.title = element_text(size = 18, color = "darkgrey"),
legend.position = "bottom")+
scale_y_continuous(breaks = c(-1, 0, 1))+
geom_hline(yintercept=0)+
scale_fill_manual(values = colors5prof)+
coord_flip()