ggplot2: Plotting two or more overlapping density plots on the same graph
March 16, 2009
One R Tip A Day uses a custom R function to plot two or more overlapping density plots on the same graph.
The same can be very easily accomplished in ggplot2.
> library(ggplot2) |
Create dummy dataframe:
> df <- data.frame(x = rnorm(1000, 0, 1), y = rnorm(1000,
0, 2), z = rnorm(1000, 2, 1.5))
|
“Melt” data:
> df.m <- melt(df) |
Default plot:
> ggplot(df.m) + geom_freqpoly(aes(x = value,
y = ..density.., colour = variable))
|
Default plot + few formatting adjustments:
> ggplot(df.m) + geom_freqpoly(aes(x = value,
y = ..density.., colour = variable)) +
labs(x = NULL) + opts(legend.position = "none") +
opts(title = "Frequency Polygons (based on binned counts)")
|
Update: Hadley kindly points out that the above plots are frequency polygons. I have updated the post with a “real” density plot.
> ggplot(df.m) + geom_density(aes(x = value,
colour = variable)) + labs(x = NULL) +
opts(legend.position = "none") + opts(title = "Densities from a kernel density estimator")
|
15 Comments
leave one →
Those are frequency polygons (based on binned counts) rather than densities from a kernel density estimator (as from geom_density).
Nice one; is there any chance of making the lines slightly less ‘jaggedy’; perhaps it’s a question of dpi in ggsave?
James, at the moment I am using dpi=72, will have a look whether increasing it will make a difference.
I wouldn’t think chaning the dpi would help much – it looks like the plots aren’t being correctly anti-aliased (unlike the base graphics plot). How are you saving them?
I am using ggsave:
ggsave(“density2.png”, dpi=72)
R 2.8.0 on WinXP.
Hmmm, and how did you save the base graphics plot?
Actually, I didn’t save it myself.
I got it from http://onertipaday.blogspot.com/2007/09/plotting-two-or-more-overlapping.html
Ah, ok. I don’t follow R windows development that closely, but it’s possible you may get somewhat better graphics by upgrading to the latest version of R may improve things (the default appearance is much better on mac and linux). Otherwise you might need to save as pdf and then use another graphics program to convert to png.
I agree width Hadley using the basic code below, under Mac OS X and Linux, the png output is ‘correctly’ anti-aliased and it looks much better:
library(“ggplot2”)
png(“densities_plots.png”)
ggplot(df.m) + geom_density(aes(x=value, y=..density.. ,colour=variable)) + labs(x=NULL) + opts(legend.position=”none”) + opts(title = “Densities from a kernel density estimator”)
dev.off()
is it possible to get rid of the extra line on x-axis
How can you make the density lines thicker?
By adjusting the line size attribute:
ggplot(df.m) + geom_density(aes(x=value, colour=variable), size = 2)ggplot(df.m) + geom_density(aes(x=value, colour=variable), size = 3)Thanks, that’s what I want — that was a combination I hadn’t tried.
In my version (R 2.15.2) I ve to include ‘library(reshape)’ before using melt
That’s true – ggplot2 does not load
reshapeby default any more.