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.
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.
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.
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!
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)
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:
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With