How To Plot With Patchwork: Exercises

June 2, 2018

(This article was first published on R-exercises, and kindly contributed to R-bloggers)


The goal of patchwork is to make it simple to combine separate ggplots into the same graphic. It tries to solve the same problem as gridExtra::grid.arrange() and cowplot::plot_grid, but using an API that incites exploration and iteration.

Before proceeding, please follow our short tutorial.

Look at the examples given and try to understand the logic behind them. Then, try to solve the exercises below by using R without looking at the answers. Then, check the solutions to check your answers.

Exercise 1

Create a scatter-plot object of mtcars between mpg and disp.

Exercise 2

Create a box-plot object of mtcars between gear and disp grouped by gear.

Exercise 3

Compose those two objects into one graph.

Exercise 4

Repeat the previous process but in one plotting operation.

Exercise 5

Display the composed graph in one column with the two graphs, one below the other.

Exercise 6

Set the graph on top to have two times the size of the graph at the bottom.

Exercise 7

Add space between your plots.

Exercise 8

Create two objects of your choice (p3, p4) and display all of your four objects in nested mode by putting p4 on top p1 and p2 and p3 at the bottom (nested one below the other.)

Exercise 9

Now, put the two nested plots next to each other.

Exercise 10

Finally, display p4 in one column and the rest of your objects in another.

To leave a comment for the author, please follow the link and comment on their blog: R-exercises. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)