Creating multiple plots makes it necessary to have a common color scale.
That's easy by using scale_color_manual
. But it gets tricky if not all levels are present in every plot like with this example
doG = function(df) {
df$g = factor(df$g, levels = c("red_dot", "green_dot", "blue_dot"))
g = ggplot(df, aes(x = x, y = y, color = g)) + geom_point()
g + scale_color_manual(values = c("red", "green", "blue"))
}
df = data.frame(x = 1:3, y = c(1,1,1), g = c("green_dot", "blue_dot", "red_dot"))
doG(df)
looks perfectly fine but then
doG(df[1:2,])
destroys all the illusions.
Is there some smart way I can reuse the function doG
for subsets where not all factor levels are present?