Basic ggplot for scientific graphing

Made with Quarto and flipbookr

George Savva

Welcome

This ‘flipbook’ template demonstrates the key features of ggplot2 in building nice scientific graphs

You need to install the development version of flipbookr from Keiran Healy for this to work.

The presentation is made completely in R with Quarto and flipbookr package for the construction of the plot slides.

The Quarto code is available for you to read!

library(readxl)

library(readxl)
walkingdata <- read_excel(path="walkingspeed.xlsx",
                          sheet="fixed")

library(readxl)
walkingdata <- read_excel(path="walkingspeed.xlsx",
                          sheet="fixed")
walkingdata$department <- factor(walkingdata$department)

library(readxl)
walkingdata <- read_excel(path="walkingspeed.xlsx",
                          sheet="fixed")
walkingdata$department <- factor(walkingdata$department)
walkingdata$time <- as.numeric(walkingdata$time)

library(readxl)
walkingdata <- read_excel(path="walkingspeed.xlsx",
                          sheet="fixed")
walkingdata$department <- factor(walkingdata$department)
walkingdata$time <- as.numeric(walkingdata$time)
walkingdata$time[walkingdata$time>100] <- NA

library(readxl)
walkingdata <- read_excel(path="walkingspeed.xlsx",
                          sheet="fixed")
walkingdata$department <- factor(walkingdata$department)
walkingdata$time <- as.numeric(walkingdata$time)
walkingdata$time[walkingdata$time>100] <- NA
walkingdata$time[walkingdata$time<1] <- NA

library(readxl)
walkingdata <- read_excel(path="walkingspeed.xlsx",
                          sheet="fixed")
walkingdata$department <- factor(walkingdata$department)
walkingdata$time <- as.numeric(walkingdata$time)
walkingdata$time[walkingdata$time>100] <- NA
walkingdata$time[walkingdata$time<1] <- NA

head(walkingdata)
# A tibble: 6 × 6
  patid group    time sex     age department
  <dbl> <chr>   <dbl> <chr> <dbl> <fct>     
1     1 treat    1.90 M        53 3         
2     2 control  3.54 M        61 3         
3     3 treat    2.93 M        65 1         
4     4 control  1.82 M        48 2         
5     5 treat    2.20 M        62 2         
6     6 control  3.04 M        62 4         

library(readxl)
walkingdata <- read_excel(path="walkingspeed.xlsx",
                          sheet="fixed")
walkingdata$department <- factor(walkingdata$department)
walkingdata$time <- as.numeric(walkingdata$time)
walkingdata$time[walkingdata$time>100] <- NA
walkingdata$time[walkingdata$time<1] <- NA

head(walkingdata)

library(ggplot2)
# A tibble: 6 × 6
  patid group    time sex     age department
  <dbl> <chr>   <dbl> <chr> <dbl> <fct>     
1     1 treat    1.90 M        53 3         
2     2 control  3.54 M        61 3         
3     3 treat    2.93 M        65 1         
4     4 control  1.82 M        48 2         
5     5 treat    2.20 M        62 2         
6     6 control  3.04 M        62 4         

library(readxl)
walkingdata <- read_excel(path="walkingspeed.xlsx",
                          sheet="fixed")
walkingdata$department <- factor(walkingdata$department)
walkingdata$time <- as.numeric(walkingdata$time)
walkingdata$time[walkingdata$time>100] <- NA
walkingdata$time[walkingdata$time<1] <- NA

head(walkingdata)

library(ggplot2)

ggplot(walkingdata)
# A tibble: 6 × 6
  patid group    time sex     age department
  <dbl> <chr>   <dbl> <chr> <dbl> <fct>     
1     1 treat    1.90 M        53 3         
2     2 control  3.54 M        61 3         
3     3 treat    2.93 M        65 1         
4     4 control  1.82 M        48 2         
5     5 treat    2.20 M        62 2         
6     6 control  3.04 M        62 4         

library(readxl)
walkingdata <- read_excel(path="walkingspeed.xlsx",
                          sheet="fixed")
walkingdata$department <- factor(walkingdata$department)
walkingdata$time <- as.numeric(walkingdata$time)
walkingdata$time[walkingdata$time>100] <- NA
walkingdata$time[walkingdata$time<1] <- NA

head(walkingdata)

library(ggplot2)

ggplot(walkingdata) +
  aes(x=group)
# A tibble: 6 × 6
  patid group    time sex     age department
  <dbl> <chr>   <dbl> <chr> <dbl> <fct>     
1     1 treat    1.90 M        53 3         
2     2 control  3.54 M        61 3         
3     3 treat    2.93 M        65 1         
4     4 control  1.82 M        48 2         
5     5 treat    2.20 M        62 2         
6     6 control  3.04 M        62 4         

library(readxl)
walkingdata <- read_excel(path="walkingspeed.xlsx",
                          sheet="fixed")
walkingdata$department <- factor(walkingdata$department)
walkingdata$time <- as.numeric(walkingdata$time)
walkingdata$time[walkingdata$time>100] <- NA
walkingdata$time[walkingdata$time<1] <- NA

