Don't transform non-crossref links
This commit is contained in:
		@@ -13,12 +13,12 @@
 | 
			
		||||
Introduction</h1>
 | 
			
		||||
<p>In <a href="#chp-EDA" data-type="xref">#chp-EDA</a>, you learned how to use plots as tools for <em>exploration</em>. When you make exploratory plots, you know—even before looking—which variables the plot will display. You made each plot for a purpose, could quickly look at it, and then move on to the next plot. In the course of most analyses, you’ll produce tens or hundreds of plots, most of which are immediately thrown away.</p>
 | 
			
		||||
<p>Now that you understand your data, you need to <em>communicate</em> your understanding to others. Your audience will likely not share your background knowledge and will not be deeply invested in the data. To help others quickly build up a good mental model of the data, you will need to invest considerable effort in making your plots as self-explanatory as possible. In this chapter, you’ll learn some of the tools that ggplot2 provides to do so.</p>
 | 
			
		||||
<p>This chapter focuses on the tools you need to create good graphics. We assume that you know what you want, and just need to know how to do it. For that reason, we highly recommend pairing this chapter with a good general visualization book. We particularly like <a href="#chp-https://www.amazon.com/gp/product/0321934075/" data-type="xref">#chp-https://www.amazon.com/gp/product/0321934075/</a>, by Albert Cairo. It doesn’t teach the mechanics of creating visualizations, but instead focuses on what you need to think about in order to create effective graphics.</p>
 | 
			
		||||
<p>This chapter focuses on the tools you need to create good graphics. We assume that you know what you want, and just need to know how to do it. For that reason, we highly recommend pairing this chapter with a good general visualization book. We particularly like <a href="https://www.amazon.com/gp/product/0321934075/"><em>The Truthful Art</em></a>, by Albert Cairo. It doesn’t teach the mechanics of creating visualizations, but instead focuses on what you need to think about in order to create effective graphics.</p>
 | 
			
		||||
 | 
			
		||||
<section id="prerequisites" data-type="sect2">
 | 
			
		||||
<h2>
 | 
			
		||||
Prerequisites</h2>
 | 
			
		||||
<p>In this chapter, we’ll focus once again on ggplot2. We’ll also use a little dplyr for data manipulation, and a few ggplot2 extension packages, including <strong>ggrepel</strong> and <strong>patchwork</strong>. Rather than loading those extensions here, we’ll refer to their functions explicitly, using the <code>::</code> notation. This will help make it clear which functions are built into ggplot2, and which come from other packages. Don’t forget you’ll need to install those packages with <code><a href="#chp-https://rdrr.io/r/utils/install.packages" data-type="xref">#chp-https://rdrr.io/r/utils/install.packages</a></code> if you don’t already have them.</p>
 | 
			
		||||
<p>In this chapter, we’ll focus once again on ggplot2. We’ll also use a little dplyr for data manipulation, and a few ggplot2 extension packages, including <strong>ggrepel</strong> and <strong>patchwork</strong>. Rather than loading those extensions here, we’ll refer to their functions explicitly, using the <code>::</code> notation. This will help make it clear which functions are built into ggplot2, and which come from other packages. Don’t forget you’ll need to install those packages with <code><a href="https://rdrr.io/r/utils/install.packages.html">install.packages()</a></code> if you don’t already have them.</p>
 | 
			
		||||
<div class="cell">
 | 
			
		||||
<pre data-type="programlisting" data-code-language="downlit">library(tidyverse)</pre>
 | 
			
		||||
</div>
 | 
			
		||||
@@ -28,7 +28,7 @@ Prerequisites</h2>
 | 
			
		||||
<section id="label" data-type="sect1">
 | 
			
		||||
<h1>
 | 
			
		||||
Label</h1>
 | 
			
		||||
<p>The easiest place to start when turning an exploratory graphic into an expository graphic is with good labels. You add labels with the <code><a href="#chp-https://ggplot2.tidyverse.org/reference/labs" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/labs</a></code> function. This example adds a plot title:</p>
 | 
			
		||||
<p>The easiest place to start when turning an exploratory graphic into an expository graphic is with good labels. You add labels with the <code><a href="https://ggplot2.tidyverse.org/reference/labs.html">labs()</a></code> function. This example adds a plot title:</p>
 | 
			
		||||
<div class="cell">
 | 
			
		||||
