To represent a single continuous variable, we use histograms. This allows us to interpret the distribution of the variable. To create a histogram, we use the geom_hist()
function.
hist <- ggplot(
pss,
aes(agea)
) +
geom_histogram()
hist
## Warning: Removed 157 rows containing non-finite outside the scale range
## (`stat_bin()`).
Now, we can customize the graph as before. Start with the titles:
hist <- ggplot(
pss,
aes(agea)
) +
geom_histogram(
color = "lightgray",
fill = "gray"
) +
labs(
x = "Age in years",
y = "Frequency",
title = "Histogram of Age (PSS)"
)
hist
## Warning: Removed 157 rows containing non-finite outside the scale range
## (`stat_bin()`).
Often, we want to interpret the distribution. Therefore, we also display the density distribution. To do this, we add the geom_density()
function. Additionally, you need to change the axis in geom_histogram()
to ..density..
!
histDens <- ggplot(
pss,
aes(agea)
) +
geom_histogram(
aes(y = ..density..),
color = "lightgray",
fill = "gray"
) +
geom_density(
alpha = 0.2,
fill = "lightblue"
) +
labs(
x = "Age in years",
y = "Density",
title = "Histogram of Age (PSS)"
)
histDens
## Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(density)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Removed 157 rows containing non-finite outside the scale range
## (`stat_bin()`).
## Warning: Removed 157 rows containing non-finite outside the scale range
## (`stat_density()`).
Here, too, we can group!