Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Creating Density Plots | Data Visualization
Data Analysis with R

bookCreating 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.

question mark

What argument controls the transparency of overlapping density plots?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 2.  4

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 2.  4
some-alt