More minor page count tweaks & fixes
And re-convert with latest htmlbook
This commit is contained in:
		@@ -1,28 +1,18 @@
 | 
			
		||||
<section data-type="chapter" id="chp-layers">
 | 
			
		||||
<h1><span id="sec-layers" class="quarto-section-identifier d-none d-lg-block"><span class="chapter-title">Layers</span></span></h1>
 | 
			
		||||
<section id="introduction" data-type="sect1">
 | 
			
		||||
<section id="layers-introduction" data-type="sect1">
 | 
			
		||||
<h1>
 | 
			
		||||
Introduction</h1>
 | 
			
		||||
<p>In the <a href="#chp-data-visualize" data-type="xref">#chp-data-visualize</a>, you learned much more than just how to make scatterplots, bar charts, and boxplots. You learned a foundation that you can use to make <em>any</em> type of plot with ggplot2.</p>
 | 
			
		||||
<p>In this chapter, you’ll expand on that foundation as you learn about the layered grammar of graphics. We’ll start with a deeper dive into aesthetic mappings, geometric objects, and facets. Then, you will learn about statistical transformations ggplot2 makes under the hood when creating a plot. These transformations are used to calculate new values to plot, such as the heights of bars in a bar plot or medians in a box plot. You will also learn about position adjustments, which modify how geoms are displayed in your plots. Finally, we’ll briefly introduce coordinate systems.</p>
 | 
			
		||||
<p>We will not cover every single function and option for each of these layers, but we will walk you through the most important and commonly used functionality provided by ggplot2 as well as introduce you to packages that extend ggplot2.</p>
 | 
			
		||||
 | 
			
		||||
<section id="prerequisites" data-type="sect2">
 | 
			
		||||
<section id="layers-prerequisites" data-type="sect2">
 | 
			
		||||
<h2>
 | 
			
		||||
Prerequisites</h2>
 | 
			
		||||
<p>This chapter focuses on ggplot2, one of the core packages in the tidyverse. To access the datasets, help pages, and functions used in this chapter, load the tidyverse by running this code:</p>
 | 
			
		||||
<div class="cell">
 | 
			
		||||
<pre data-type="programlisting" data-code-language="r">library(tidyverse)
 | 
			
		||||
#> ── Attaching core tidyverse packages ──────────────── tidyverse 1.3.2.9000 ──
 | 
			
		||||
#> ✔ dplyr     1.0.99.9000     ✔ readr     2.1.3      
 | 
			
		||||
#> ✔ forcats   0.5.2.9000      ✔ stringr   1.5.0.9000 
 | 
			
		||||
#> ✔ ggplot2   3.4.0.9000      ✔ tibble    3.1.8      
 | 
			
		||||
#> ✔ lubridate 1.9.0           ✔ tidyr     1.2.1.9001 
 | 
			
		||||
#> ✔ purrr     1.0.1           
 | 
			
		||||
#> ── Conflicts ─────────────────────────────────────── tidyverse_conflicts() ──
 | 
			
		||||
#> ✖ dplyr::filter() masks stats::filter()
 | 
			
		||||
#> ✖ dplyr::lag()    masks stats::lag()
 | 
			
		||||
#> ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors</pre>
 | 
			
		||||
<pre data-type="programlisting" data-code-language="r">library(tidyverse)</pre>
 | 
			
		||||
</div>
 | 
			
		||||
</section>
 | 
			
		||||
</section>
 | 
			
		||||
@@ -37,15 +27,15 @@ Aesthetic mappings</h1>
 | 
			
		||||
<div class="cell">
 | 
			
		||||
<pre data-type="programlisting" data-code-language="r">mpg
 | 
			
		||||
#> # A tibble: 234 × 11
 | 
			
		||||
#>   manufacturer model displ  year   cyl trans    drv     cty   hwy fl    class
 | 
			
		||||
#>   <chr>        <chr> <dbl> <int> <int> <chr>    <chr> <int> <int> <chr> <chr>
 | 
			
		||||
#> 1 audi         a4      1.8  1999     4 auto(l5) f        18    29 p     comp…
 | 
			
		||||
#> 2 audi         a4      1.8  1999     4 manual(… f        21    29 p     comp…
 | 
			
		||||
#> 3 audi         a4      2    2008     4 manual(… f        20    31 p     comp…
 | 
			
		||||
