Skip to content
This repository has been archived by the owner on Sep 18, 2019. It is now read-only.

Commit

Permalink
refresh block020_multiple-plots-on-a-page
Browse files Browse the repository at this point in the history
  • Loading branch information
jennybc committed Oct 20, 2015
1 parent a42f87c commit f4cce41
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 50 deletions.
77 changes: 52 additions & 25 deletions block020_multiple-plots-on-a-page.html

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions block020_multiple-plots-on-a-page.md
Expand Up @@ -16,43 +16,34 @@ You may need to install `gridExtra` and you will certainly need to load it.
```r
# install.packages("gridExtra")
library(gridExtra)
## Loading required package: grid
```

### Load the Gapminder data and ggplot2


```r
library(gapminder)
library(ggplot2)
gDat <- read.delim("gapminderDataFiveYear.tsv")
str(gDat)
## 'data.frame': 1704 obs. of 6 variables:
## $ country : Factor w/ 142 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ year : int 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
## $ pop : num 8425333 9240934 10267083 11537966 13079460 ...
## $ continent: Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
## $ lifeExp : num 28.8 30.3 32 34 36.1 ...
## $ gdpPercap: num 779 821 853 836 740 ...
```

### Use the `arrangeGrob()` function
### Use the `arrangeGrob()` function and friends

Store the constituent plots to plot objects and then pass them to `arrangeGrob()`.


```r
p_dens <- ggplot(gDat, aes(x = gdpPercap)) + geom_density() + scale_x_log10() +
p_dens <- ggplot(gapminder, aes(x = gdpPercap)) + geom_density() + scale_x_log10() +
theme(axis.text.x = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_blank())
p_scatter <- ggplot(gDat, aes(x = gdpPercap, y = lifeExp)) +
p_scatter <- ggplot(gapminder, aes(x = gdpPercap, y = lifeExp)) +
geom_point() + scale_x_log10()
p_both <- arrangeGrob(
p_dens, p_scatter, nrow = 2, heights = c(0.35, 0.65))
p_both
#p_both <- arrangeGrob(p_dens, p_scatter, nrow = 2, heights = c(0.35, 0.65))
#print(p_both)
grid.arrange(p_dens, p_scatter, nrow = 2, heights = c(0.35, 0.65))
```

![](./block020_multiple-plots-on-a-page_files/figure-html/arrangeGrob-demo.png)
![](block020_multiple-plots-on-a-page_files/figure-html/arrangeGrob-demo-1.png)

You can find many examples of `arrangeGrob()` usage in the [R Graph Catalog](http://shinyapps.stat.ubc.ca/r-graph-catalog/).

Expand Down Expand Up @@ -117,3 +108,12 @@ multiplot(p1, p2, p3, p4, cols = 2)
```

Visit [Multiple graphs on one page (ggplot2)](http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/) to see a complete worked example.

### Use the `cowplot` package

The `cowplot` package (on [CRAN](https://cran.r-project.org/web/packages/cowplot/index.html), on [GitHub](https://github.com/wilkelab/cowplot)) does (at least) two things:

* Provide a publication-ready theme for `ggplot2`
* Helps combine multiple plots into one figure

Check out [the vignette](https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html) to see it in action.
24 changes: 16 additions & 8 deletions block020_multiple-plots-on-a-page.rmd
Expand Up @@ -28,25 +28,24 @@ library(gridExtra)
### Load the Gapminder data and ggplot2

```{r}
library(gapminder)
library(ggplot2)
gDat <- read.delim("gapminderDataFiveYear.tsv")
str(gDat)
```

### Use the `arrangeGrob()` function
### Use the `arrangeGrob()` function and friends

Store the constituent plots to plot objects and then pass them to `arrangeGrob()`.

```{r arrangeGrob-demo}
p_dens <- ggplot(gDat, aes(x = gdpPercap)) + geom_density() + scale_x_log10() +
p_dens <- ggplot(gapminder, aes(x = gdpPercap)) + geom_density() + scale_x_log10() +
theme(axis.text.x = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_blank())
p_scatter <- ggplot(gDat, aes(x = gdpPercap, y = lifeExp)) +
p_scatter <- ggplot(gapminder, aes(x = gdpPercap, y = lifeExp)) +
geom_point() + scale_x_log10()
p_both <- arrangeGrob(
p_dens, p_scatter, nrow = 2, heights = c(0.35, 0.65))
p_both
#p_both <- arrangeGrob(p_dens, p_scatter, nrow = 2, heights = c(0.35, 0.65))
#print(p_both)
grid.arrange(p_dens, p_scatter, nrow = 2, heights = c(0.35, 0.65))
```

You can find many examples of `arrangeGrob()` usage in the [R Graph Catalog](http://shinyapps.stat.ubc.ca/r-graph-catalog/).
Expand Down Expand Up @@ -110,3 +109,12 @@ multiplot(p1, p2, p3, p4, cols = 2)
```

Visit [Multiple graphs on one page (ggplot2)](http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/) to see a complete worked example.

### Use the `cowplot` package

The `cowplot` package (on [CRAN](https://cran.r-project.org/web/packages/cowplot/index.html), on [GitHub](https://github.com/wilkelab/cowplot)) does (at least) two things:

* Provide a publication-ready theme for `ggplot2`
* Helps combine multiple plots into one figure

Check out [the vignette](https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html) to see it in action.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.

0 comments on commit f4cce41

Please sign in to comment.