From 48acb4b0e63880232cc5950531fdc1020ed4fcb5 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Wed, 23 Feb 2022 14:05:33 -0600 Subject: [PATCH] Tweak plot sizing strategy --- _common.R | 5 ++--- data-visualize.Rmd | 12 ++++++------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/_common.R b/_common.R index 09b6669..c40e884 100644 --- a/_common.R +++ b/_common.R @@ -4,10 +4,9 @@ knitr::opts_chunk$set( comment = "#>", collapse = TRUE, # cache = TRUE, - out.width = "70%", - fig.align = 'center', + fig.retina = 2, fig.width = 6, - fig.asp = 0.618, # 1 / phi + fig.asp = 2/3, fig.show = "hold" ) diff --git a/data-visualize.Rmd b/data-visualize.Rmd index e17e724..01969bc 100644 --- a/data-visualize.Rmd +++ b/data-visualize.Rmd @@ -147,7 +147,7 @@ You can display a point (like the one below) in different ways by changing the v Since we already use the word "value" to describe data, let's use the word "level" to describe aesthetic properties. Here we change the levels of a point's size, shape, and color to make the point small, triangular, or blue: -```{r, echo = FALSE, asp = 1/4, fig.alt = "Diagram that shows four plotting characters next to each other. The first is a large circle, the second is a small circle, the third is a triangle, and the fourth is a blue circle."} +```{r, echo = FALSE, fig.asp = 1/4, fig.alt = "Diagram that shows four plotting characters next to each other. The first is a large circle, the second is a small circle, the third is a triangle, and the fourth is a blue circle."} ggplot() + geom_point(aes(1, 1), size = 20) + geom_point(aes(2, 1), size = 10) + @@ -188,7 +188,7 @@ ggplot(data = mpg) + Or we could have mapped `class` to the *alpha* aesthetic, which controls the transparency of the points, or to the *shape* aesthetic, which controls the shape of the points. -```{r, out.width = "50%", fig.align = 'default', warning = FALSE, fig.asp = 1/2, fig.cap ="", fig.alt = "Two scatterplots next to each other, both visualizing highway fuel efficiency versus engine size of cars in ggplot2::mpg and showing a negative association. In the plot on the left class is mapped to the alpha aesthetic, resulting in different transparency levels for each level of class. In the plot on the right class is mapped the shape aesthetic, resulting in different plotting character shapes for each level of class. Each plot comes with a legend that shows the mapping between alpha level or shape and levels of the class variable."} +```{r, fig.width = 4, out.width = "50%", fig.align = 'default', warning = FALSE, fig.asp = 1/2, fig.cap ="", fig.alt = "Two scatterplots next to each other, both visualizing highway fuel efficiency versus engine size of cars in ggplot2::mpg and showing a negative association. In the plot on the left class is mapped to the alpha aesthetic, resulting in different transparency levels for each level of class. In the plot on the right class is mapped the shape aesthetic, resulting in different plotting character shapes for each level of class. Each plot comes with a legend that shows the mapping between alpha level or shape and levels of the class variable."} # Left ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, alpha = class)) @@ -229,7 +229,7 @@ You'll need to pick a level that makes sense for that aesthetic: - The shape of a point as a number, as shown in Figure \@ref(fig:shapes). -```{r shapes, echo = FALSE, out.width = "75%", fig.asp = 1/3, fig.cap="R has 25 built in shapes that are identified by numbers. There are some seeming duplicates: for example, 0, 15, and 22 are all squares. The difference comes from the interaction of the `colour` and `fill` aesthetics. The hollow shapes (0--14) have a border determined by `colour`; the solid shapes (15--20) are filled with `colour`; the filled shapes (21--24) have a border of `colour` and are filled with `fill`.", warning = FALSE, fig.alt = "Mapping between shapes and the numbers that represent them: 0 - square, 1 - circle, 2 - triangle point up, 3 - plus, 4 - cross, 5 - diamond, 6 - triangle point down, 7 - square cross, 8 - star, 9 - diamond plus, 10 - circle plus, 11 - triangles up and down, 12 - square plus, 13 - circle cross, 14 - square and triangle down, 15 - filled square, 16 - filled circle, 17 - filled triangle point-up, 18 - filled diamond, 19 - solid circle, 20 - bullet (smaller circle), 21 - filled circle blue, 22 - filled square blue, 23 - filled diamond blue, 24 - filled triangle point-up blue, 25 - filled triangle point down blue."} +```{r shapes, echo = FALSE, fig.asp = 1/2.75, fig.cap="R has 25 built in shapes that are identified by numbers. There are some seeming duplicates: for example, 0, 15, and 22 are all squares. The difference comes from the interaction of the `colour` and `fill` aesthetics. The hollow shapes (0--14) have a border determined by `colour`; the solid shapes (15--20) are filled with `colour`; the filled shapes (21--24) have a border of `colour` and are filled with `fill`.", warning = FALSE, fig.alt = "Mapping between shapes and the numbers that represent them: 0 - square, 1 - circle, 2 - triangle point up, 3 - plus, 4 - cross, 5 - diamond, 6 - triangle point down, 7 - square cross, 8 - star, 9 - diamond plus, 10 - circle plus, 11 - triangles up and down, 12 - square plus, 13 - circle cross, 14 - square and triangle down, 15 - filled square, 16 - filled circle, 17 - filled triangle point-up, 18 - filled diamond, 19 - solid circle, 20 - bullet (smaller circle), 21 - filled circle blue, 22 - filled square blue, 23 - filled diamond blue, 24 - filled triangle point-up blue, 25 - filled triangle point down blue."} shapes <- tibble( shape = c(0, 1, 2, 5, 3, 4, 6:19, 22, 21, 24, 23, 20), x = (0:24 %/% 5) / 2, @@ -289,7 +289,7 @@ In this case, it's usually easy to start from scratch again by pressing ESCAPE t One common problem when creating ggplot2 graphics is to put the `+` in the wrong place: it has to come at the end of the line, not the start. In other words, make sure you haven't accidentally written code like this: -``` {.r} +``` r ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) ``` @@ -399,7 +399,7 @@ If you prefer to not facet in the rows or columns dimension, use a `.` instead o How are these two plots similar? -```{r, echo = FALSE, out.width = "50%", fig.align="default", message = FALSE, fig.alt = "Two plots: the plot on the left is a scatterplot of highway fuel efficiency versus engine size of cars in ggplot2::mpg and the plot on the right shows a smooth curve that follows the trajectory of the relationship between these variables. A confidence interval around the smooth curve is also displayed."} +```{r, echo = FALSE, fig.width = 4, out.width = "50%", fig.align="default", message = FALSE, fig.alt = "Two plots: the plot on the left is a scatterplot of highway fuel efficiency versus engine size of cars in ggplot2::mpg and the plot on the right shows a smooth curve that follows the trajectory of the relationship between these variables. A confidence interval around the smooth curve is also displayed."} ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) @@ -682,7 +682,7 @@ There are three reasons you might need to use a stat explicitly: ggplot2 provides over 20 stats for you to use. Each stat is a function, so you can get help in the usual way, e.g. `?stat_bin`. -To see a complete list of stats, try the [ggplot2 cheatsheet](). +To see a complete list of stats, try the [ggplot2 cheatsheet](http://rstudio.com/resources/cheatsheets). ### Exercises