Skip to content

Commit

Permalink
improved vignette
Browse files Browse the repository at this point in the history
  • Loading branch information
clauswilke committed Oct 20, 2017
1 parent ddc2bc2 commit e743b1e
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 46 deletions.
4 changes: 2 additions & 2 deletions inst/doc/axis_position.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<meta name="author" content="Claus O. Wilke" />

<meta name="date" content="2017-08-13" />
<meta name="date" content="2017-10-20" />

<title>Changing the axis positions</title>

Expand Down Expand Up @@ -70,7 +70,7 @@

<h1 class="title toc-ignore">Changing the axis positions</h1>
<h4 class="author"><em>Claus O. Wilke</em></h4>
<h4 class="date"><em>2017-08-13</em></h4>
<h4 class="date"><em>2017-10-20</em></h4>



Expand Down
24 changes: 24 additions & 0 deletions inst/doc/introduction.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,27 @@ ggdraw() +
theme(legend.justification = "top"), 0.5, 0.52, 0.5, 0.4) +
draw_plot_label(c("A", "B"), c(0, 0.5), c(1, 0.92), size = 15)

## ----message=FALSE-------------------------------------------------------
p <- ggplot(iris, aes(x=Sepal.Length, fill=Species)) + geom_density(alpha = 0.7)
ggdraw() +
draw_image("http://jeroen.github.io/images/tiger.svg") +
draw_plot(p)

## ----message=FALSE, results="hide"---------------------------------------
img <- magick::image_read("http://jeroen.github.io/images/tiger.svg")
img <- magick::image_transparent(img, color = "white")
img2 <- magick::image_charcoal(img)
img2 <- magick::image_transparent(img2, color = "white")

## ----message=FALSE-------------------------------------------------------
ggplot(data.frame(x=1:3, y=1:3), aes(x, y)) +
geom_point(size = 3) +
geom_abline(slope = 1, intercept = 0, linetype = 2, color = "blue") +
draw_image(img, x=1, y=1, scale = .9) +
draw_image(img2, x=2, y=2, scale = .9)

## ----message=FALSE, fig.width=7, fig.height=2.5--------------------------
p <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) + geom_density(alpha = 0.7)
p2 <- ggdraw() + draw_image("http://jeroen.github.io/images/tiger.svg", scale = 0.9)
plot_grid(p, p2, labels = "AUTO")

36 changes: 35 additions & 1 deletion inst/doc/introduction.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ ggdraw(plot.mpg) +

Note that placing a plot on top of annotations only makes sense if the plot background is transparent. This is one of the main differences between `theme_cowplot()` and `theme_classic()`. If you tried the same example with `theme_classic()`, the gray boxes underneath the plot would not show.

Finally, the `draw_plot()` function also allows us to place graphs at arbitrary locations and at arbitrary sizes onto the canvas. This is useful for combining subplots into a layout that is not a simple grid, e.g. with an inset plotted inside a larger graph.
The `draw_plot()` function also allows us to place graphs at arbitrary locations and at arbitrary sizes onto the canvas. This is useful for combining subplots into a layout that is not a simple grid, e.g. with an inset plotted inside a larger graph.
```{r message=FALSE, fig.width=7, fig.height=6}
# plot.mpg and plot.diamonds were defined earlier
library(viridis)
Expand All @@ -211,3 +211,37 @@ ggdraw() +
draw_plot_label(c("A", "B"), c(0, 0.5), c(1, 0.92), size = 15)
```

We can also combine plots and images, using the function `draw_image()`. This function, which requires the **magick** package to be installed, can take images in many different formats and combine them with **ggplot2** plots. For example, we can use an image as a plot background:


```{r message=FALSE}
p <- ggplot(iris, aes(x=Sepal.Length, fill=Species)) + geom_density(alpha = 0.7)
ggdraw() +
draw_image("http://jeroen.github.io/images/tiger.svg") +
draw_plot(p)
```

Or we can manipulate images and draw them in plot coordinates:

