library(ggplot2)
asp = read.table(file.path(pfadu, "asp.txt"))
coronal = read.table(file.path(pfadu, "coronal.txt"))
int.df = read.table(file.path(pfadu, "intdauer.txt"))

###########################################################################
# 1. Zwei Unabhängige Variablen
############################################################################
# Anstatt y, x
# y, x1, x2

########################## geom_boxplot()
# Was ist der Einfluss der Artikulationsstelle (Kons) und Betonung (Bet) auf die Dauer (d) im Data-Frame asp?
# Wird die Dauer (d) von x1 (Kons) und von x2 (Bet) beeinflusst?
# y ~ x1 + x2
# y (d): numerisch
# x1 (Kons): kategorial
# x2 (Bet): kategorial
# Boxplot: y ist numerisch, die anderen (abhängigen) Variablen sind kategorial

# Zwei Möglichkeiten. 
# Die zusätzliche Variable:
# i. mit Farbe oder Linientyp kodieren (es gibt dann eine Legende per default)
# ii. getrennt in einem anderen Panel darstellen.

# Zu i. 
# Zuerst y ~ x
ggplot(asp) + 
  aes(y = d, x = Kons) + 
  geom_boxplot() 

# y ~ x1 + x2
ggplot(asp) + 
  aes(y = d, x = Kons, col = Bet) + 
  geom_boxplot() 
# kann auch y ~ x2 + x1 sein
ggplot(asp) + 
  aes(y = d, x = Bet, col = Kons) + 
  geom_boxplot() 

#oder
ggplot(asp) + 
  aes(y = d, x = Bet, fill = Kons) + 
  geom_boxplot() 

#oder Linientyp
ggplot(asp) + 
  aes(y = d, x = Bet, lty = Kons) + 
  geom_boxplot() 

# Zu ii. Getrennte Panels, x1 in einem Panel, x2 in dem anderen mit der Funktion facet_wrap()
ggplot(asp) + 
  aes(y = d, x = Kons) + 
  geom_boxplot() + 
  facet_wrap(~Bet)

# i und ii zusammen

########################## geom_barchart()
# Was ist der Einfluss des Dialektes (Region) und Sozialklasse (Socialclass) auf die Artikulationsstelle (Fr) in dem Data-Frame coronal?
# y (Fr): kategorial
# x1 (Region): kategorial
# x2 (Sozialklasse): kategorial
# y ~ x1 + x2
# Alle Variablen sind kategorial: daher geom_bar().

# Nur x1 (also ohne zwischen den Sozialklassen zu differenzieren)
p1 = ggplot(coronal) + 
  aes(fill = Fr, x = Region) + 
  geom_bar() 
p1
# Mit x2
p2 = facet_wrap(~Socialclass)
p1 + p2

# Mit eigenen Farben
farben = c("cyan", "gold")
p3 = scale_fill_manual(values = farben) 
p1 + p2 + p3

########################## geom_point() und oder geom_line()
# Inwiefern wird die Dauer (Dauer) von der Intensität (dB) - getrennt pro Person (Vpn) - im Data-Frame int.df beeinflusst?
# y: Dauer (numerisch)
# x1: Intensität (numerisch)
# x2: Vpn (kategorial)
# y ~ x1 + x2. Zwei Variablen sind numerisch. Daher geom_point()
# Zwei Möglichkeiten. 
# Die zusätzliche Variable:
# i. mit Farbe kodieren
# ii. getrennt in einem anderen Panel darstellen.

# Zu i. 
# Zuerst y ~ x
ggplot(int.df) + 
  aes(y = dB, x = Dauer) + 
  geom_point() 
# y ~ x1 + x2
ggplot(int.df) + 
  aes(y = dB, x = Dauer, col = Vpn) + 
  geom_point() 

# Zu ii. Getrennte Panels, x1 in einem Panel, x2 in dem anderen.
ggplot(int.df) + 
  aes(y = dB, x = Dauer) + 
  geom_point() + 
  facet_wrap(~Vpn)
# Selbstverständlich geht auch i. und ii.
ggplot(int.df) + 
  aes(y = dB, x = Dauer, col = Vpn) + 
  geom_point() + 
  facet_wrap(~Vpn)
