Use of scale_fill_manual values with geom_stratum
Closed this issue · 2 comments
Description of the issue
Up until last week, I was able to use scale_fill_manual to apply color values to both of the geom_stratum. Now the color values only apply to the first geom_stratum
Reproducible example
Data
library(tidyverse)
library(ggalluvial)
trendCombined <- data.frame(
stringsAsFactors = FALSE,
profBOY = c("At or Above","At or Above",
"Below or Well Below","Below or Well Below"),
profEOY = c("At or Above",
"Below or Well Below","At or Above","Below or Well Below"),
EndYear = c(2019L, 2019L, 2019L, 2019L),
sum = c(13461L, 924L, 2921L, 3764L),
SchoolName = c("District", "District", "District", "District"),
CDESchoolNumber = c(9998L, 9998L, 9998L, 9998L),
totaln = c(24503L, 24503L, 24503L, 24503L),
totalNBoy = c(22764L, 22764L, 22764L, 22764L),
totalInBoyPB = c(15274, 15274, 7490, 7490),
percentInBoyPB = c("67%", "67%", "33%", "33%"),
totalNEoy = c(22703L, 22703L, 22703L, 22703L),
totalInEoyPB = c(17381, 5322, 17381, 5322),
percentInEoyPB = c("77%", "23%", "77%", "23%"),
percent = c("55%", "4%", "12%", "15%")
)
Plot
ggplot(trendCombined,
aes(y = sum,
axis1 = str_wrap(profBOY, 10),
axis2 = str_wrap(profEOY, 10),
fill = profBOY
)) +
geom_flow(color = '#e57a3c', curve_type = 'quintic') +
scale_x_discrete(limits = c("Beginning \nof Year", "End \nof Year")) +
scale_fill_manual(values = c("#315683","#6c7070")) +
geom_stratum(aes(fill = profEOY), color = 'grey', width = 2/3) +
geom_stratum(aes(fill = profBOY), color = 'grey', width = 2/3) +
geom_text(stat = 'stratum', aes(label = paste0(percentInBoyPB, '%')), vjust = 1, size = 6, color = 'white')+
geom_text(stat = 'stratum', aes(label = paste0('n = ', totalInBoyPB)), vjust = -.7, size = 5, color = 'white')+
geom_text(stat = 'stratum', aes(label = paste0(percentInEoyPB, '%')), vjust = 1, size = 6, color = 'white')+
geom_text(stat = 'stratum', aes(label = paste0('n = ', totalInEoyPB)), vjust = -.3, size = 5, color = 'white')+
labs(fill = 'Performance Level')+
facet_wrap(vars(factor(EndYear)),
nrow = 1,
scales = 'free_y')+
theme_minimal()+
theme(axis.text.y = element_blank(),
axis.title.y = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank(),
legend.position = 'top',
strip.text = element_text(size = 18),
strip.background = element_rect(fill = 'lightgrey', color = 'lightgrey')
)
Can you support my understand of may have changed with ggalluvial or ggplot to result in this difference?
Thanks so much!
Hi @Spswitzer, thanks for raising the issue. I'm not sure why the issue would have arisen with a recent release, but if it was not an issue before then this was an oversight on my part!
The package is not designed for multiple stratum layers in a single plot, and what's happening here is that the grey default color of missing strata is being superimposed on the left axis (geom_stratum(aes(fill = profBOY), color = 'grey', width = 2/3)
) after the colored strata have already been rendered on that axis (geom_stratum(aes(fill = profEOY), color = 'grey', width = 2/3)
). To see this, try commenting out one, and then the other, but not both, of those lines.
Usually, if the strata are to be colored, then the data should be put into long form rather than wide form. See this page of the documentation for details on how to do that.
However, in this case there is a way to get what you want while keeping the data in wide form: Set na.value = NA
in scale_fill_manual()
, which will tell the builder to make the strata at other axes transparent. Since you're generating the strata one axis at a time, this will prevent the missingness values from other axes from obscuring the strata at a given axis. Please let me know if that does or does not work for you!
Thank you so much for your quick and informative response. Our community appreciates the plots that I am able to create with your package. The impact of your work knows no end!! Kudos to you!