I have data on a variable spanning multiple years.

I want to see how the distribution has evolved over the years.

  • Is there an easy way to produce a density plot for each year in single plot with different colours for each year?

2 Answers 2


Without seeing the data, I would suggest trying trellis displays. If you are using R, this is very easy to do with the lattice (even latticeExtra) or ggplot2 packages.

> my.df <- data.frame(x=rnorm(300), year=gl(3, 100, 300, labels=2000:2002))
> head(my.df)
           x year
1 -0.3260365 2000
2  0.5524619 2000
3 -0.6749438 2000
4  0.2143595 2000
5  0.3107692 2000
6  1.1739663 2000
> library(lattice)
> densityplot(~ x, data=my.df, groups=year)

which gives

enter image description here

Compare to densityplot(~ x | year, data=my.df, layout=c(3,1)) (for a facetted display).


I'll assume you are using R (from previous questions). Note that this also makes your question better suited for StackOverflow.com.

As such, I remembered an answer there that could be of service to you:Q6030684.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.