I would like to make a geom_area plot where the legend shows more colors than there is data for. I’ve provided a made-up dataset below that has two treatments, and I want the legend to show colors for both of them as well as a third treatment. I have read that the “limits” argument of scale_fill_manual can be used to force the legend to have certain values, so I prepared a named vector of values to use in scale_fill_manual, where the named vector will be the “values” argument and the names of that vector will be the “limits” argument.
Using the “limits” argument has ensured that treatment “C” shows up on the legend, but its color is blank. How can I make the legend show the color red next to treatment “C” instead of gray?
The reason I want to do this is that I will be combining multiple plots with a shared legend, but I’m ignoring that complication for the example below. I realize that I can stick zero-value rows into my data frame for the unused treatment so that it is technically present, but not showing in the geom_area plot, but I would like to find a method that doesn’t involve modifying the data frame this way.
Note: I know that it would be really helpful to show a screenshot here, but StackOverflow wouldn’t accept my 50kb .PNG plot attachment or any screenshots, and I couldn’t figure out another way to share it. Hopefully this is very easy to reproduce.
Thank you!
Packages:
library(ggplot2)
Data:
df = data.frame(
treatment = rep(c("A", "B"), 3),
year = rep(c(2000, 2010, 2020), each=2),
measurement = c(0.5, 0.5, 0.6, 0.7, 0.6, 0.8)
)
Script:
legend.values = c("blue", "green", "red")
legend.limits = c("A", "B", "C")
names(legend.values) = legend.limits
plot = ggplot(data=df) + geom_area(aes(y=measurement, x=year, fill=treatment)) + scale_fill_manual(values=legend.values, limits=legend.limits)