Start: Histogramm

Um eine einzelne kontinuierliche Variable darzustellen, nutzen wir Histogramme. Damit kann die Verteilung der Variable interpretiert werden. Für ein Histogramm nutzen wir die Funktion geom_hist().

hist <- ggplot(
  pss, 
  aes(agea)
) + 
  geom_histogram()  

hist
## Warning: Removed 157 rows containing non-finite outside the scale range
## (`stat_bin()`).

Nun können wir die Grafik etwas anpassen wie zuvor. Beginne mit den Titeln:

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()`).

Oftmals möchten wir die Verteilung interpretieren. Deshalb zeigen wir uns die Verteilung der Dichte zusätzlich an. Dazu fügen wir die Funktion geom_density() hinzu. Zusätzlich musst du die Achse in geom_histogram() auf ..density.. ändern!

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()`).

Auch hier können wir gruppieren!