head(walkingdata)

library(ggplot2)

ggplot(walkingdata) +
  aes(x=group) +
  aes(y=time)
# A tibble: 6 × 6
  patid group    time sex     age department
  <dbl> <chr>   <dbl> <chr> <dbl> <fct>     
1     1 treat    1.90 M        53 3         
2     2 control  3.54 M        61 3         
3     3 treat    2.93 M        65 1         
4     4 control  1.82 M        48 2         
5     5 treat    2.20 M        62 2         
6     6 control  3.04 M        62 4         

library(readxl)
walkingdata <- read_excel(path="walkingspeed.xlsx",
                          sheet="fixed")
walkingdata$department <- factor(walkingdata$department)
walkingdata$time <- as.numeric(walkingdata$time)
walkingdata$time[walkingdata$time>100] <- NA
walkingdata$time[walkingdata$time<1] <- NA

head(walkingdata)

library(ggplot2)

ggplot(walkingdata) +
  aes(x=group) +
  aes(y=time) +

  geom_boxplot(outlier.shape = NA, fill=NA)
# A tibble: 6 × 6
  patid group    time sex     age department
  <dbl> <chr>   <dbl> <chr> <dbl> <fct>     
1     1 treat    1.90 M        53 3         
2     2 control  3.54 M        61 3         
3     3 treat    2.93 M        65 1         
4     4 control  1.82 M        48 2         
5     5 treat    2.20 M        62 2         
6     6 control  3.04 M        62 4         

library(readxl)
walkingdata <- read_excel(path="walkingspeed.xlsx",
                          sheet="fixed")
walkingdata$department <- factor(walkingdata$department)
walkingdata$time <- as.numeric(walkingdata$time)
walkingdata$time[walkingdata$time>100] <- NA
walkingdata$time[walkingdata$time<1] <- NA

head(walkingdata)

library(ggplot2)

ggplot(walkingdata) +
  aes(x=group) +
  aes(y=time) +

  geom_boxplot(outlier.shape = NA, fill=NA) +
  scale_y_log10()
# A tibble: 6 × 6
  patid group    time sex     age department
  <dbl> <chr>   <dbl> <chr> <dbl> <fct>     
1     1 treat    1.90 M        53 3         
2     2 control  3.54 M        61 3         
3     3 treat    2.93 M        65 1         
4     4 control  1.82 M        48 2         
5     5 treat    2.20 M        62 2         
6     6 control  3.04 M        62 4         

library(readxl)
walkingdata <- read_excel(path="walkingspeed.xlsx",
                          sheet="fixed")
walkingdata$department <- factor(walkingdata$department)
walkingdata$time <- as.numeric(walkingdata$time)
walkingdata$time[walkingdata$time>100] <- NA
walkingdata$time[walkingdata$time<1] <- NA

head(walkingdata)

library(ggplot2)

ggplot(walkingdata) +
  aes(x=group) +
  aes(y=time) +

  geom_boxplot(outlier.shape = NA, fill=NA) +
  scale_y_log10() +
  labs(x="Treatment Group")
# A tibble: 6 × 6
  patid group    time sex     age department
  <dbl> <chr>   <dbl> <chr> <dbl> <fct>     
1     1 treat    1.90 M        53 3         
2     2 control  3.54 M        61 3         
3     3 treat    2.93 M        65 1         
4     4 control  1.82 M        48 2         
5     5 treat    2.20 M        62 2         
6     6 control  3.04 M        62 4         

library(readxl)
walkingdata <- read_excel(path="walkingspeed.xlsx",
                          sheet="fixed")
walkingdata$department <- factor(walkingdata$department)
walkingdata$time <- as.numeric(walkingdata$time)
walkingdata$time[walkingdata$time>100] <- NA
walkingdata$time[walkingdata$time<1] <- NA

head(walkingdata)

library(ggplot2)

ggplot(walkingdata) +
  aes(x=group) +
  aes(y=time) +

  geom_boxplot(outlier.shape = NA, fill=NA) +
  scale_y_log10() +
  labs(x="Treatment Group") +
  labs(y="Time (seconds)")
# A tibble: 6 × 6
  patid group    time sex     age department
  <dbl> <chr>   <dbl> <chr> <dbl> <fct>     
1     1 treat    1.90 M        53 3         
2     2 control  3.54 M        61 3         
3     3 treat    2.93 M        65 1         
4     4 control  1.82 M        48 2         
5     5 treat    2.20 M        62 2         
6     6 control  3.04 M        62 4         

library(readxl)
walkingdata <- read_excel(path="walkingspeed.xlsx",
                          sheet="fixed")
walkingdata$department <- factor(walkingdata$department)
walkingdata$time <- as.numeric(walkingdata$time)
walkingdata$time[walkingdata$time>100] <- NA
walkingdata$time[walkingdata$time<1] <- NA

head(walkingdata)

library(ggplot2)

ggplot(walkingdata) +
  aes(x=group) +
  aes(y=time) +

  geom_boxplot(outlier.shape = NA, fill=NA) +
  scale_y_log10() +
  labs(x="Treatment Group") +
  labs(y="Time (seconds)") +
  facet_wrap(~sex)
