ggplot2 change fill for color legend when fill also used in aesthetic

For data-related aspects of the legend, theme() won't help you. Instead you need to use guides and override the fill for the color legend:

ggplot(dat)+
    geom_bar(aes(id, color=var1, fill=var2))+
    scale_color_manual(values=c("A"="black", "B"="grey"))+
    guides(color = guide_legend(override.aes = list(fill = "white"))) +
    theme_light()

The general rule is that theme() only controls the overall look of the plot, not the data-dependent parts like scales (maybe instead of "data-dependent" it should be "the parts of the plot that vary based on data").

Tags:

R

Ggplot2