Multiple Density Plots and Coloring by Variable with ggplot2 in R
Last Updated :
13 Jun, 2023
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 first make a data frame with values and categories. 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()
Multiple Density Plots with Coloring by Variable
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/colors.
R
# set seed
set.seed(1234)
# create dataframe
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 library ggplot2 package
library(ggplot2)
# Basic density plot with custom color
ggplot(df, aes(x=value, color=category)) +
# color property for changing color of plot
# geom_density() function plots the density plot
geom_density()
Output:
Density Plots in RColor Customization
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/colors. 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 library
library(ggplot2)
# Basic density plot with custom color
# color property to determine the color of plot
# fill property to determine the color beneath plot
ggplot(df, aes(x=value, color=category, fill=category)) +
geom_density(alpha=0.3)
Output:
Density Plots in RAdd legend and increase color transparency:
R
library(ggplot2)
# set seed
set.seed(1234)
# create dataframe
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))))
# Customize density plot
ggplot(df, aes(x = value, fill = category)) +
geom_density(alpha = 0.5, color = "black", size = 1) +
scale_fill_manual(values = c("category1" = "red", "category2" = "blue")) +
labs(x = "Value", y = "Density", title = "Density Plot by Category") +
theme_minimal()
Output:
Density Plots in RÂ Log scale customization
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 the 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 seed
set.seed(1234)
# create dataframe
df <- 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 ggplot2
library(ggplot2)
# Basic density plot
# color property is used to determine the color of plot
ggplot(df, aes(x=value, color=category)) +
geom_density()+
scale_x_log10()
Output:
Density Plots in R
Similar Reads
How to Add Vertical Lines By a Variable in Multiple Density Plots with ggplot2 in R In this article, we will discuss how to add vertical lines by a variable in multiple density plots with ggplot2 package in the R  Programming language. To do so first we will create multiple density plots colored by group and then add the line as a separate element. Basic Multiple Density Plot: To
3 min read
Coloring Points Based on Variable with R ggpairs This article will explain how to color points based on a variable using ggpairs() By adding color to the points in a pairwise plot based on a categorical or continuous variable, we can easily see how different categories or ranges of values behave across multiple pairwise relationships using R Progr
3 min read
Create a Scatter Plot with Multiple Groups using ggplot2 in R In this article, we will discuss how to create a scatter plot with multiple groups in R Programming Language. Geoms can be added to the plot to compute various graphical representations of the data in the plot (points, lines, bars). The geom_point() method is used to create scatter plots in R. The g
2 min read
How to Create a Scatterplot in R with Multiple Variables? In this article, we will be looking at the way to create a scatter plot with multiple variables in the R programming language. Â Using Plot() And Points() Function In Base R: In this approach to create a scatter plot with multiple variables, the user needs to call the plot() function Plot() function:
3 min read
How To Join Multiple ggplot2 Plots with cowplot? In this article, we are going to see how to join multiple ggplot2 plots with cowplot. To join multiple ggplot2 plots, we use the plot_grid() function of the cowplot package of R Language. Syntax: plot_grid(plot1,plot2,label=<label-vector>, ncol, nrow) Parameters: plot1 and plot2 are plots that
3 min read