<pre data-type="programlisting" data-code-language="downlit">ggplot(mpg, aes(displ, hwy)) +
 | 
			
		||||
  geom_point(aes(color = class)) +
 | 
			
		||||
@@ -55,7 +55,7 @@ Label</h1>
 | 
			
		||||
<p><img src="communicate-plots_files/figure-html/unnamed-chunk-4-1.png" width="576"/></p>
 | 
			
		||||
</div>
 | 
			
		||||
</div>
 | 
			
		||||
<p>You can also use <code><a href="#chp-https://ggplot2.tidyverse.org/reference/labs" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/labs</a></code> to replace the axis and legend titles. It’s usually a good idea to replace short variable names with more detailed descriptions, and to include the units.</p>
 | 
			
		||||
<p>You can also use <code><a href="https://ggplot2.tidyverse.org/reference/labs.html">labs()</a></code> to replace the axis and legend titles. It’s usually a good idea to replace short variable names with more detailed descriptions, and to include the units.</p>
 | 
			
		||||
<div class="cell">
 | 
			
		||||
<pre data-type="programlisting" data-code-language="downlit">ggplot(mpg, aes(displ, hwy)) +
 | 
			
		||||
  geom_point(aes(colour = class)) +
 | 
			
		||||
@@ -69,7 +69,7 @@ Label</h1>
 | 
			
		||||
<p><img src="communicate-plots_files/figure-html/unnamed-chunk-5-1.png" width="576"/></p>
 | 
			
		||||
</div>
 | 
			
		||||
</div>
 | 
			
		||||
<p>It’s possible to use mathematical equations instead of text strings. Just switch <code>""</code> out for <code><a href="#chp-https://rdrr.io/r/base/substitute" data-type="xref">#chp-https://rdrr.io/r/base/substitute</a></code> and read about the available options in <code><a href="#chp-https://rdrr.io/r/grDevices/plotmath" data-type="xref">#chp-https://rdrr.io/r/grDevices/plotmath</a></code>:</p>
 | 
			
		||||
<p>It’s possible to use mathematical equations instead of text strings. Just switch <code>""</code> out for <code><a href="https://rdrr.io/r/base/substitute.html">quote()</a></code> and read about the available options in <code><a href="https://rdrr.io/r/grDevices/plotmath.html">?plotmath</a></code>:</p>
 | 
			
		||||
<div class="cell">
 | 
			
		||||
