Forcing the color of density plots in ggplot2

density-plot, ggplot2, r

Solution

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 given `na.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).

Problem

I've got two overlapping density functions I created as follows: ``` require(ggplot2) set.seed(2) a =rnorm(100) b = rnorm(100,.5,1.2) c = rnorm(100,.3,1.2) data <- rbind( data.frame(type="a", lr=a), data.frame(type="b", lr=b)) data2 <- rbind( data.frame(type="b", lr=b), data.frame(type="c", lr=c)) m <- ggplot(data, aes(x=lr)) m <- m + geom_density(aes(fill=factor(type)), size=2, alpha=.4) m2 <- ggplot(data2, aes(x=lr)) m2 <- m2 + geom_density(aes(fill=factor(type)), size=2, alpha=.4) ``` which produces these two nice plots: ``` m ``` ``` m2 ``` My problem is that ggplot alphabetically decides the red and blue colours and as you see in first one "b" is blue but in the second one "b" is red. I don't know how to force the "b" to be red. How can I select the colours for each of them?

Original source