7

Imagine I have two vectors each of different length. I want to generate one plot with the density of both vectors overlaid. What I thought I should do is this:

vec1 <- data.frame(x=rnorm(2000, 0, 1))
vec2 <- data.frame(x=rnorm(3000, 1, 1.5))
ggplot() + geom_density(aes(x=x, colour="red"), data=vec1) + 
  geom_density(aes(x=x, colour="blue"), data=vec2)

Then I thought I should do this:

vec1 <- data.frame(x=rnorm(2000, 0, 1))
vec2 <- data.frame(y=rnorm(3000, 1, 1.5))
ggplot() + geom_density(aes(x=x, colour="red"), data=vec1) + 
  geom_density(aes(x=y, colour="blue"), data=vec2)

Neither of these quite work, because the colors get mixed up.

Based on another solution I found in StackOverflow 1 2, I realized I should try this:

vec1 <- data.frame(x=rnorm(2000, 0, 1), grp="vec1")
vec2 <- data.frame(x=rnorm(3000, 1, 1.5), grp="vec2")
allDat <- rbind(vec1, vec2)

ggplot(allDat, aes(x, colour=grp)) + geom_density()

ggplot(allDat, aes(x, colour=grp)) + geom_density() + 
  scale_colour_manual(values=c("green", "blue"))

ggplot(allDat, aes(x, colour=grp)) + geom_density() + 
  scale_colour_manual(values=c(vec2="green", vec1="blue"))

OK, I solved my original problem. But is there a way to do something akin to the first one I tried above? From the way things are worded in the ggplot documentation, I would have thought so. Appreciate any suggestions.

Community
  • 1
  • 1
rmflight
  • 1,871
  • 1
  • 14
  • 22
  • You could probably add `scale_colour_manual(values = c('blue','red'))` to your first attempt, but your last attempt is probably the more accepted route. – joran Aug 30 '12 at 15:28
  • Hmmm, and nope. that doesn't work either. – rmflight Aug 30 '12 at 15:39
  • You'll have to be more specific, because it works just fine for me. – joran Aug 30 '12 at 15:40
  • Using: ggplot() + geom_density(aes(x=x, colour="red"), data=vec1) + geom_density(aes(x=x, colour="blue"), data=vec2) + scale_colour_manual(values=c('red', 'blue')) Yes, it changes the colors. But, it assigns the wrong color to the wrong distribution. At least for me, the 0 centered dist. is blue, and the wider dist. w mean of 1 is red, when I am asking for the opposite. – rmflight Aug 30 '12 at 15:47
  • Um...so switch them in the two `geom_density` calls to reverse them back? I realize that's backwards and everything, but your first attempt just isn't how the logic of legends works in ggplot. You don't assign color labels to things in the geom. That's just not how it's done. – joran Aug 30 '12 at 15:52

3 Answers3

20

Everything will work fine if you move the assignment of the colour parameter out of aes().

vec1 <- data.frame(x=rnorm(2000, 0, 1))
vec2 <- data.frame(x=rnorm(3000, 1, 1.5))

library(ggplot2)

ggplot() + geom_density(aes(x=x), colour="red", data=vec1) + 
  geom_density(aes(x=x), colour="blue", data=vec2)

enter image description here

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
  • And that was the "little" twiggly thing that kept tripping me up. All the examples I found showed `colour` in `aes()`. Thank you, this might be useful when I have some really **big** vectors that I don't really want to concatenate into one data frame. – rmflight Aug 30 '12 at 18:53
4

Try this if you want have legends too:

df <- rbind(data.frame(x=rnorm(2000, 0, 1), vec='1'),
            data.frame(x=rnorm(3000, 1, 1.5), vec='2'))
ggplot(df, aes(x, group=vec, col=vec)) + geom_density(position='dodge')

enter image description here

Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63
0

I had some troubles with the the above solution, as my data was contained in a single data frame. Using ... data=df$A in the aesthetics doesn't work as this will provide ggplot with a vector of class "numeric", which isn't supported.

Therefor, to overlay different columns all contained in the same data frame, I'd suggest:

vec1 <- rnorm(3000, 0, 1)
vec2 <- rnorm(3000, 1, 1.5)

df <- data.frame(vec1, vec2)
colnames(df) <- c("A", "B")

library(ggplot2)

ggplot() + geom_density(aes(x=df$A), colour="red") + 
  geom_density(aes(x=df$B), colour="blue")

enter image description here

For most people this might seem obvious, but for me as a beginner, it wasn't. Hope this helps.

Peer Wünsche
  • 175
  • 3
  • 8