Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arranging plots: slide edits #54

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 01-introduction.Rmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# (PART\*) Getting Started {-}

# Introduction {-}
# Introduction

**Learning objectives:**

Expand Down
91 changes: 63 additions & 28 deletions 09-Arranging_plots.Rmd
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
# Arranging Plots


**Learning Objectives**

- Produce several subplots part of the same main visualization
- A range of packages for providing different approaches to arranging separate plots
1. Produce several subplots as part of the same main visualization
2. Understand how to arrange plots
- in rows and columns
- on top of each other (insets)



## Introduction {-}

## Introduction
Want to arrange multiple plots; need more than faceting

This chapter focuses on making more than one plot in one visualization, using the following packages:
This chapter discusses {patchwork}.

1. patchwork
2. cowplot
3. gridExtra
4. ggpubr
Others:

* {cowplot}
* {gridExtra}
* {ggpubr}

## Arranging plots side by side with no overlap
## Arranging plots side by side with no overlap {-}

```{r 09-library, message=FALSE, warning=FALSE, include=FALSE, paged.print=FALSE}
library(tidyverse)
```

Let's imagine we have several plots:

```{r 09-dataplots, include=FALSE}
```{r 09-dataplots}
p1 <- ggplot(mpg) +
geom_point(aes(x = displ, y = hwy))

Expand All @@ -46,15 +50,15 @@ p4 <- ggplot(mpg) +
```{r 09-plot1}
library(patchwork)

p1 + p2 # same as p3 | p4
p1 + p2 # same as p1 | p2
```

```{r 09-plot2}
p1 + p2 + p3 + p4
```


### Taking control of the layout
## Controlling the layout {-}

```{r 09-plot3}
p1 + p2 + p3 + plot_layout(ncol = 2)
Expand All @@ -64,12 +68,13 @@ p1 + p2 + p3 + plot_layout(ncol = 2)
p1 / p2
```

More compositions:
## More compositions: {-}

```{r 09-plot5}
p3 | (p2 / (p1 | p4))
```

### More about layouts
## Layouts can get creative! {-}

```{r 09-plot6}
layout <- "
Expand All @@ -81,25 +86,30 @@ CDD
p1 + p2 + p3 + p4 + plot_layout(design = layout)
```

## Collect repeats of the same legend {-}

```{r 09-plot7}
p1 + p2 + p3 + guide_area() + plot_layout(ncol = 2, guides = "collect")
```


This way is possible a custom modification of the theme for one plot or for both.
## Parts of the patchwork object can still be modified {-}

```{r 09-plot8}
p12 <- p1 + p2
p12[[1]] <- p12[[1]] + theme_light()
p12
```

## New operator: `&` adds whole-plot themes {-}

```{r 09-plot9}
p1 + p4 &
scale_y_continuous(limits = c(0, 45)) &
theme_minimal()
```

### Plot annotations
### Plot annotations {-}
```{r 09-plot10}
p34 <- p3 + p4 +
plot_annotation(
Expand All @@ -111,21 +121,44 @@ p34 <- p3 + p4 +
p34
```

## Labeling plots (e.g. parts of figures) {-}

```{r 09-plot11}
p34[[2]] <- p34[[2]] + plot_layout(tag_level = "new")
new <- p34
keep <- p34
new[[2]] <- new[[2]] + plot_layout(tag_level = "new")
keep[[2]] <- keep[[2]] + plot_layout(tag_level = "keep")
```

```{r}
new
```

```{r}
keep
```


## Specify the type of tags/labels {-}
```{r}
p34 + plot_annotation(tag_levels = c("I", "a"))
```


## Arranging plots on top of each other
## Arranging plots on top of each other {-}

Instead of putting plots next to or above/below each other, we can nest them.

It is possible to arrange plots in a way that they are nested to each other, as well as setting the position inside the main plot.
General options: left, right, top, and bottom.
Set specific locations

General options are left, right, top, and bottom locations, but more specific locations can be set, such as using: `grid::unit()` (default uses npc units which goes from 0 to 1)
- `grid::unit()`--npc units ("Normalized Parent Coordinates"), ranges from 0 to 1

In addition, the location is by default set to the **panel area**, but can be align_to` **plot area**.
Location is relative to **panel area**, not **plot area**. `align_to` arg changes this.

An inset can be placed exactly 15 mm from the top right corner.
## Example: {-}

Placing an inset exactly 15 mm from the top right corner:

```{r 09-plot12}
p1 +
Expand All @@ -139,6 +172,8 @@ p1 +
)
```

## Another inset example with annotation {-}

```{r 09-plot13}
p24 <- p2 / p4 + plot_layout(guides = "collect")

Expand All @@ -150,7 +185,7 @@ p1 +



## Extra
## Extra {-}

**grid and gridExtra packages**
```{r 09-plot14, message=FALSE, warning=FALSE, paged.print=FALSE}
Expand Down Expand Up @@ -221,21 +256,21 @@ ggarrange(p1, p2, p3, ncol = 2, nrow = 2,common.legend = TRUE)
```


## Conclusions
## Conclusions {-}


[Patchwork - imaginist](https://patchwork.data-imaginist.com/) is one of the packages mentioned in the book, also some other packages provide same results with different approaches.

### Extra resources:
### Extra resources: {-}

- [grid and gridExtra](https://cran.r-project.org/web/packages/gridExtra/vignettes/arrangeGrob.html)
- [cowplot](https://wilkelab.org/cowplot/articles/plot_grid.html)
- [ggpubr](https://rpkgs.datanovia.com/ggpubr/reference/ggarrange.html)


## Meeting Videos
## Meeting Videos {-}

### Cohort 1
### Cohort 1 {-}

`r knitr::include_url("https://www.youtube.com/embed/mGE639JpneE")`

Expand Down
Loading