#> 4 audi         a4      2    2008     4 auto(av) f        21    30 p     comp…
 | 
			
		||||
#> 5 audi         a4      2.8  1999     6 auto(l5) f        16    26 p     comp…
 | 
			
		||||
#> 6 audi         a4      2.8  1999     6 manual(… f        18    26 p     comp…
 | 
			
		||||
#> # … with 228 more rows</pre>
 | 
			
		||||
#>   manufacturer model displ  year   cyl trans      drv     cty   hwy fl   
 | 
			
		||||
#>   <chr>        <chr> <dbl> <int> <int> <chr>      <chr> <int> <int> <chr>
 | 
			
		||||
#> 1 audi         a4      1.8  1999     4 auto(l5)   f        18    29 p    
 | 
			
		||||
#> 2 audi         a4      1.8  1999     4 manual(m5) f        21    29 p    
 | 
			
		||||
#> 3 audi         a4      2    2008     4 manual(m6) f        20    31 p    
 | 
			
		||||
#> 4 audi         a4      2    2008     4 auto(av)   f        21    30 p    
 | 
			
		||||
#> 5 audi         a4      2.8  1999     6 auto(l5)   f        16    26 p    
 | 
			
		||||
#> 6 audi         a4      2.8  1999     6 manual(m5) f        18    26 p    
 | 
			
		||||
#> # … with 228 more rows, and 1 more variable: class <chr></pre>
 | 
			
		||||
</div>
 | 
			
		||||
<p>Among the variables in <code>mpg</code> are:</p>
 | 
			
		||||
<ol type="1"><li><p><code>displ</code>: A car’s engine size, in liters. A numerical variable.</p></li>
 | 
			
		||||
@@ -134,7 +124,7 @@ ggplot(mpg, aes(x = displ, y = hwy, alpha = class)) +
 | 
			
		||||
<p>So far we have discussed aesthetics that we can map or set in a scatterplot, when using a point geom. You can learn more about all possible aesthetic mappings in the aesthetic specifications vignette at <a href="https://ggplot2.tidyverse.org/articles/ggplot2-specs.html" class="uri">https://ggplot2.tidyverse.org/articles/ggplot2-specs.html</a>.</p>
 | 
			
		||||
<p>The specific aesthetics you can use for a plot depend on the geom you use to represent the data. In the next section we dive deeper into geoms.</p>
 | 
			
		||||
 | 
			
		||||
<section id="exercises" data-type="sect2">
 | 
			
		||||
<section id="layers-exercises" data-type="sect2">
 | 
			
		||||
<h2>
 | 
			
		||||
Exercises</h2>
 | 
			
		||||
<ol type="1"><li><p>Create a scatterplot of <code>hwy</code> vs. <code>displ</code> where the points are pink filled in triangles.</p></li>
 | 
			
		||||
@@ -285,7 +275,7 @@ ggplot(mpg, aes(x = hwy, y = drv, fill = drv, color = drv)) +
 | 
			
		||||
</div>
 | 
			
		||||
<p>The best place to get a comprehensive overview of all of the geoms ggplot2 offers, as well as all functions in the package, is the reference page: <a href="https://ggplot2.tidyverse.org/reference" class="uri">https://ggplot2.tidyverse.org/reference</a>. To learn more about any single geom, use the help (e.g. <code><a href="https://ggplot2.tidyverse.org/reference/geom_smooth.html">?geom_smooth</a></code>).</p>
 | 
			
		||||
 | 
			
		||||
<section id="exercises-1" data-type="sect2">
 | 
			
		||||
<section id="layers-exercises-1" data-type="sect2">
 | 
			
		||||
<h2>
 | 
			
		||||
Exercises</h2>
 | 
			
		||||
<ol type="1"><li><p>What geom would you use to draw a line chart? A boxplot? A histogram? An area chart?</p></li>
 | 
			
		||||
@@ -361,7 +351,7 @@ Facets</h1>
 | 
			
		||||
</div>
 | 
			
		||||
</div>
 | 
			
		||||
 | 
			
		||||
<section id="exercises-2" data-type="sect2">
 | 
			
		||||
<section id="layers-exercises-2" data-type="sect2">
 | 
			
		||||
<h2>
 | 
			
		||||
Exercises</h2>
 | 
			
		||||
<ol type="1"><li><p>What happens if you facet on a continuous variable?</p></li>
 | 
			
		||||