# A tibble: 6 × 6
  patid group    time sex     age department
  <dbl> <chr>   <dbl> <chr> <dbl> <fct>     
1     1 treat    1.90 M        53 3         
2     2 control  3.54 M        61 3         
3     3 treat    2.93 M        65 1         
4     4 control  1.82 M        48 2         
5     5 treat    2.20 M        62 2         
6     6 control  3.04 M        62 4         

library(readxl)
walkingdata <- read_excel(path="walkingspeed.xlsx",
                          sheet="fixed")
walkingdata$department <- factor(walkingdata$department)
walkingdata$time <- as.numeric(walkingdata$time)
walkingdata$time[walkingdata$time>100] <- NA
walkingdata$time[walkingdata$time<1] <- NA

head(walkingdata)

library(ggplot2)

ggplot(walkingdata) +
  aes(x=group) +
  aes(y=time) +

  geom_boxplot(outlier.shape = NA, fill=NA) +
  scale_y_log10() +
  labs(x="Treatment Group") +
  labs(y="Time (seconds)") +
  facet_wrap(~sex) +
  theme_bw()
# A tibble: 6 × 6
  patid group    time sex     age department
  <dbl> <chr>   <dbl> <chr> <dbl> <fct>     
1     1 treat    1.90 M        53 3         
2     2 control  3.54 M        61 3         
3     3 treat    2.93 M        65 1         
4     4 control  1.82 M        48 2         
5     5 treat    2.20 M        62 2         
6     6 control  3.04 M        62 4         

library(readxl)
walkingdata <- read_excel(path="walkingspeed.xlsx",
                          sheet="fixed")
walkingdata$department <- factor(walkingdata$department)
walkingdata$time <- as.numeric(walkingdata$time)
walkingdata$time[walkingdata$time>100] <- NA
walkingdata$time[walkingdata$time<1] <- NA

head(walkingdata)

library(ggplot2)

ggplot(walkingdata) +
  aes(x=group) +
  aes(y=time) +

  geom_boxplot(outlier.shape = NA, fill=NA) +
  scale_y_log10() +
  labs(x="Treatment Group") +
  labs(y="Time (seconds)") +
  facet_wrap(~sex) +
  theme_bw() +
  coord_flip()
# A tibble: 6 × 6
  patid group    time sex     age department
  <dbl> <chr>   <dbl> <chr> <dbl> <fct>     
1     1 treat    1.90 M        53 3         
2     2 control  3.54 M        61 3         
3     3 treat    2.93 M        65 1         
4     4 control  1.82 M        48 2         
5     5 treat    2.20 M        62 2         
6     6 control  3.04 M        62 4         

library(readxl)
walkingdata <- read_excel(path="walkingspeed.xlsx",
                          sheet="fixed")
walkingdata$department <- factor(walkingdata$department)
walkingdata$time <- as.numeric(walkingdata$time)
walkingdata$time[walkingdata$time>100] <- NA
walkingdata$time[walkingdata$time<1] <- NA

head(walkingdata)

library(ggplot2)

ggplot(walkingdata) +
  aes(x=group) +
  aes(y=time) +

  geom_boxplot(outlier.shape = NA, fill=NA) +
  scale_y_log10() +
  labs(x="Treatment Group") +
  labs(y="Time (seconds)") +
  facet_wrap(~sex) +
  theme_bw() +
  coord_flip() +
  ggtitle("The relationship between walking
  speed and treatment group, stratified by sex")
# A tibble: 6 × 6
  patid group    time sex     age department
  <dbl> <chr>   <dbl> <chr> <dbl> <fct>     
1     1 treat    1.90 M        53 3         
2     2 control  3.54 M        61 3         
3     3 treat    2.93 M        65 1         
4     4 control  1.82 M        48 2         
5     5 treat    2.20 M        62 2         
6     6 control  3.04 M        62 4         

library(readxl)
walkingdata <- read_excel(path="walkingspeed.xlsx",
                          sheet="fixed")
walkingdata$department <- factor(walkingdata$department)
walkingdata$time <- as.numeric(walkingdata$time)
walkingdata$time[walkingdata$time>100] <- NA
walkingdata$time[walkingdata$time<1] <- NA

head(walkingdata)

library(ggplot2)

ggplot(walkingdata) +
  aes(x=group) +
  aes(y=time) +

  geom_boxplot(outlier.shape = NA, fill=NA) +
  scale_y_log10() +
  labs(x="Treatment Group") +
  labs(y="Time (seconds)") +
  facet_wrap(~sex) +
  theme_bw() +
  coord_flip() +
  ggtitle("The relationship between walking
  speed and treatment group, stratified by sex")
# A tibble: 6 × 6
  patid group    time sex     age department
  <dbl> <chr>   <dbl> <chr> <dbl> <fct>     
1     1 treat    1.90 M        53 3         
2     2 control  3.54 M        61 3         
3     3 treat    2.93 M        65 1         
4     4 control  1.82 M        48 2         
5     5 treat    2.20 M        62 2         
6     6 control  3.04 M        62 4