简体   繁体   中英

Overlapped density plots in ggplot2

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.

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)

在此输入图像描述

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')

在此输入图像描述

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")

在此输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM