1. Einführung

  • Was ist ggplot2?

    • ggplot2 ist ein R-Paket zur Erstellung von Grafiken. Es basiert auf der Grammar of Graphics, also der Idee, dass eine Grafik aus Daten, Ästhetik (Mapping) und Geometrien aufgebaut ist.
  • Warum ggplot2?

    • Grafiken sehen professionell aus.

    • Sehr flexibel und gut erweiterbar.

    • Ideal für Datenanalyse-Projekte.

2. Aufbau einer ggplot2-Grafik

ggplot(data = DATEN, aes(x = X, y = Y)) +
  geom_POINT() +
  labs(title="Titel", x="X-Achse", y="Y-Achse") +
  theme_minimal()

Erklärung:

  1. ggplot(data = DATEN, aes(x = X, y = Y)) : welche Daten und welche Variablen?

  2. geom_…() : Welche Art von Grafik (Punkte, Linien, Balken …)?

  3. labs() : Achsenbeschriftungen und Titel.

  4. theme_…() : Gestaltung der Grafik.

3. Wichtige Geometrien

  • geom_point() : - Streudiagramm Punkteplot von x vs y

  • geom_line() : Liniendiagramm Linie durch Datenpunkte

  • geom_bar() : Balkendiagramm Häufigkeiten anzeigen

  • geom_histogram() : Histogramm Verteilung einer Variablen

  • geom_boxplot() : Boxplot Quartile & Ausreisser

4. Ästhetik (aes)

  • Farben, Formen, Grösse können variabel gestaltet werden.
ggplot(mtcars, aes(x=wt, y=mpg, color=factor(cyl))) +
  geom_point(size=3)

  • Hier wird die Farbe nach der Anzahl Zylinder (cyl) unterschieden.

5. Gruppierung & Facetten

Facetten: kleine Multiples für verschiedene Gruppen.

ggplot(mtcars, aes(x=wt, y=mpg)) +
  geom_point() +
  facet_wrap(~cyl)  # Diagramme pro Zylinderanzahl

Beispiel

1 Daten

# Beispiel-Daten
daten <- data.frame(
  x = 1:5,
  y = c(3, 5, 2, 8, 7)
)

# ggplot verwenden
library(ggplot2)
ggplot(daten, aes(x = x, y = y)) +
  geom_point()

2. Farben und Grösse ändern

ggplot(daten, aes(x = x, y = y, color = y, size = y)) +
  geom_point() +
  labs(title = "Scatterplot mit Farbe und Grösse") +
  theme_classic()

3. Liniendiagramm

ggplot(daten, aes(x = x, y = y)) +
  geom_line(color = "blue", size = 1.2) +
  geom_point() +
  labs(title = "Liniendiagramm") +
  theme_light()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

4. Histogramm

ggplot(daten, aes(x = y)) +
  geom_histogram(binwidth = 1, fill = "skyblue", color = "black") +
  labs(title = "Histogramm der y-Werte") +
  theme_minimal()

5. Boxplot

# Beispiel mit Gruppen
daten2 <- data.frame(
  Gruppe = rep(c("A", "B", "C"), each = 5),
  Wert = c(3,4,2,5,6, 5,7,6,8,7, 4,5,6,3,4)
)

ggplot(daten2, aes(x = Gruppe, y = Wert, fill = Gruppe)) +
  geom_boxplot() +
  labs(title = "Boxplot nach Gruppen") +
  theme_minimal()

6. Facetten

ggplot(daten2, aes(x = Wert)) +
  geom_histogram(binwidth = 1, fill = "orange", color = "black") +
  facet_wrap(~Gruppe) +
  labs(title = "Histogramme pro Gruppe") +
  theme_light()

7. Kurze Zusammenfassung

  • geom_point() → Punkteplot

  • geom_line() → Linienplot

  • geom_histogram() → Histogramm

  • geom_boxplot() → Boxplot

  • facet_wrap(~Gruppe) → Plots für jede Gruppe

  • aes() → Mapping von Variablen auf Achsen, Farbe, Grösse

  • theme_…() → Optik der Grafik