Learn R – How to Create Multiple Density Plots Using GGPlot
Learn to create multiple density curves or plots using ggplot2 package in R programming language.
Join the DZone community and get the full member experience.
Join For Freethis article represents code samples which could be used to create multiple density curves or plots using ggplot2 package in r programming language. please feel free to comment/suggest if i missed one or more important points.
multiple density curves/graphs with ggplot
the code samples given below work for the “diamonds” dataset which is loaded as part of ggplot2 package . the following are two different types of plots shown below:
- density plot with multiple fills
- density plot with single fill
density plots with multiple fills
the following code represents density plots with multiple fills. pay attention to the “fill” parameter passed to “aes” method.
# create density plots for single variable filtered by fill condition
# in example below, fill is assigned to cut
ggplot(diamonds, aes(x=carat, fill=cut)) + geom_density() +
labs(title="density plot", x="carat")
density plot with single fill
following plot demonstrates the density plot with single fill.
# create density plot for single variable
ggplot(diamonds, aes(x=carat)) + geom_density(col="red", fill="yellow", alpha=0.4) +
labs(title="density plot", x="carat")
Opinions expressed by DZone contributors are their own.
Comments