Lösungen vorhanden

Rmd

Intro

Oft müssen Daten zunächst zusammengefasst werden, um die Werte zu ermitteln, die dargestellt werden sollen. Auf diesen Aspekt konzentrieren sich die Darstellungen hier. Es geht also um das Erstellen von Grafiken aus Rohdaten. Die notwendigen Parameter, beispielsweise Mittelwerte oder Streuungen, werden direkt durch und in ggplot() ermittelt.

Durch die Kombination einer stat_...() Funktion in Kombination mit einem geom_...() werden die Rohdaten in die darzustellenden Daten umgewandelt (aggregiert).

Hier Beispiele mit stat_summary() als aggregierender Funktion. Sie versteht als Parameter eine aggregierende Funktion (hier “mean” und “mean_cl_normal” ) Die berechneten aggregierten Werte werden durch ein geom visualisiert (hier “point” oder “errorbar”), die stat_summary() als Parameter übergeben werden

Beispiel: Aggregieren innerhalb von Teilgruppen

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.3     ✓ purrr   0.3.4
## ✓ tibble  3.1.0     ✓ dplyr   1.0.5
## ✓ tidyr   1.1.3     ✓ stringr 1.4.0
## ✓ readr   1.4.0     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
dd <- readr::read_tsv("http://md.psych.bio.uni-goettingen.de/mv/data/virt/v_bmi_preprocessed.txt")
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   .default = col_double(),
##   name = col_character(),
##   gender = col_character(),
##   bmi_class = col_character(),
##   bmi_dichotom = col_character(),
##   bmi_normal = col_logical()
## )
## ℹ Use `spec()` for the full column specifications.
dd$bmi_class <- factor(dd$bmi_class, levels=c("low", "normal", "high", "obese"))
dd$bodysatisf <- 101 - dd$bodysatisf
# we want to plot bodysatisf for the groups
dd %>% ggplot(aes(x=bmi_class, y=bodysatisf, color=bmi_class)) + geom_point()

# if we want to see the mean for each group, we have to aggregate within the subgroups
dd %>% ggplot(aes(x=bmi_class, y=bodysatisf, color=bmi_class)) + stat_summary(fun=mean, geom="point", size=3)

# not too informative yet, how about error bars?
dd %>% ggplot(aes(x=bmi_class, y=bodysatisf, color=bmi_class)) +
  stat_summary(fun = mean, geom="point", size = 3) +
  stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width=0.2, size=0.5)

# we can even plot all the observations additionally
dd %>% ggplot(aes(x=bmi_class, y=bodysatisf, color=bmi_class)) +
  stat_summary(fun = mean, geom="point", size = 3) +
  stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width=0.2, size=0.5) +
  geom_point(position = "jitter", alpha=0.3)

# we might want to interconnect the aggregated means by a line, we have to group the subgroups of bmi_class
dd %>% ggplot(aes(x=bmi_class, y=bodysatisf, color=bmi_class)) +
  stat_summary(fun = mean, geom="point", size = 3) +
  stat_summary(fun = mean, geom="line", size = 0.2, group=1) +
  stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width=0.2, size=0.5) +
  geom_point(position = "jitter", alpha=0.3)

# we might even want to split by gender
dd %>% ggplot(aes(x=bmi_class, y=bodysatisf, color=bmi_class, group=gender, shape=gender)) +
  stat_summary(fun = mean, geom="point", size = 3) +
  stat_summary(fun = mean, geom="line", size = 0.2) +
  stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width=0.2, size=0.5) +
  geom_point(position = "jitter", alpha=0.3)

Beispiel: Aggregieren innerhalb von Teilgruppen über Zeitverlauf, mit wide -> long Conversion

Bei der Darstellung von Zeitverläufen oder Entwicklungen über aggregierte Daten pro Messzeitpunkt ist das Long-Format geeignet.

Hier werden die Daten zunächst nach long konvertiert. Dann werden die aggregierten Mittelwerte por Gruppe mit Streuungsinformationen über die Zeit geplottet. Schließlich werden mit weniger Opacity die individuellen Verläufe pro Subj dazugeplottet.

require(tidyverse)
dd <-tibble(
  subj = as.factor(c(1,2,3,4,5, 6)),
  grp  = c("T", "C", "T", "C", "T", "C"),
  x.c    = c(11, 12, 13, 14, 15, 16),
  y1.c   = c( 2,  0,  5,  4,  7, 8),
  y2.c   = c( 6,  2,  7, 12,  9, 11),
  y3.c   = c( 9,  7,  8, 14,  11, 15),
  y4.c   = c( 10,  8,  9, 15,  12, 16),
)


# we convert dd to long format
dd.l <- dd %>% tidyr::gather(time, score, c(y1.c:y4.c)) %>% dplyr::arrange(subj)

require(ggplot2)
# without aggregation we can easyly plot our subjects time course as we can easyly groupe in long format
dd.l %>% ggplot(aes(x = time, y = score, group=subj, color=subj)) + 
  geom_point( alpha=0.2) 

# and we can easyly do a line plot
dd.l %>% ggplot(aes(x = time, y = score)) + 
  geom_point(aes(group=subj, color=subj), alpha=0.3) +
  geom_line(aes(group=subj, color=subj), alpha=0.3) 

dd.l %>% ggplot(aes(x = time, y = score, group=grp, shape=grp, color=grp)) + 
  stat_summary(fun = mean, geom="point", size=5) +
  stat_summary(fun.data = mean_se, geom = "errorbar", width=0.3) +
  stat_summary(fun = mean, geom="line") +
  geom_point(aes(group=subj, shape=grp), alpha=0.3) +
  geom_line(aes(group=subj), alpha=0.3) 

Geoms mit implizitem Aggregieren

Manche geom_...() aggregieren implizit, d. h. sie haben eine voreingestellte Art, ihre Daten zu aggregieren. Hier gezeigt am Beispiel geom_bar().

geom_bar() “zählt aus”, kann also z. B. Auftretenshäufigkeiten von Ausprägungen in Faktoren visualisieren.

Mit dem Trick `interaction(factor1, factor2) können auch die Kombinationen von Faktorausprägungen visuell “ausgezählt” werden

require(tidyverse)
dd <- read.delim("http://md.psych.bio.uni-goettingen.de/mv/data/be/e-2x2-unbal.txt")
dd$gender <- factor(dd$A, levels=c(1, -1), labels=c("f", "m"))
dd$group  <- factor(dd$B, levels=c(1, -1), labels=c("Treatment", "Control"))

# distribution of gender
dd %>% ggplot(aes(x=gender)) +
  geom_bar()

# distribution of group
dd %>% ggplot(aes(x=group)) +
  geom_bar()

dd <- read.delim("http://md.psych.bio.uni-goettingen.de/mv/data/virt/v_stait_exam_wide_unbal.txt")
dd$gender <- factor(dd$gender)
dd$group  <- factor(dd$group)
# we visualize counts of group membership 
dd %>% ggplot(aes(x=group)) +
  geom_bar()

# we visualize counts of gender membership 
dd %>% ggplot(aes(x=gender)) +
  geom_bar()

# ... and their combination
dd %>% ggplot(aes(x=interaction(gender, group))) +
  geom_bar()

Screencasts

Version: 22 April, 2021 07:48