Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlapped density plots in ggplot2

Tags:

r

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.

like image 993
rmflight Avatar asked Aug 30 '12 15:08

rmflight


People also ask

What is density in ggplot2?

A density plot is a representation of the distribution of a numeric variable. It is a smoothed version of the histogram and is used in the same kind of situation. Here is a basic example built with the ggplot2 library. Density Section Density theory. Density plots are built in ggplot2 thanks to the geom_density geom.

How to visualize multiple density plots in R using Ggplot2?

A density plot is a useful way to visualize the distribution of values in a dataset. Often you may want to visualize the density plots of several variables at once. Fortunately, this is easy to do using the ggplot2 data visualization package in R with the following syntax: The alpha argument controls the opacity of each density plot.

How to add a normal density curve to a ggplot2 histogram?

However, there’s still no normal density line in the plot… We can add such a normal density curve to our plot using the stat_function command as shown below: After running the previous R syntax the ggplot2 histogram with normal density curve shown in Figure 3 has been drawn. Looks great!

How do I create a density plot of multiple variables?

A density plot is a useful way to visualize the distribution of values in a dataset. Often you may want to visualize the density plots of several variables at once. Fortunately, this is easy to do using the ggplot2 data visualization package in R with the following syntax: ggplot (data, aes(x=value, fill=variable)) + geom_density (alpha=.25)

How many rows and variables does the ggplot2 plot contain?

It shows that our example data is composed of 300 rows and two variables. The variable “value” contains the data points and the variable “group” consists of the corresponding groups. To be able to use the functions of the ggplot2 package, we also need to install and load ggplot2:


2 Answers

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

like image 107
Sven Hohenstein Avatar answered Oct 30 '22 10:10

Sven Hohenstein


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

like image 39
Sandipan Dey Avatar answered Oct 30 '22 12:10

Sandipan Dey