Creating Density Plots
Why Use Density Plots?
A density plot is a smoothed version of a histogram. It is useful to:
- Understand the distribution of a numeric variable;
- Compare distributions across groups (like fuel types);
- Spot peaks, skewness, and spread.
Unlike histograms, density plots estimate the probability of a value occurring within a range.
Density Plot Syntax in ggplot2
In ggplot2, a density plot can be created using geom_density()
.
ggplot(data = df, aes(x = variable)) +
geom_density()
To compare groups, you can map a categorical variable to fill
and adjust the transparency with alpha
so the curves overlap clearly.
ggplot(data = df, aes(x = variable, fill = group_variable)) +
geom_density(alpha = 0.5)
This makes it easy to compare how the distribution of a numeric variable differs across categories.
Example: Selling Price Distribution
A density plot provides a smooth representation of how car prices are distributed. In this example, the curve is filled in blue, and axis labels clarify the meaning of the values.
ggplot(df, aes(x = selling_price)) +
geom_density(fill = "blue") +
labs(title = "Density Plot of Selling Prices",
x = "Selling Price",
y = "Density")
This visualization highlights where most car prices cluster, as well as how widely prices are spread across the dataset. It is particularly useful for identifying peaks in the distribution and comparing against other variables later.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 4
Creating Density Plots
Swipe to show menu
Why Use Density Plots?
A density plot is a smoothed version of a histogram. It is useful to:
- Understand the distribution of a numeric variable;
- Compare distributions across groups (like fuel types);
- Spot peaks, skewness, and spread.
Unlike histograms, density plots estimate the probability of a value occurring within a range.
Density Plot Syntax in ggplot2
In ggplot2, a density plot can be created using geom_density()
.
ggplot(data = df, aes(x = variable)) +
geom_density()
To compare groups, you can map a categorical variable to fill
and adjust the transparency with alpha
so the curves overlap clearly.
ggplot(data = df, aes(x = variable, fill = group_variable)) +
geom_density(alpha = 0.5)
This makes it easy to compare how the distribution of a numeric variable differs across categories.
Example: Selling Price Distribution
A density plot provides a smooth representation of how car prices are distributed. In this example, the curve is filled in blue, and axis labels clarify the meaning of the values.
ggplot(df, aes(x = selling_price)) +
geom_density(fill = "blue") +
labs(title = "Density Plot of Selling Prices",
x = "Selling Price",
y = "Density")
This visualization highlights where most car prices cluster, as well as how widely prices are spread across the dataset. It is particularly useful for identifying peaks in the distribution and comparing against other variables later.
Thanks for your feedback!