<pre data-type="programlisting" data-code-language="downlit">df <- tibble(
 | 
			
		||||
  x = runif(10),
 | 
			
		||||
@@ -105,7 +105,7 @@ Exercises</h2>
 | 
			
		||||
<section id="annotations" data-type="sect1">
 | 
			
		||||
<h1>
 | 
			
		||||
Annotations</h1>
 | 
			
		||||
<p>In addition to labelling major components of your plot, it’s often useful to label individual observations or groups of observations. The first tool you have at your disposal is <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_text" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_text</a></code>. <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_text" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_text</a></code> is similar to <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_point" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_point</a></code>, but it has an additional aesthetic: <code>label</code>. This makes it possible to add textual labels to your plots.</p>
 | 
			
		||||
<p>In addition to labelling major components of your plot, it’s often useful to label individual observations or groups of observations. The first tool you have at your disposal is <code><a href="https://ggplot2.tidyverse.org/reference/geom_text.html">geom_text()</a></code>. <code><a href="https://ggplot2.tidyverse.org/reference/geom_text.html">geom_text()</a></code> is similar to <code><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point()</a></code>, but it has an additional aesthetic: <code>label</code>. This makes it possible to add textual labels to your plots.</p>
 | 
			
		||||
<p>There are two possible sources of labels. First, you might have a tibble that provides labels. The plot below isn’t terribly useful, but it illustrates a useful approach: pull out the most efficient car in each class with dplyr, and then label it on the plot:</p>
 | 
			
		||||
<div class="cell">
 | 
			
		||||
<pre data-type="programlisting" data-code-language="downlit">best_in_class <- mpg |>
 | 
			
		||||
@@ -119,7 +119,7 @@ ggplot(mpg, aes(displ, hwy)) +
 | 
			
		||||
<p><img src="communicate-plots_files/figure-html/unnamed-chunk-8-1.png" width="576"/></p>
 | 
			
		||||
</div>
 | 
			
		||||
</div>
 | 
			
		||||
<p>This is hard to read because the labels overlap with each other, and with the points. We can make things a little better by switching to <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_text" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_text</a></code> which draws a rectangle behind the text. We also use the <code>nudge_y</code> parameter to move the labels slightly above the corresponding points:</p>
 | 
			
		||||
<p>This is hard to read because the labels overlap with each other, and with the points. We can make things a little better by switching to <code><a href="https://ggplot2.tidyverse.org/reference/geom_text.html">geom_label()</a></code> which draws a rectangle behind the text. We also use the <code>nudge_y</code> parameter to move the labels slightly above the corresponding points:</p>
 | 
			
		||||
<div class="cell">
 | 
			
		||||
<pre data-type="programlisting" data-code-language="downlit">ggplot(mpg, aes(displ, hwy)) +
 | 
			
		||||
  geom_point(aes(colour = class)) +
 | 
			
		||||
@@ -161,7 +161,7 @@ ggplot(mpg, aes(displ, hwy, colour = class)) +
 | 
			
		||||
<p><img src="communicate-plots_files/figure-html/unnamed-chunk-11-1.png" width="576"/></p>
 | 
			
		||||
</div>
 | 
			
		||||
</div>
 | 
			
		||||
<p>Alternatively, you might just want to add a single label to the plot, but you’ll still need to create a data frame. Often, you want the label in the corner of the plot, so it’s convenient to create a new data frame using <code><a href="#chp-https://dplyr.tidyverse.org/reference/summarise" data-type="xref">#chp-https://dplyr.tidyverse.org/reference/summarise</a></code> to compute the maximum values of x and y.</p>
 | 
			
		||||
<p>Alternatively, you might just want to add a single label to the plot, but you’ll still need to create a data frame. Often, you want the label in the corner of the plot, so it’s convenient to create a new data frame using <code><a href="https://dplyr.tidyverse.org/reference/summarise.html">summarize()</a></code> to compute the maximum values of x and y.</p>
 | 
			
		||||
<div class="cell">
 | 
			
		||||
<pre data-type="programlisting" data-code-language="downlit">label_info <- mpg |>
 | 
			
		||||
  summarise(
 | 
			
		||||
@@ -177,7 +177,7 @@ ggplot(mpg, aes(displ, hwy)) +
 | 
			
		||||
<p><img src="communicate-plots_files/figure-html/unnamed-chunk-12-1.png" width="576"/></p>
 | 
			
		||||
</div>
 | 
			
		||||
</div>
 | 
			
		||||
<p>If you want to place the text exactly on the borders of the plot, you can use <code>+Inf</code> and <code>-Inf</code>. Since we’re no longer computing the positions from <code>mpg</code>, we can use <code><a href="#chp-https://tibble.tidyverse.org/reference/tibble" data-type="xref">#chp-https://tibble.tidyverse.org/reference/tibble</a></code> to create the data frame:</p>
 | 
			
		||||
<p>If you want to place the text exactly on the borders of the plot, you can use <code>+Inf</code> and <code>-Inf</code>. Since we’re no longer computing the positions from <code>mpg</code>, we can use <code><a href="https://tibble.tidyverse.org/reference/tibble.html">tibble()</a></code> to create the data frame:</p>
 | 
			
		||||
<div class="cell">
 | 
			
		||||
<pre data-type="programlisting" data-code-language="downlit">label_info <- tibble(
 | 
			
		||||
  displ = Inf,
 | 
			
		||||
@@ -192,7 +192,7 @@ ggplot(mpg, aes(displ, hwy)) +
 | 
			
		||||
<p><img src="communicate-plots_files/figure-html/unnamed-chunk-13-1.png" width="576"/></p>
 | 
			
		||||
</div>
 | 
			
		||||
</div>
 | 
			
		||||
<p>In these examples, we manually broke the label up into lines using <code>"\n"</code>. Another approach is to use <code><a href="#chp-https://stringr.tidyverse.org/reference/str_wrap" data-type="xref">#chp-https://stringr.tidyverse.org/reference/str_wrap</a></code> to automatically add line breaks, given the number of characters you want per line:</p>
 | 
			
		||||
<p>In these examples, we manually broke the label up into lines using <code>"\n"</code>. Another approach is to use <code><a href="https://stringr.tidyverse.org/reference/str_wrap.html">stringr::str_wrap()</a></code> to automatically add line breaks, given the number of characters you want per line:</p>
 | 
			
		||||
<div class="cell">
 | 
			
		||||
<pre data-type="programlisting" data-code-language="downlit">"Increasing engine size is related to decreasing fuel economy." |>
 | 
			
		||||
  str_wrap(width = 40) |>
 | 
			
		||||
@@ -209,20 +209,20 @@ ggplot(mpg, aes(displ, hwy)) +
 | 
			
		||||
</figure>
 | 
			
		||||
</div>
 | 
			
		||||
</div>
 | 
			
		||||
<p>Remember, in addition to <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_text" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_text</a></code>, you have many other geoms in ggplot2 available to help annotate your plot. A few ideas:</p>
 | 
			
		||||
<ul><li><p>Use <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_abline" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_abline</a></code> and <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_abline" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_abline</a></code> to add reference lines. We often make them thick (<code>size = 2</code>) and white (<code>colour = white</code>), and draw them underneath the primary data layer. That makes them easy to see, without drawing attention away from the data.</p></li>
 | 
			
		||||
<li><p>Use <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_tile" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_tile</a></code> to draw a rectangle around points of interest. The boundaries of the rectangle are defined by aesthetics <code>xmin</code>, <code>xmax</code>, <code>ymin</code>, <code>ymax</code>.</p></li>
 | 
			
		||||
<li><p>Use <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_segment" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_segment</a></code> with the <code>arrow</code> argument to draw attention to a point with an arrow. Use aesthetics <code>x</code> and <code>y</code> to define the starting location, and <code>xend</code> and <code>yend</code> to define the end location.</p></li>
 | 
			
		||||
<p>Remember, in addition to <code><a href="https://ggplot2.tidyverse.org/reference/geom_text.html">geom_text()</a></code>, you have many other geoms in ggplot2 available to help annotate your plot. A few ideas:</p>
 | 
			
		||||
<ul><li><p>Use <code><a href="https://ggplot2.tidyverse.org/reference/geom_abline.html">geom_hline()</a></code> and <code><a href="https://ggplot2.tidyverse.org/reference/geom_abline.html">geom_vline()</a></code> to add reference lines. We often make them thick (<code>size = 2</code>) and white (<code>colour = white</code>), and draw them underneath the primary data layer. That makes them easy to see, without drawing attention away from the data.</p></li>
 | 
			
		||||
<li><p>Use <code><a href="https://ggplot2.tidyverse.org/reference/geom_tile.html">geom_rect()</a></code> to draw a rectangle around points of interest. The boundaries of the rectangle are defined by aesthetics <code>xmin</code>, <code>xmax</code>, <code>ymin</code>, <code>ymax</code>.</p></li>
 | 
			
		||||
<li><p>Use <code><a href="https://ggplot2.tidyverse.org/reference/geom_segment.html">geom_segment()</a></code> with the <code>arrow</code> argument to draw attention to a point with an arrow. Use aesthetics <code>x</code> and <code>y</code> to define the starting location, and <code>xend</code> and <code>yend</code> to define the end location.</p></li>
 | 
			
		||||
</ul><p>The only limit is your imagination (and your patience with positioning annotations to be aesthetically pleasing)!</p>
 | 
			
		||||
 | 
			
		||||
<section id="exercises-1" data-type="sect2">
 | 
			
		||||
<h2>
 | 
			
		||||
Exercises</h2>
 | 
			
		||||
<ol type="1"><li><p>Use <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_text" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_text</a></code> with infinite positions to place text at the four corners of the plot.</p></li>
 | 
			
		||||
<li><p>Read the documentation for <code><a href="#chp-https://ggplot2.tidyverse.org/reference/annotate" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/annotate</a></code>. How can you use it to add a text label to a plot without having to create a tibble?</p></li>
 | 
			
		||||
<li><p>How do labels with <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_text" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_text</a></code> interact with faceting? How can you add a label to a single facet? How can you put a different label in each facet? (Hint: Think about the underlying data.)</p></li>
 | 
			
		||||
<li><p>What arguments to <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_text" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_text</a></code> control the appearance of the background box?</p></li>
 | 
			
		||||
<li><p>What are the four arguments to <code><a href="#chp-https://rdrr.io/r/grid/arrow" data-type="xref">#chp-https://rdrr.io/r/grid/arrow</a></code>? How do they work? Create a series of plots that demonstrate the most important options.</p></li>
 | 
			
		||||
<ol type="1"><li><p>Use <code><a href="https://ggplot2.tidyverse.org/reference/geom_text.html">geom_text()</a></code> with infinite positions to place text at the four corners of the plot.</p></li>
 | 
			
		||||
<li><p>Read the documentation for <code><a href="https://ggplot2.tidyverse.org/reference/annotate.html">annotate()</a></code>. How can you use it to add a text label to a plot without having to create a tibble?</p></li>
 | 
			
		||||
<li><p>How do labels with <code><a href="https://ggplot2.tidyverse.org/reference/geom_text.html">geom_text()</a></code> interact with faceting? How can you add a label to a single facet? How can you put a different label in each facet? (Hint: Think about the underlying data.)</p></li>
 | 
			
		||||
<li><p>What arguments to <code><a href="https://ggplot2.tidyverse.org/reference/geom_text.html">geom_label()</a></code> control the appearance of the background box?</p></li>
 | 
			
		||||
<li><p>What are the four arguments to <code><a href="https://rdrr.io/r/grid/arrow.html">arrow()</a></code>? How do they work? Create a series of plots that demonstrate the most important options.</p></li>
 | 
			
		||||
</ol></section>
 | 
			
		||||
</section>
 | 
			
		||||
 | 
			
		||||
@@ -283,7 +283,7 @@ Axis ticks and legend keys</h2>
 | 
			
		||||
</div>
 | 
			
		||||
</div>
 | 
			
		||||
<p>Note that the specification of breaks and labels for date and datetime scales is a little different:</p>
 | 
			
		||||
<ul><li><p><code>date_labels</code> takes a format specification, in the same form as <code><a href="#chp-https://readr.tidyverse.org/reference/parse_datetime" data-type="xref">#chp-https://readr.tidyverse.org/reference/parse_datetime</a></code>.</p></li>
 | 
			
		||||
<ul><li><p><code>date_labels</code> takes a format specification, in the same form as <code><a href="https://readr.tidyverse.org/reference/parse_datetime.html">parse_datetime()</a></code>.</p></li>
 | 
			
		||||
<li><p><code>date_breaks</code> (not shown here), takes a string like “2 days” or “1 month”.</p></li>
 | 
			
		||||
</ul></section>
 | 
			
		||||
 | 
			
		||||
@@ -291,7 +291,7 @@ Axis ticks and legend keys</h2>
 | 
			
		||||
<h2>
 | 
			
		||||
Legend layout</h2>
 | 
			
		||||
<p>You will most often use <code>breaks</code> and <code>labels</code> to tweak the axes. While they both also work for legends, there are a few other techniques you are more likely to use.</p>
 | 
			
		||||
<p>To control the overall position of the legend, you need to use a <code><a href="#chp-https://ggplot2.tidyverse.org/reference/theme" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/theme</a></code> setting. We’ll come back to themes at the end of the chapter, but in brief, they control the non-data parts of the plot. The theme setting <code>legend.position</code> controls where the legend is drawn:</p>
 | 
			
		||||
<p>To control the overall position of the legend, you need to use a <code><a href="https://ggplot2.tidyverse.org/reference/theme.html">theme()</a></code> setting. We’ll come back to themes at the end of the chapter, but in brief, they control the non-data parts of the plot. The theme setting <code>legend.position</code> controls where the legend is drawn:</p>
 | 
			
		||||
<div>
 | 
			
		||||
<pre data-type="programlisting" data-code-language="downlit">base <- ggplot(mpg, aes(displ, hwy)) +
 | 
			
		||||
  geom_point(aes(colour = class))
 | 
			
		||||
@@ -320,7 +320,7 @@ base + theme(legend.position = "right") # the default</pre>
 | 
			
		||||
</div>
 | 
			
		||||
</div>
 | 
			
		||||
<p>You can also use <code>legend.position = "none"</code> to suppress the display of the legend altogether.</p>
 | 
			
		||||
<p>To control the display of individual legends, use <code><a href="#chp-https://ggplot2.tidyverse.org/reference/guides" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/guides</a></code> along with <code><a href="#chp-https://ggplot2.tidyverse.org/reference/guide_legend" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/guide_legend</a></code> or <code><a href="#chp-https://ggplot2.tidyverse.org/reference/guide_colourbar" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/guide_colourbar</a></code>. The following example shows two important settings: controlling the number of rows the legend uses with <code>nrow</code>, and overriding one of the aesthetics to make the points bigger. This is particularly useful if you have used a low <code>alpha</code> to display many points on a plot.</p>
 | 
			
		||||
<p>To control the display of individual legends, use <code><a href="https://ggplot2.tidyverse.org/reference/guides.html">guides()</a></code> along with <code><a href="https://ggplot2.tidyverse.org/reference/guide_legend.html">guide_legend()</a></code> or <code><a href="https://ggplot2.tidyverse.org/reference/guide_colourbar.html">guide_colorbar()</a></code>. The following example shows two important settings: controlling the number of rows the legend uses with <code>nrow</code>, and overriding one of the aesthetics to make the points bigger. This is particularly useful if you have used a low <code>alpha</code> to display many points on a plot.</p>
 | 
			
		||||
<div class="cell">
 | 
			
		||||
<pre data-type="programlisting" data-code-language="downlit">ggplot(mpg, aes(displ, hwy)) +
 | 
			
		||||
  geom_point(aes(colour = class)) +
 | 
			
		||||
@@ -394,7 +394,7 @@ ggplot(mpg, aes(displ, hwy)) +
 | 
			
		||||
<p><img src="communicate-plots_files/figure-html/unnamed-chunk-26-1.png" width="576"/></p>
 | 
			
		||||
</div>
 | 
			
		||||
</div>
 | 
			
		||||
<p>The ColorBrewer scales are documented online at <a href="https://colorbrewer2.org/" class="uri">https://colorbrewer2.org/</a> and made available in R via the <strong>RColorBrewer</strong> package, by Erich Neuwirth. <a href="#fig-brewer" data-type="xref">#fig-brewer</a> shows the complete list of all palettes. The sequential (top) and diverging (bottom) palettes are particularly useful if your categorical values are ordered, or have a “middle”. This often arises if you’ve used <code><a href="#chp-https://rdrr.io/r/base/cut" data-type="xref">#chp-https://rdrr.io/r/base/cut</a></code> to make a continuous variable into a categorical variable.</p>
 | 
			
		||||
<p>The ColorBrewer scales are documented online at <a href="https://colorbrewer2.org/" class="uri">https://colorbrewer2.org/</a> and made available in R via the <strong>RColorBrewer</strong> package, by Erich Neuwirth. <a href="#fig-brewer" data-type="xref">#fig-brewer</a> shows the complete list of all palettes. The sequential (top) and diverging (bottom) palettes are particularly useful if your categorical values are ordered, or have a “middle”. This often arises if you’ve used <code><a href="https://rdrr.io/r/base/cut.html">cut()</a></code> to make a continuous variable into a categorical variable.</p>
 | 
			
		||||
<div class="cell">
 | 
			
		||||
<div class="cell-output-display">
 | 
			
		||||
 | 
			
		||||
@@ -403,7 +403,7 @@ ggplot(mpg, aes(displ, hwy)) +
 | 
			
		||||
</figure>
 | 
			
		||||
</div>
 | 
			
		||||
</div>
 | 
			
		||||
<p>When you have a predefined mapping between values and colors, use <code><a href="#chp-https://ggplot2.tidyverse.org/reference/scale_manual" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/scale_manual</a></code>. For example, if we map presidential party to colour, we want to use the standard mapping of red for Republicans and blue for Democrats:</p>
 | 
			
		||||
<p>When you have a predefined mapping between values and colors, use <code><a href="https://ggplot2.tidyverse.org/reference/scale_manual.html">scale_colour_manual()</a></code>. For example, if we map presidential party to colour, we want to use the standard mapping of red for Republicans and blue for Democrats:</p>
 | 
			
		||||
<div class="cell">
 | 
			
		||||
<pre data-type="programlisting" data-code-language="downlit">presidential |>
 | 
			
		||||
  mutate(id = 33 + row_number()) |>
 | 
			
		||||
@@ -415,7 +415,7 @@ ggplot(mpg, aes(displ, hwy)) +
 | 
			
		||||
<p><img src="communicate-plots_files/figure-html/unnamed-chunk-28-1.png" width="576"/></p>
 | 
			
		||||
</div>
 | 
			
		||||
</div>
 | 
			
		||||
<p>For continuous colour, you can use the built-in <code><a href="#chp-https://ggplot2.tidyverse.org/reference/scale_gradient" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/scale_gradient</a></code> or <code><a href="#chp-https://ggplot2.tidyverse.org/reference/scale_gradient" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/scale_gradient</a></code>. If you have a diverging scale, you can use <code><a href="#chp-https://ggplot2.tidyverse.org/reference/scale_gradient" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/scale_gradient</a></code>. That allows you to give, for example, positive and negative values different colors. That’s sometimes also useful if you want to distinguish points above or below the mean.</p>
 | 
			
		||||
<p>For continuous colour, you can use the built-in <code><a href="https://ggplot2.tidyverse.org/reference/scale_gradient.html">scale_colour_gradient()</a></code> or <code><a href="https://ggplot2.tidyverse.org/reference/scale_gradient.html">scale_fill_gradient()</a></code>. If you have a diverging scale, you can use <code><a href="https://ggplot2.tidyverse.org/reference/scale_gradient.html">scale_colour_gradient2()</a></code>. That allows you to give, for example, positive and negative values different colors. That’s sometimes also useful if you want to distinguish points above or below the mean.</p>
 | 
			
		||||
<p>Another option is to use the viridis color scales. The designers, Nathaniel Smith and Stéfan van der Walt, carefully tailored continuous colour schemes that are perceptible to people with various forms of colour blindness as well as perceptually uniform in both color and black and white. These scales are available as continuous (<code>c</code>), discrete (<code>d</code>), and binned (<code>b</code>) palettes in ggplot2.</p>
 | 
			
		||||
<div>
 | 
			
		||||
<pre data-type="programlisting" data-code-language="downlit">df <- tibble(
 | 
			
		||||
@@ -469,7 +469,7 @@ Exercises</h2>
 | 
			
		||||
  coord_fixed()</pre>
 | 
			
		||||
</div>
 | 
			
		||||
</li>
 | 
			
		||||
<li><p>What is the first argument to every scale? How does it compare to <code><a href="#chp-https://ggplot2.tidyverse.org/reference/labs" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/labs</a></code>?</p></li>
 | 
			
		||||
<li><p>What is the first argument to every scale? How does it compare to <code><a href="https://ggplot2.tidyverse.org/reference/labs.html">labs()</a></code>?</p></li>
 | 
			
		||||
<li>
 | 
			
		||||
<p>Change the display of the presidential terms by:</p>
 | 
			
		||||
<ol type="a"><li>Combining the two variants shown above.</li>
 | 
			
		||||
@@ -497,9 +497,9 @@ Zooming</h1>
 | 
			
		||||
<p>There are three ways to control the plot limits:</p>
 | 
			
		||||
<ol type="1"><li>Adjusting what data are plotted</li>
 | 
			
		||||
<li>Setting the limits in each scale</li>
 | 
			
		||||
<li>Setting <code>xlim</code> and <code>ylim</code> in <code><a href="#chp-https://ggplot2.tidyverse.org/reference/coord_cartesian" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/coord_cartesian</a></code>
 | 
			
		||||
<li>Setting <code>xlim</code> and <code>ylim</code> in <code><a href="https://ggplot2.tidyverse.org/reference/coord_cartesian.html">coord_cartesian()</a></code>
 | 
			
		||||
</li>
 | 
			
		||||
</ol><p>To zoom in on a region of the plot, it’s generally best to use <code><a href="#chp-https://ggplot2.tidyverse.org/reference/coord_cartesian" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/coord_cartesian</a></code>. Compare the following two plots:</p>
 | 
			
		||||
</ol><p>To zoom in on a region of the plot, it’s generally best to use <code><a href="https://ggplot2.tidyverse.org/reference/coord_cartesian.html">coord_cartesian()</a></code>. Compare the following two plots:</p>
 | 
			
		||||
<div>
 | 
			
		||||
<pre data-type="programlisting" data-code-language="downlit">ggplot(mpg, mapping = aes(displ, hwy)) +
 | 
			
		||||
  geom_point(aes(color = class)) +
 | 
			
		||||
@@ -597,26 +597,26 @@ Themes</h1>
 | 
			
		||||
</div>
 | 
			
		||||
</div>
 | 
			
		||||
<p>Many people wonder why the default theme has a gray background. This was a deliberate choice because it puts the data forward while still making the grid lines visible. The white grid lines are visible (which is important because they significantly aid position judgments), but they have little visual impact and we can easily tune them out. The grey background gives the plot a similar typographic colour to the text, ensuring that the graphics fit in with the flow of a document without jumping out with a bright white background. Finally, the grey background creates a continuous field of colour which ensures that the plot is perceived as a single visual entity.</p>
 | 
			
		||||
<p>It’s also possible to control individual components of each theme, like the size and colour of the font used for the y axis. Unfortunately, this level of detail is outside the scope of this book, so you’ll need to read the <a href="#chp-https://ggplot2-book.org/" data-type="xref">#chp-https://ggplot2-book.org/</a> for the full details. You can also create your own themes, if you are trying to match a particular corporate or journal style.</p>
 | 
			
		||||
<p>It’s also possible to control individual components of each theme, like the size and colour of the font used for the y axis. Unfortunately, this level of detail is outside the scope of this book, so you’ll need to read the <a href="https://ggplot2-book.org/">ggplot2 book</a> for the full details. You can also create your own themes, if you are trying to match a particular corporate or journal style.</p>
 | 
			
		||||
</section>
 | 
			
		||||
 | 
			
		||||
<section id="sec-ggsave" data-type="sect1">
 | 
			
		||||
<h1>
 | 
			
		||||
Saving your plots</h1>
 | 
			
		||||
<p>There are two main ways to get your plots out of R and into your final write-up: <code><a href="#chp-https://ggplot2.tidyverse.org/reference/ggsave" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/ggsave</a></code> and knitr. <code><a href="#chp-https://ggplot2.tidyverse.org/reference/ggsave" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/ggsave</a></code> will save the most recent plot to disk:</p>
 | 
			
		||||
<p>There are two main ways to get your plots out of R and into your final write-up: <code><a href="https://ggplot2.tidyverse.org/reference/ggsave.html">ggsave()</a></code> and knitr. <code><a href="https://ggplot2.tidyverse.org/reference/ggsave.html">ggsave()</a></code> will save the most recent plot to disk:</p>
 | 
			
		||||
<div class="cell">
 | 
			
		||||
<pre data-type="programlisting" data-code-language="downlit">ggplot(mpg, aes(displ, hwy)) + geom_point()
 | 
			
		||||
ggsave("my-plot.pdf")
 | 
			
		||||
#> Saving 6 x 4 in image</pre>
 | 
			
		||||
</div>
 | 
			
		||||
<p>If you don’t specify the <code>width</code> and <code>height</code> they will be taken from the dimensions of the current plotting device. For reproducible code, you’ll want to specify them.</p>
 | 
			
		||||
<p>Generally, however, we recommend that you assemble your final reports using Quarto, so we focus on the important code chunk options that you should know about for graphics. You can learn more about <code><a href="#chp-https://ggplot2.tidyverse.org/reference/ggsave" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/ggsave</a></code> in the documentation.</p>
 | 
			
		||||
<p>Generally, however, we recommend that you assemble your final reports using Quarto, so we focus on the important code chunk options that you should know about for graphics. You can learn more about <code><a href="https://ggplot2.tidyverse.org/reference/ggsave.html">ggsave()</a></code> in the documentation.</p>
 | 
			
		||||
</section>
 | 
			
		||||
 | 
			
		||||
<section id="learning-more" data-type="sect1">
 | 
			
		||||
<h1>
 | 
			
		||||
Learning more</h1>
 | 
			
		||||
<p>The absolute best place to learn more is the ggplot2 book: <a href="#chp-https://ggplot2-book.org/" data-type="xref">#chp-https://ggplot2-book.org/</a>. It goes into much more depth about the underlying theory, and has many more examples of how to combine the individual pieces to solve practical problems.</p>
 | 
			
		||||
<p>The absolute best place to learn more is the ggplot2 book: <a href="https://ggplot2-book.org/"><em>ggplot2: Elegant graphics for data analysis</em></a>. It goes into much more depth about the underlying theory, and has many more examples of how to combine the individual pieces to solve practical problems.</p>
 | 
			
		||||
<p>Another great resource is the ggplot2 extensions gallery <a href="https://exts.ggplot2.tidyverse.org/gallery/" class="uri">https://exts.ggplot2.tidyverse.org/gallery/</a>. This site lists many of the packages that extend ggplot2 with new geoms and scales. It’s a great place to start if you’re trying to do something that seems hard with ggplot2.</p>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user