# Chapter 6 Do It Yourself

## 6.1 Jur Brauers

``````jb <- read.csv("https://stulp.gmw.rug.nl/GSMS/synthetic_data_resilience.csv")

library(tidyverse)
library(lubridate)
jb <- jb %>%
select(-X) %>% # drop first (empty) variable
mutate(date_ymd = ymd(date_ymd)) # turn date into date format

# Player ID 57 has most measurements
jb_57 <- jb %>% filter(playerId == 57)``````
``````# Reshape from wide to long
jb_57_long <- jb_57 %>%
select(-teamId, -playerId) %>%
gather(-date_ymd, key = "Variable", value = "Score")

ggplot(jb_57_long, aes(x = date_ymd, y = Score, colour = Variable)) +
geom_point(size = 0.5) +
geom_smooth(se = FALSE)``````

``````ggplot(filter(jb_57_long, !is.na(Score)),  # Remove missing values
aes(x = date_ymd, y = Score)) +
geom_path() +
facet_wrap(~Variable, ncol = 1)``````

## 6.2 Annet Vulto

``````av <- read.csv("https://stulp.gmw.rug.nl/GSMS/Vulto1.csv")

# From wide to long
av_long <- av %>%
gather(-ID, key = "Dosage", value = "Ratio_THF")  ``````
``````ggplot(av_long, aes(x = Dosage, y = Ratio_THF, group = factor(ID))) +
geom_point(aes(colour = factor(ID)), alpha = 0.5) +
geom_line(aes(colour = factor(ID)), alpha = 0.5) +
geom_point(aes(group = NULL), stat = "summary", fun = "mean",
fun.args = list(na.rm = TRUE), colour = "black", size = 2) +
geom_line(aes(group = "overall"), stat = "summary", fun = "mean",
fun.args = list(na.rm = TRUE), colour = "black") +
scale_x_discrete(limits = c("X10mg.prednisolone", "X7.5mg.prednisolone",
"X5mg.prednisolone", "X2.5mg.prednisolone.",
"X4.weeks.0.mg", "X12.weeks.0mg", "Control"),
labels = c("10 mg", "7.5 mg", "5 mg", "2.5 mg",
"0 mg, 4 weeks", "0 mg, 12 weeks", "Control")) +
guides(colour = FALSE) +
labs(y = "Ratio (THF + aTHF) /THE") +
theme_minimal()``````

``````av2 <- read.csv("https://stulp.gmw.rug.nl/GSMS/Vulto2.csv")

# install.packages("devtools)
#                           dependencies = TRUE)
library(tidyverse)

av2 <- av2 %>%
rename(group = "SF.36") %>% # Variables needs to be named "group"
mutate_at(vars(-group), function(x) x/100) # get them on scale from 0, 1

## 6.3 Roos Bleckman

``````library(ggalluvial)
library(haven)

# Freq <- sub826\$Freq
# first <- sub826\$first
# sub <- sub826\$Sub
# Agegroup <- sub826\$Agegroup
# as.factor(sub826\$Agegroup)
# data <- data.frame(sub, first, Freq)

#without age groups (figure 1)
ggplot(sub826, aes(y = Freq, axis1 = Sub, axis2 = first)) +
geom_alluvium(aes(fill = Sub), width = 1/8, knot.pos = 0,
reverse = TRUE, aes.bind = "alluvia" , curve_type = "cubic") +
guides(fill = "none") +
geom_stratum(alpha = .25, width = 1/8, reverse = TRUE) +
geom_text(stat = "stratum", aes(label = after_stat(stratum)),reverse = TRUE) +
scale_x_continuous(breaks = 1:2, labels = c("End of treatment", "End of treatment specify")) +
ggtitle("Example data") +
scale_y_continuous(breaks=c(0, 100, 200, 300, 400, 500, 600, 700, 800),
name="Number of patients")  +
scale_fill_brewer(type = "qual", palette = "Set3", direction=-1) +
theme_minimal()``````

``````#including age groups (figure 2)
ggplot(sub826, aes(y = Freq, axis1 = Sub, axis2 = first)) +
geom_alluvium(aes(fill = factor(Agegroup)),
width = 1/8, knot.pos = 0, reverse = TRUE,
aes.bind = "alluvia" , curve_type = "cubic") +
# guides(fill = "none") +
geom_stratum(alpha = .25, width = 1/8, reverse = TRUE) +
geom_text(stat = "stratum", aes(label = after_stat(stratum)),reverse = TRUE) +
scale_x_continuous(breaks = 1:2, labels = c("End of treatment", "End of treatment specify")) +
ggtitle("Example data") +
scale_y_continuous(breaks=c(0, 100, 200, 300, 400, 500, 600, 700, 800),
name = "Number of patients")  +
scale_fill_brewer(type = "qual", palette = "Set3", direction=-1) +
theme_minimal()``````

``````ggplot(sub826, aes(y = Freq, axis1 = Sub, axis2 = first)) +
geom_alluvium(aes(fill = Sub), width = 1/8, knot.pos = 0,
reverse = TRUE, aes.bind = "alluvia" , curve_type = "cubic") +
guides(fill = "none") +
geom_stratum(alpha = .25, width = 1/8, reverse = TRUE) +
geom_text(stat = "stratum", aes(label = after_stat(stratum)),reverse = TRUE) +
scale_x_continuous(breaks = 1:2, labels = c("End of treatment", "End of treatment specify")) +
ggtitle("Example data") +
scale_y_continuous(breaks=c(0, 100, 200, 300, 400, 500, 600, 700, 800),
name="Number of patients")  +
scale_fill_brewer(type = "qual", palette = "Set3", direction=-1) +
theme_minimal() +
facet_wrap(~factor(Agegroup, levels = c(0, 1), labels = c("young", "old")))``````