@@ -502,7 +492,7 @@ ggplot(cut_frequencies, aes(x = cut, y = freq)) +
 | 
			
		||||
</li>
 | 
			
		||||
</ol><p>ggplot2 provides more than 20 stats for you to use. Each stat is a function, so you can get help in the usual way, e.g. <code><a href="https://ggplot2.tidyverse.org/reference/geom_histogram.html">?stat_bin</a></code>.</p>
 | 
			
		||||
 | 
			
		||||
<section id="exercises-3" data-type="sect2">
 | 
			
		||||
<section id="layers-exercises-3" data-type="sect2">
 | 
			
		||||
<h2>
 | 
			
		||||
Exercises</h2>
 | 
			
		||||
<ol type="1"><li><p>What is the default geom associated with <code><a href="https://ggplot2.tidyverse.org/reference/stat_summary.html">stat_summary()</a></code>? How could you rewrite the previous plot to use that geom function instead of the stat function?</p></li>
 | 
			
		||||
@@ -608,7 +598,7 @@ ggplot(diamonds, aes(x = cut, color = clarity)) +
 | 
			
		||||
<p>Adding randomness seems like a strange way to improve your plot, but while it makes your graph less accurate at small scales, it makes your graph <em>more</em> revealing at large scales. Because this is such a useful operation, ggplot2 comes with a shorthand for <code>geom_point(position = "jitter")</code>: <code><a href="https://ggplot2.tidyverse.org/reference/geom_jitter.html">geom_jitter()</a></code>.</p>
 | 
			
		||||
<p>To learn more about a position adjustment, look up the help page associated with each adjustment: <code><a href="https://ggplot2.tidyverse.org/reference/position_dodge.html">?position_dodge</a></code>, <code><a href="https://ggplot2.tidyverse.org/reference/position_stack.html">?position_fill</a></code>, <code><a href="https://ggplot2.tidyverse.org/reference/position_identity.html">?position_identity</a></code>, <code><a href="https://ggplot2.tidyverse.org/reference/position_jitter.html">?position_jitter</a></code>, and <code><a href="https://ggplot2.tidyverse.org/reference/position_stack.html">?position_stack</a></code>.</p>
 | 
			
		||||
 | 
			
		||||
<section id="exercises-4" data-type="sect2">
 | 
			
		||||
<section id="layers-exercises-4" data-type="sect2">
 | 
			
		||||
<h2>
 | 
			
		||||
Exercises</h2>
 | 
			
		||||
<ol type="1"><li>
 | 
			
		||||
@@ -681,7 +671,7 @@ bar + coord_polar()</pre>
 | 
			
		||||
</div>
 | 
			
		||||
</li>
 | 
			
		||||
</ul>
 | 
			
		||||
<section id="exercises-5" data-type="sect2">
 | 
			
		||||
<section id="layers-exercises-5" data-type="sect2">
 | 
			
		||||
<h2>
 | 
			
		||||
Exercises</h2>
 | 
			
		||||
<ol type="1"><li><p>Turn a stacked bar chart into a pie chart using <code><a href="https://ggplot2.tidyverse.org/reference/coord_polar.html">coord_polar()</a></code>.</p></li>
 | 
			
		||||
@@ -726,7 +716,7 @@ The layered grammar of graphics</h1>
 | 
			
		||||
<p>If you’d like to learn more about the theoretical underpinnings of ggplot2, you might enjoy reading “<a href="https://vita.had.co.nz/papers/layered-grammar.pdf">The Layered Grammar of Graphics</a>”, the scientific paper that describes the theory of ggplot2 in detail.</p>
 | 
			
		||||
</section>
 | 
			
		||||
 | 
			
		||||
<section id="summary" data-type="sect1">
 | 
			
		||||
<section id="layers-summary" data-type="sect1">
 | 
			
		||||
<h1>
 | 
			
		||||
Summary</h1>
 | 
			
		||||
<p>In this chapter you learned about the layered grammar of graphics starting with aesthetics and geometries to build a simple plot, facets for splitting the plot into subsets, statistics for understanding how geoms are calculated, position adjustments for controlling the fine details of position when geoms might otherwise overlap, and coordinate systems allow you fundamentally change what <code>x</code> and <code>y</code> mean. One layer we have not yet touched on is theme, which we will introduce in <a href="#sec-themes" data-type="xref">#sec-themes</a>.</p>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user