Para representar uma única variável contínua, usamos Histogramas. Isso nos permite interpretar a distribuição da variável. Para um Histograma, usamos a função geom_hist()
.
hist <- ggplot(
pss,
aes(agea)
) +
geom_histogram()
hist
## Warning: Removed 157 rows containing non-finite outside the scale range
## (`stat_bin()`).
Agora podemos personalizar o gráfico um pouco como antes. Comece com os títulos:
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()`).
Muitas vezes queremos interpretar a distribuição. Portanto, vamos adicionar a visualização da densidade da distribuição. Para isso, adicionamos a função geom_density()
. Além disso, você precisa alterar o eixo em geom_histogram()
para ..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()`).
Também podemos agrupar aqui!