13 Cheatsheet for ‘patchwork’

Maria Pratyusha

Link to presentation and resources :- https://docs.google.com/presentation/d/1nKju2-jcMC8Ep7tjBPAkacSX181DVra8hawrTlqaF7Q/edit?usp=sharing

Note: although this code works fine locally, it is causing issues when rendered through GitHub Actions and therefore the chunks that involve patchwork will not be evaluated.

Import the dataset

data("iris")
head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa

Install the package

Used in combination with ggplot2 so install and load that as well

Let’s plot a few graphs using ‘ggplot2’

Scatterplot

ggp1 <- ggplot(iris,               
               aes(x = Sepal.Length,
                   y = Sepal.Width,
                   col = Species)) +
  geom_point()
ggp1 

Barchart

ggp2 <- ggplot(iris,               
               aes(x = Species,
                   y = Sepal.Width,
                   fill = Species)) +
  geom_bar(stat = "identity")
ggp2

Boxplot

ggp3 <- ggplot(iris,              
               aes(x = Species,
                   y = Sepal.Width,
                   col = Species)) +
  geom_boxplot()
ggp3

Composition plot using plot_arithmetic

ggp1 + ggp2 + ggp3 + plot_layout(ncol = 1)
ggp1 + ggp2 - ggp3 + plot_layout(ncol = 1)
ggp_sbs <- (ggp1 + ggp2) / ggp3   
ggp_sbs  

area()

layout <- c(
  area(1, 1),
  area(1, 3, 3),
  area(3, 1, 3, 2)
)
plot(layout)
ggp1 + ggp2 + ggp3 + plot_layout(design = layout)

guide_area()

ggp1 + ggp2 + ggp3 +  
  plot_layout(guides = 'collect', ncol = 2)

inset_element()

ggp1 +          
  inset_element(ggp2, 0.01, 0.01, 0.7, 0.5) +
  inset_element(ggp3, 0.4, 0.6, 0.99, 0.99)

plot_annotation

ggp1 / (ggp2 | ggp3) +
  plot_annotation(tag_levels = 'A')
ggp1 / ((ggp2 | ggp3) + plot_layout(tag_level = 'new')) +
  plot_annotation(tag_levels = c('A', '1'))