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:
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.