Gruppierte Scatterplots

Auch Scatterplots können wir nach einer Gruppenvariable teilen. Hier nutzen wir wieder die Variable der Distrikte vom Beginn des Lernmoduls:

groupscatter <- ggplot(
  pss, 
  aes(
    trstplt, 
    trstlgl,
    color = district
  )
) +
  geom_point() +
  geom_jitter(
    width = 1, 
    height = 1
  ) +
  scale_color_manual(
    name = "Distrikte", 
    values = beyonce_palette(25)
  ) +
  scale_x_continuous(
    breaks = seq(
      0, 
      10,
      1
    )
  ) + 
  scale_y_continuous( 
    breaks = seq(
      0,
      10,
      1
    )
  ) + 
  labs(
    x = "Trust in Politicians", 
    y = "Trust in Legal System",
    title = "Group Scatterplot"
  ) 

groupscatter
## Warning: Removed 23 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Removed 23 rows containing missing values or values outside the scale range
## (`geom_point()`).

Leider ist hierauf nicht wirklich viel zu erkennen, da trotzdem alles innerhalb eines Plots dargestellt wird. Um das zu umgehen, können wir facets nutzen!

Facets

Wir können aber auch vier einzelne Plots in einem darstellen. Dazu nutzen wir die Funktion facet_grid() :

groupscatter +
  facet_grid(. ~ district)
## Warning: Removed 23 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Removed 23 rows containing missing values or values outside the scale range
## (`geom_point()`).

Ein weiterer Weg ist mit der Funktion facet_wrap() möglich. Dieser schafft sowohl in Spalten als auch Zeilen gleichmäßige Felder, in denen dann die einzelnen Plots dargestellt werden:

groupscatter + 
  facet_wrap(~district)
## Warning: Removed 23 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Removed 23 rows containing missing values or values outside the scale range
## (`geom_point()`).

Nun haben wir aber die Legende zweimal, daher wollen wir eine loswerden:

groupscatter2 <- ggplot(
  pss, 
  aes(
    trstplt, 
    trstlgl,
    color = district
  )
) +
  geom_point() +
  geom_jitter(
    width = 1, 
    height = 1
  ) +
  scale_color_manual(values = beyonce_palette(25)) +
  scale_x_continuous(
    breaks = seq(
      0,
      10,
      1
    )
  ) + 
  scale_y_continuous(
    breaks = seq(
      0,
      10,
      1
    )
  ) + 
  labs(
    x = "Trust in Politicians", 
    y = "Trust in Legal System",
    title = "Group Scatterplot"
  ) +
  facet_wrap(~ district) +
  theme(legend.position = "none")

groupscatter2
## Warning: Removed 23 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Removed 23 rows containing missing values or values outside the scale range
## (`geom_point()`).

Zuguterletzt können wir auch hier eine Regressionslinie hinzufügen:

groupscatter2 + 
  geom_smooth(
    method = "lm", 
    se = TRUE,
    color = "darkgray"
  )
## Warning: Removed 23 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 23 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Removed 23 rows containing missing values or values outside the scale range
## (`geom_point()`).

Gehen wir weiter zu einer anderen Art der Darstellung für pseudo-metrische Daten!