Multiple Density Plots and Coloring by Variable with ggplot2 in R - GeeksforGeeks

05 Oct, 2021
In this article, we will discuss how to make multiple density plots with coloring by variable in R Programming Language.
To make multiple density plots with coloring by variable in R with ggplot2, we firstly make a data frame with values and category. Then we draw the ggplot2 density plot using the geom_desnity() function. To color them according to the variable we add the fill property as a category in ggplot() function.
Syntax: ggplot(dataFrame, aes( x, color, fill)) + geom_density()
We get multiple density plots in the ggplot of two colors corresponding to two-level/values for the second categorical variable. If our categorical variable has n levels, then ggplot2 would make multiple density plots with n densities/color.
R
# set seedset.seed(1234)  # create dataframedf <- data.frame( category=factor(rep(c("category1",                                         "category2"),                                       each=500)),                    value=round(c(rnorm(500, mean=95, sd=5),                                rnorm(500, mean=105, sd=7))))# load library ggplot2 packagelibrary(ggplot2)  # Basic density plot with custom colorggplot(df, aes(x=value, color=category)) +     # color property for changing color of plot  # geom_density() function plots the density plot  geom_density()
Output:
To add color beneath the density plot we use the fill property. We can also customize the transparency of the plot with the alpha property. We get multiple density plots in the ggplot filled with two colors corresponding to two-level/values for the second categorical variable. If our categorical variable has n levels, then ggplot2 would make multiple density plots with n densities/color. The alpha value of 0.3 gives 30% transparency to the plot fill.
R
set.seed(1234)df < - data.frame(    category=factor(rep(c("category1", "category2"), each=500)),    value=round(c(rnorm(500, mean=95, sd=5),                  rnorm(500, mean=105, sd=7))))# load librarylibrary(ggplot2)  # Basic density plot with custom color# color property to determine the color of plot# fill property to determine the color beneath plotggplot(df, aes(x=value, color=category, fill=category)) +geom_density(alpha=0.3)
Output:
When x or y-axis data is very large, we can draw plots with a log scale. In ggplot2, we can transform x-axis values to log scale using scale_x_log10() function.
Syntax: plot+ scale_x_log10()
Now our density plot is drawn in Log scale. This becomes more effective when our dataset is skew. It helps to correct the skewness of the plot.
R
# set seedset.seed(1234)  # create dataframedf < - data.frame(    category=factor(rep(c("category1", "category2"),                        each=5000)),    value=round(c(rnorm(5000, mean=90005, sd=50000),                  rnorm(5000, mean=70005, sd=70000))))  # load library ggplot2library(ggplot2)  # Basic density plot# color property is used to determine the color of plotggplot(df, aes(x=value, color=category)) +geom_density()+  # geom_density() creates density plotscale_x_log10()  # converts x-axis values in log scale
Output:
Picked
R-ggplot
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Change Color of Bars in Barchart using ggplot2 in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
Replace Specific Characters in String in R
How to filter R dataframe by multiple conditions?
R - if statement
How to import an Excel File into R ?
How to change the order of bars in bar chart in R ?