One color for all groups
It sounds like you want both density plots to be red. In that case, you can use group
instead of fill
within aes
to indicate the grouping variable, and then set the fill
color for the whole graph, as follows:
m <- m + geom_density(aes(
group=factor(type)), fill="#ff0000"
, size=2, alpha=.4)
Specific colors for each group
But if you're actually trying to set specific colors for each group, then you can use scale_fill_manual
. There are two ways to do this: either with a named vector of colors, or with two vectors--one with colors, and one with group names. Here it is from the docs:
values
a set of aesthetic values to map data values to. If this is a named vector, then the values will be matched based on the names. If unnamed, values will be matched in order (usually alphabetical) with the limits of the scale. Any data values that don't match will be givenna.value
.
1. Using a Named Vector
groupColors <- c(a="#00ff00", b="#ff0000", c="#0000ff")
m <- ggplot(data, aes(x=lr))
m <- m + geom_density(aes(fill=factor(type)), size=2, alpha=.4) +
scale_fill_manual(values=groupColors)
m2 <- ggplot(data2, aes(x=lr))
m2 <- m2 + geom_density(aes(fill=factor(type)), size=2, alpha=.4) +
scale_fill_manual(values=groupColors)
2. Using Two Vectors
groupLimits <- c("a", "b", "c")
groupColors <- c("#00ff00", "#ff0000", "#0000ff")
m <- ggplot(data, aes(x=lr))
m <- m + geom_density(aes(fill=factor(type)), size=2, alpha=.4) +
scale_fill_manual(values=groupColors, limits=groupLimits)
m2 <- ggplot(data2, aes(x=lr))
m2 <- m2 + geom_density(aes(fill=factor(type)), size=2, alpha=.4) +
scale_fill_manual(values=groupColors, limits=groupLimits)
One key difference in this approach is that groupLimits
will control the order in which the different groups appear in the legend (and, in this case, will also force all three groups to appear).