```{r message=FALSE, results="hide"}
img <- magick::image_read("http://jeroen.github.io/images/tiger.svg")
img <- magick::image_transparent(img, color = "white")
img2 <- magick::image_charcoal(img)
img2 <- magick::image_transparent(img2, color = "white")
```

```{r message=FALSE}
ggplot(data.frame(x=1:3, y=1:3), aes(x, y)) +
geom_point(size = 3) +
geom_abline(slope = 1, intercept = 0, linetype = 2, color = "blue") +
draw_image(img, x=1, y=1, scale = .9) +
draw_image(img2, x=2, y=2, scale = .9)
```

We can also make a plot grid with a regular plot and an image:

```{r message=FALSE, fig.width=7, fig.height=2.5}
p <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) + geom_density(alpha = 0.7)
p2 <- ggdraw() + draw_image("http://jeroen.github.io/images/tiger.svg", scale = 0.9)
plot_grid(p, p2, labels = "AUTO")
```
48 changes: 36 additions & 12 deletions inst/doc/introduction.html

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions inst/doc/plot_annotations.html

Large diffs are not rendered by default.

30 changes: 15 additions & 15 deletions inst/doc/plot_grid.html

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions inst/doc/shared_legends.html

Large diffs are not rendered by default.

36 changes: 35 additions & 1 deletion vignettes/introduction.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ ggdraw(plot.mpg) +

Note that placing a plot on top of annotations only makes sense if the plot background is transparent. This is one of the main differences between `theme_cowplot()` and `theme_classic()`. If you tried the same example with `theme_classic()`, the gray boxes underneath the plot would not show.

Finally, the `draw_plot()` function also allows us to place graphs at arbitrary locations and at arbitrary sizes onto the canvas. This is useful for combining subplots into a layout that is not a simple grid, e.g. with an inset plotted inside a larger graph.
The `draw_plot()` function also allows us to place graphs at arbitrary locations and at arbitrary sizes onto the canvas. This is useful for combining subplots into a layout that is not a simple grid, e.g. with an inset plotted inside a larger graph.
```{r message=FALSE, fig.width=7, fig.height=6}
# plot.mpg and plot.diamonds were defined earlier
library(viridis)
Expand All @@ -211,3 +211,37 @@ ggdraw() +
draw_plot_label(c("A", "B"), c(0, 0.5), c(1, 0.92), size = 15)
```

We can also combine plots and images, using the function `draw_image()`. This function, which requires the **magick** package to be installed, can take images in many different formats and combine them with **ggplot2** plots. For example, we can use an image as a plot background:


```{r message=FALSE}
p <- ggplot(iris, aes(x=Sepal.Length, fill=Species)) + geom_density(alpha = 0.7)
ggdraw() +
draw_image("http://jeroen.github.io/images/tiger.svg") +
draw_plot(p)
```

Or we can manipulate images and draw them in plot coordinates:

```{r message=FALSE, results="hide"}
img <- magick::image_read("http://jeroen.github.io/images/tiger.svg")
img <- magick::image_transparent(img, color = "white")
img2 <- magick::image_charcoal(img)
img2 <- magick::image_transparent(img2, color = "white")
```

```{r message=FALSE}
ggplot(data.frame(x=1:3, y=1:3), aes(x, y)) +
geom_point(size = 3) +
geom_abline(slope = 1, intercept = 0, linetype = 2, color = "blue") +
draw_image(img, x=1, y=1, scale = .9) +
draw_image(img2, x=2, y=2, scale = .9)
```

We can also make a plot grid with a regular plot and an image:

```{r message=FALSE, fig.width=7, fig.height=2.5}
p <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) + geom_density(alpha = 0.7)
p2 <- ggdraw() + draw_image("http://jeroen.github.io/images/tiger.svg", scale = 0.9)
plot_grid(p, p2, labels = "AUTO")
```

0 comments on commit e743b1e

Please sign in to comment.