Don't transform non-crossref links
This commit is contained in:
@@ -24,7 +24,7 @@ Prerequisites</h2>
|
||||
#> ✖ dplyr::lag() masks stats::lag()</pre>
|
||||
</div>
|
||||
<p>That one line of code loads the core tidyverse; packages which you will use in almost every data analysis. It also tells you which functions from the tidyverse conflict with functions in base R (or from other packages you might have loaded).</p>
|
||||
<p>If you run this code and get the error message “there is no package called ‘tidyverse’”, you’ll need to first install it, then run <code><a href="#chp-https://rdrr.io/r/base/library" data-type="xref">#chp-https://rdrr.io/r/base/library</a></code> once again.</p>
|
||||
<p>If you run this code and get the error message “there is no package called ‘tidyverse’”, you’ll need to first install it, then run <code><a href="https://rdrr.io/r/base/library.html">library()</a></code> once again.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">install.packages("tidyverse")
|
||||
library(tidyverse)</pre>
|
||||
@@ -41,7 +41,7 @@ First steps</h1>
|
||||
<section id="the-mpg-data-frame" data-type="sect2">
|
||||
<h2>
|
||||
The<code>mpg</code> data frame</h2>
|
||||
<p>You can test your answer with the <code>mpg</code> <strong>data frame</strong> found in ggplot2 (a.k.a. <code><a href="#chp-https://ggplot2.tidyverse.org/reference/mpg" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/mpg</a></code>). A data frame is a rectangular collection of variables (in the columns) and observations (in the rows). <code>mpg</code> contains observations collected by the US Environmental Protection Agency on 38 car models.</p>
|
||||
<p>You can test your answer with the <code>mpg</code> <strong>data frame</strong> found in ggplot2 (a.k.a. <code><a href="https://ggplot2.tidyverse.org/reference/mpg.html">ggplot2::mpg</a></code>). A data frame is a rectangular collection of variables (in the columns) and observations (in the rows). <code>mpg</code> contains observations collected by the US Environmental Protection Agency on 38 car models.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">mpg
|
||||
#> # A tibble: 234 × 11
|
||||
@@ -58,7 +58,7 @@ The<code>mpg</code> data frame</h2>
|
||||
<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.</p></li>
|
||||
<li><p><code>hwy</code>, a car’s fuel efficiency on the highway, in miles per gallon (mpg). A car with a low fuel efficiency consumes more fuel than a car with a high fuel efficiency when they travel the same distance.</p></li>
|
||||
</ol><p>To learn more about <code>mpg</code>, open its help page by running <code><a href="#chp-https://ggplot2.tidyverse.org/reference/mpg" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/mpg</a></code>.</p>
|
||||
</ol><p>To learn more about <code>mpg</code>, open its help page by running <code><a href="https://ggplot2.tidyverse.org/reference/mpg.html">?mpg</a></code>.</p>
|
||||
</section>
|
||||
|
||||
<section id="creating-a-ggplot" data-type="sect2">
|
||||
@@ -73,9 +73,9 @@ Creating a ggplot</h2>
|
||||
</div>
|
||||
</div>
|
||||
<p>The plot shows a negative relationship between engine size (<code>displ</code>) and fuel efficiency (<code>hwy</code>). In other words, cars with smaller engine sizes have higher fuel efficiency and, in general, as engine size increases, fuel efficiency decreases. Does this confirm or refute your hypothesis about fuel efficiency and engine size?</p>
|
||||
<p>With ggplot2, you begin a plot with the function <code><a href="#chp-https://ggplot2.tidyverse.org/reference/ggplot" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/ggplot</a></code>. <code><a href="#chp-https://ggplot2.tidyverse.org/reference/ggplot" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/ggplot</a></code> creates a coordinate system that you can add layers to. The first argument of <code><a href="#chp-https://ggplot2.tidyverse.org/reference/ggplot" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/ggplot</a></code> is the dataset to use in the graph. So <code>ggplot(data = mpg)</code> creates an empty graph, but it’s not very interesting so we won’t show it here.</p>
|
||||
<p>You complete your graph by adding one or more layers to <code><a href="#chp-https://ggplot2.tidyverse.org/reference/ggplot" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/ggplot</a></code>. The function <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_point" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_point</a></code> adds a layer of points to your plot, which creates a scatterplot. ggplot2 comes with many geom functions that each adds a different type of layer to a plot. You’ll learn a whole bunch of them throughout this chapter.</p>
|
||||
<p>Each geom function in ggplot2 takes a <code>mapping</code> argument. This defines how variables in your dataset are mapped to visual properties of your plot. The <code>mapping</code> argument is always paired with <code><a href="#chp-https://ggplot2.tidyverse.org/reference/aes" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/aes</a></code>, and the <code>x</code> and <code>y</code> arguments of <code><a href="#chp-https://ggplot2.tidyverse.org/reference/aes" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/aes</a></code> specify which variables to map to the x and y axes. ggplot2 looks for the mapped variables in the <code>data</code> argument, in this case, <code>mpg</code>.</p>
|
||||
<p>With ggplot2, you begin a plot with the function <code><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot()</a></code>. <code><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot()</a></code> creates a coordinate system that you can add layers to. The first argument of <code><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot()</a></code> is the dataset to use in the graph. So <code>ggplot(data = mpg)</code> creates an empty graph, but it’s not very interesting so we won’t show it here.</p>
|
||||
<p>You complete your graph by adding one or more layers to <code><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot()</a></code>. The function <code><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point()</a></code> adds a layer of points to your plot, which creates a scatterplot. ggplot2 comes with many geom functions that each adds a different type of layer to a plot. You’ll learn a whole bunch of them throughout this chapter.</p>
|
||||
<p>Each geom function in ggplot2 takes a <code>mapping</code> argument. This defines how variables in your dataset are mapped to visual properties of your plot. The <code>mapping</code> argument is always paired with <code><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes()</a></code>, and the <code>x</code> and <code>y</code> arguments of <code><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes()</a></code> specify which variables to map to the x and y axes. ggplot2 looks for the mapped variables in the <code>data</code> argument, in this case, <code>mpg</code>.</p>
|
||||
</section>
|
||||
|
||||
<section id="a-graphing-template" data-type="sect2">
|
||||
@@ -94,7 +94,7 @@ A graphing template</h2>
|
||||
Exercises</h2>
|
||||
<ol type="1"><li><p>Run <code>ggplot(data = mpg)</code>. What do you see?</p></li>
|
||||
<li><p>How many rows are in <code>mpg</code>? How many columns?</p></li>
|
||||
<li><p>What does the <code>drv</code> variable describe? Read the help for <code><a href="#chp-https://ggplot2.tidyverse.org/reference/mpg" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/mpg</a></code> to find out.</p></li>
|
||||
<li><p>What does the <code>drv</code> variable describe? Read the help for <code><a href="https://ggplot2.tidyverse.org/reference/mpg.html">?mpg</a></code> to find out.</p></li>
|
||||
<li><p>Make a scatterplot of <code>hwy</code> vs <code>cyl</code>.</p></li>
|
||||
<li><p>What happens if you make a scatterplot of <code>class</code> vs <code>drv</code>? Why is the plot not useful?</p></li>
|
||||
</ol></section>
|
||||
@@ -128,7 +128,7 @@ Aesthetic mappings</h1>
|
||||
</div>
|
||||
</div>
|
||||
<p>(If you prefer British English, like Hadley, you can use <code>colour</code> instead of <code>color</code>.)</p>
|
||||
<p>To map an aesthetic to a variable, associate the name of the aesthetic with the name of the variable inside <code><a href="#chp-https://ggplot2.tidyverse.org/reference/aes" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/aes</a></code>. ggplot2 will automatically assign a unique level of the aesthetic (here a unique color) to each unique value of the variable, a process known as <strong>scaling</strong>. ggplot2 will also add a legend that explains which levels correspond to which values.</p>
|
||||
<p>To map an aesthetic to a variable, associate the name of the aesthetic with the name of the variable inside <code><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes()</a></code>. ggplot2 will automatically assign a unique level of the aesthetic (here a unique color) to each unique value of the variable, a process known as <strong>scaling</strong>. ggplot2 will also add a legend that explains which levels correspond to which values.</p>
|
||||
<p>The colors reveal that many of the unusual points (with engine size greater than 5 liters and highway fuel efficiency greater than 20 miles per gallon) are two-seater cars. These cars don’t seem like hybrids, and are, in fact, sports cars! Sports cars have large engines like SUVs and pickup trucks, but small bodies like midsize and compact cars, which improves their gas mileage. In hindsight, these cars were unlikely to be hybrids since they have large engines.</p>
|
||||
<p>In the above example, we mapped <code>class</code> to the color aesthetic, but we could have mapped <code>class</code> to the size aesthetic in the same way. In this case, the exact size of each point would reveal its class affiliation. We get a <em>warning</em> here: mapping an unordered variable (<code>class</code>) to an ordered aesthetic (<code>size</code>) is generally not a good idea because it implies a ranking that does not in fact exist.</p>
|
||||
<div class="cell">
|
||||
@@ -160,7 +160,7 @@ ggplot(data = mpg) +
|
||||
</div>
|
||||
</div>
|
||||
<p>What happened to the SUVs? ggplot2 will only use six shapes at a time. By default, additional groups will go unplotted when you use the shape aesthetic.</p>
|
||||
<p>For each aesthetic, you use <code><a href="#chp-https://ggplot2.tidyverse.org/reference/aes" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/aes</a></code> to associate the name of the aesthetic with a variable to display. The <code><a href="#chp-https://ggplot2.tidyverse.org/reference/aes" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/aes</a></code> function gathers together each of the aesthetic mappings used by a layer and passes them to the layer’s mapping argument. The syntax highlights a useful insight about <code>x</code> and <code>y</code>: the x and y locations of a point are themselves aesthetics, visual properties that you can map to variables to display information about the data.</p>
|
||||
<p>For each aesthetic, you use <code><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes()</a></code> to associate the name of the aesthetic with a variable to display. The <code><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes()</a></code> function gathers together each of the aesthetic mappings used by a layer and passes them to the layer’s mapping argument. The syntax highlights a useful insight about <code>x</code> and <code>y</code>: the x and y locations of a point are themselves aesthetics, visual properties that you can map to variables to display information about the data.</p>
|
||||
<p>Once you map an aesthetic, ggplot2 takes care of the rest. It selects a reasonable scale to use with the aesthetic, and it constructs a legend that explains the mapping between levels and values. For x and y aesthetics, ggplot2 does not create a legend, but it creates an axis line with tick marks and a label. The axis line acts as a legend; it explains the mapping between locations and values.</p>
|
||||
<p>You can also <em>set</em> the aesthetic properties of your geom manually. For example, we can make all of the points in our plot blue:</p>
|
||||
<div class="cell">
|
||||
@@ -170,7 +170,7 @@ ggplot(data = mpg) +
|
||||
<p><img src="data-visualize_files/figure-html/unnamed-chunk-12-1.png" alt="Scatterplot of highway fuel efficiency versus engine size of cars that shows a negative association. All points are blue." width="576"/></p>
|
||||
</div>
|
||||
</div>
|
||||
<p>Here, the color doesn’t convey information about a variable, but only changes the appearance of the plot. To set an aesthetic manually, set the aesthetic by name as an argument of your geom function. In other words, it goes <em>outside</em> of <code><a href="#chp-https://ggplot2.tidyverse.org/reference/aes" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/aes</a></code>. You’ll need to pick a value that makes sense for that aesthetic:</p>
|
||||
<p>Here, the color doesn’t convey information about a variable, but only changes the appearance of the plot. To set an aesthetic manually, set the aesthetic by name as an argument of your geom function. In other words, it goes <em>outside</em> of <code><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes()</a></code>. You’ll need to pick a value that makes sense for that aesthetic:</p>
|
||||
<ul><li>The name of a color as a character string.</li>
|
||||
<li>The size of a point in mm.</li>
|
||||
<li>The shape of a point as a number, as shown in <a href="#fig-shapes" data-type="xref">#fig-shapes</a>.</li>
|
||||
@@ -196,10 +196,10 @@ Exercises</h2>
|
||||
</div>
|
||||
</div>
|
||||
</li>
|
||||
<li><p>Which variables in <code>mpg</code> are categorical? Which variables are continuous? (Hint: type <code><a href="#chp-https://ggplot2.tidyverse.org/reference/mpg" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/mpg</a></code> to read the documentation for the dataset). How can you see this information when you run <code>mpg</code>?</p></li>
|
||||
<li><p>Which variables in <code>mpg</code> are categorical? Which variables are continuous? (Hint: type <code><a href="https://ggplot2.tidyverse.org/reference/mpg.html">?mpg</a></code> to read the documentation for the dataset). How can you see this information when you run <code>mpg</code>?</p></li>
|
||||
<li><p>Map a continuous variable to <code>color</code>, <code>size</code>, and <code>shape</code>. How do these aesthetics behave differently for categorical vs. continuous variables?</p></li>
|
||||
<li><p>What happens if you map the same variable to multiple aesthetics?</p></li>
|
||||
<li><p>What does the <code>stroke</code> aesthetic do? What shapes does it work with? (Hint: use <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_point" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_point</a></code>)</p></li>
|
||||
<li><p>What does the <code>stroke</code> aesthetic do? What shapes does it work with? (Hint: use <code><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">?geom_point</a></code>)</p></li>
|
||||
<li><p>What happens if you map an aesthetic to something other than a variable name, like <code>aes(color = displ < 5)</code>? Note, you’ll also need to specify x and y.</p></li>
|
||||
</ol></section>
|
||||
</section>
|
||||
@@ -220,7 +220,7 @@ Common problems</h1>
|
||||
<h1>
|
||||
Facets</h1>
|
||||
<p>One way to add additional variables to a plot is by mapping them to an aesthetic. Another way, which is particularly useful for categorical variables, is to split your plot into <strong>facets</strong>, subplots that each display one subset of the data.</p>
|
||||
<p>To facet your plot by a single variable, use <code><a href="#chp-https://ggplot2.tidyverse.org/reference/facet_wrap" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/facet_wrap</a></code>. The first argument of <code><a href="#chp-https://ggplot2.tidyverse.org/reference/facet_wrap" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/facet_wrap</a></code> is a formula<span data-type="footnote">Here “formula” is the name of the type of thing created by <code>~</code>, not a synonym for “equation”.</span>, which you create with <code>~</code> followed by a variable name. The variable that you pass to <code><a href="#chp-https://ggplot2.tidyverse.org/reference/facet_wrap" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/facet_wrap</a></code> should be discrete.</p>
|
||||
<p>To facet your plot by a single variable, use <code><a href="https://ggplot2.tidyverse.org/reference/facet_wrap.html">facet_wrap()</a></code>. The first argument of <code><a href="https://ggplot2.tidyverse.org/reference/facet_wrap.html">facet_wrap()</a></code> is a formula<span data-type="footnote">Here “formula” is the name of the type of thing created by <code>~</code>, not a synonym for “equation”.</span>, which you create with <code>~</code> followed by a variable name. The variable that you pass to <code><a href="https://ggplot2.tidyverse.org/reference/facet_wrap.html">facet_wrap()</a></code> should be discrete.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">ggplot(data = mpg) +
|
||||
geom_point(mapping = aes(x = displ, y = hwy)) +
|
||||
@@ -229,7 +229,7 @@ Facets</h1>
|
||||
<p><img src="data-visualize_files/figure-html/unnamed-chunk-15-1.png" alt="Scatterplot of highway fuel efficiency versus engine size of cars, faceted by class, with facets spanning two rows." width="576"/></p>
|
||||
</div>
|
||||
</div>
|
||||
<p>To facet your plot with the combination of two variables, switch from <code><a href="#chp-https://ggplot2.tidyverse.org/reference/facet_wrap" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/facet_wrap</a></code> to <code><a href="#chp-https://ggplot2.tidyverse.org/reference/facet_grid" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/facet_grid</a></code>. The first argument of <code><a href="#chp-https://ggplot2.tidyverse.org/reference/facet_grid" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/facet_grid</a></code> is also a formula, but now it’s a double sided formula: <code>rows ~ cols</code>.</p>
|
||||
<p>To facet your plot with the combination of two variables, switch from <code><a href="https://ggplot2.tidyverse.org/reference/facet_wrap.html">facet_wrap()</a></code> to <code><a href="https://ggplot2.tidyverse.org/reference/facet_grid.html">facet_grid()</a></code>. The first argument of <code><a href="https://ggplot2.tidyverse.org/reference/facet_grid.html">facet_grid()</a></code> is also a formula, but now it’s a double sided formula: <code>rows ~ cols</code>.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">ggplot(data = mpg) +
|
||||
geom_point(mapping = aes(x = displ, y = hwy)) +
|
||||
@@ -274,7 +274,7 @@ ggplot(data = mpg) +
|
||||
</div>
|
||||
<p>What are the advantages to using faceting instead of the color aesthetic? What are the disadvantages? How might the balance change if you had a larger dataset?</p>
|
||||
</li>
|
||||
<li><p>Read <code><a href="#chp-https://ggplot2.tidyverse.org/reference/facet_wrap" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/facet_wrap</a></code>. What does <code>nrow</code> do? What does <code>ncol</code> do? What other options control the layout of the individual panels? Why doesn’t <code><a href="#chp-https://ggplot2.tidyverse.org/reference/facet_grid" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/facet_grid</a></code> have <code>nrow</code> and <code>ncol</code> arguments?</p></li>
|
||||
<li><p>Read <code><a href="https://ggplot2.tidyverse.org/reference/facet_wrap.html">?facet_wrap</a></code>. What does <code>nrow</code> do? What does <code>ncol</code> do? What other options control the layout of the individual panels? Why doesn’t <code><a href="https://ggplot2.tidyverse.org/reference/facet_grid.html">facet_grid()</a></code> have <code>nrow</code> and <code>ncol</code> arguments?</p></li>
|
||||
<li>
|
||||
<p>Which of the following two plots makes it easier to compare engine size (<code>displ</code>) across cars with different drive trains? What does this say about when to place a faceting variable across rows or columns?</p>
|
||||
<div class="cell">
|
||||
@@ -294,7 +294,7 @@ ggplot(data = mpg) +
|
||||
</div>
|
||||
</li>
|
||||
<li>
|
||||
<p>Recreate this plot using <code><a href="#chp-https://ggplot2.tidyverse.org/reference/facet_wrap" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/facet_wrap</a></code> instead of <code><a href="#chp-https://ggplot2.tidyverse.org/reference/facet_grid" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/facet_grid</a></code>. How do the positions of the facet labels change?</p>
|
||||
<p>Recreate this plot using <code><a href="https://ggplot2.tidyverse.org/reference/facet_wrap.html">facet_wrap()</a></code> instead of <code><a href="https://ggplot2.tidyverse.org/reference/facet_grid.html">facet_grid()</a></code>. How do the positions of the facet labels change?</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">ggplot(data = mpg) +
|
||||
geom_point(mapping = aes(x = displ, y = hwy)) +
|
||||
@@ -323,7 +323,7 @@ Geometric objects</h1>
|
||||
</div>
|
||||
<p>Both plots contain the same x variable, the same y variable, and both describe the same data. But the plots are not identical. Each plot uses a different visual object to represent the data. In ggplot2 syntax, we say that they use different <strong>geoms</strong>.</p>
|
||||
<p>A <strong>geom</strong> is the geometrical object that a plot uses to represent data. People often describe plots by the type of geom that the plot uses. For example, bar charts use bar geoms, line charts use line geoms, boxplots use boxplot geoms, and so on. Scatterplots break the trend; they use the point geom. As we see above, you can use different geoms to plot the same data. The plot on the left uses the point geom, and the plot on the right uses the smooth geom, a smooth line fitted to the data.</p>
|
||||
<p>To change the geom in your plot, change the geom function that you add to <code><a href="#chp-https://ggplot2.tidyverse.org/reference/ggplot" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/ggplot</a></code>. For instance, to make the plots above, you can use this code:</p>
|
||||
<p>To change the geom in your plot, change the geom function that you add to <code><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot()</a></code>. For instance, to make the plots above, you can use this code:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit"># Left
|
||||
ggplot(data = mpg) +
|
||||
@@ -333,7 +333,7 @@ ggplot(data = mpg) +
|
||||
ggplot(data = mpg) +
|
||||
geom_smooth(mapping = aes(x = displ, y = hwy))</pre>
|
||||
</div>
|
||||
<p>Every geom function in ggplot2 takes a <code>mapping</code> argument. However, not every aesthetic works with every geom. You could set the shape of a point, but you couldn’t set the “shape” of a line. On the other hand, you <em>could</em> set the linetype of a line. <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_smooth" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_smooth</a></code> will draw a different line, with a different linetype, for each unique value of the variable that you map to linetype.</p>
|
||||
<p>Every geom function in ggplot2 takes a <code>mapping</code> argument. However, not every aesthetic works with every geom. You could set the shape of a point, but you couldn’t set the “shape” of a line. On the other hand, you <em>could</em> set the linetype of a line. <code><a href="https://ggplot2.tidyverse.org/reference/geom_smooth.html">geom_smooth()</a></code> will draw a different line, with a different linetype, for each unique value of the variable that you map to linetype.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">ggplot(data = mpg) +
|
||||
geom_smooth(mapping = aes(x = displ, y = hwy, linetype = drv))</pre>
|
||||
@@ -341,7 +341,7 @@ ggplot(data = mpg) +
|
||||
<p><img src="data-visualize_files/figure-html/unnamed-chunk-24-1.png" alt="A plot of highway fuel efficiency versus engine size of cars. The data are represented with smooth curves, which use a different line type (solid, dashed, or long dashed) for each type of drive train. Confidence intervals around the smooth curves are also displayed." width="576"/></p>
|
||||
</div>
|
||||
</div>
|
||||
<p>Here, <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_smooth" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_smooth</a></code> separates the cars into three lines based on their <code>drv</code> value, which describes a car’s drive train. One line describes all of the points that have a <code>4</code> value, one line describes all of the points that have an <code>f</code> value, and one line describes all of the points that have an <code>r</code> value. Here, <code>4</code> stands for four-wheel drive, <code>f</code> for front-wheel drive, and <code>r</code> for rear-wheel drive.</p>
|
||||
<p>Here, <code><a href="https://ggplot2.tidyverse.org/reference/geom_smooth.html">geom_smooth()</a></code> separates the cars into three lines based on their <code>drv</code> value, which describes a car’s drive train. One line describes all of the points that have a <code>4</code> value, one line describes all of the points that have an <code>f</code> value, and one line describes all of the points that have an <code>r</code> value. Here, <code>4</code> stands for four-wheel drive, <code>f</code> for front-wheel drive, and <code>r</code> for rear-wheel drive.</p>
|
||||
<p>If this sounds strange, we can make it more clear by overlaying the lines on top of the raw data and then coloring everything according to <code>drv</code>.</p>
|
||||
<div class="cell">
|
||||
<div class="cell-output-display">
|
||||
@@ -349,8 +349,8 @@ ggplot(data = mpg) +
|
||||
</div>
|
||||
</div>
|
||||
<p>Notice that this plot contains two geoms in the same graph! If this makes you excited, buckle up. You will learn how to place multiple geoms in the same plot very soon.</p>
|
||||
<p>ggplot2 provides more than 40 geoms, and extension packages provide even more (see <a href="https://exts.ggplot2.tidyverse.org/gallery/" class="uri">https://exts.ggplot2.tidyverse.org/gallery/</a> for a sampling). The best way to get a comprehensive overview is the ggplot2 cheatsheet, which you can find at <a href="https://rstudio.com/resources/cheatsheets" class="uri">https://rstudio.com/resources/cheatsheets</a>. To learn more about any single geom, use the help (e.g. <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_smooth" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_smooth</a></code>).</p>
|
||||
<p>Many geoms, like <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_smooth" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_smooth</a></code>, use a single geometric object to display multiple rows of data. For these geoms, you can set the <code>group</code> aesthetic to a categorical variable to draw multiple objects. ggplot2 will draw a separate object for each unique value of the grouping variable. In practice, ggplot2 will automatically group the data for these geoms whenever you map an aesthetic to a discrete variable (as in the <code>linetype</code> example). It is convenient to rely on this feature because the <code>group</code> aesthetic by itself does not add a legend or distinguishing features to the geoms.</p>
|
||||
<p>ggplot2 provides more than 40 geoms, and extension packages provide even more (see <a href="https://exts.ggplot2.tidyverse.org/gallery/" class="uri">https://exts.ggplot2.tidyverse.org/gallery/</a> for a sampling). The best way to get a comprehensive overview is the ggplot2 cheatsheet, which you can find at <a href="https://rstudio.com/resources/cheatsheets" class="uri">https://rstudio.com/resources/cheatsheets</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>
|
||||
<p>Many geoms, like <code><a href="https://ggplot2.tidyverse.org/reference/geom_smooth.html">geom_smooth()</a></code>, use a single geometric object to display multiple rows of data. For these geoms, you can set the <code>group</code> aesthetic to a categorical variable to draw multiple objects. ggplot2 will draw a separate object for each unique value of the grouping variable. In practice, ggplot2 will automatically group the data for these geoms whenever you map an aesthetic to a discrete variable (as in the <code>linetype</code> example). It is convenient to rely on this feature because the <code>group</code> aesthetic by itself does not add a legend or distinguishing features to the geoms.</p>
|
||||
<div>
|
||||
<pre data-type="programlisting" data-code-language="downlit">ggplot(data = mpg) +
|
||||
geom_smooth(mapping = aes(x = displ, y = hwy))
|
||||
@@ -377,7 +377,7 @@ ggplot(data = mpg) +
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p>To display multiple geoms in the same plot, add multiple geom functions to <code><a href="#chp-https://ggplot2.tidyverse.org/reference/ggplot" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/ggplot</a></code>:</p>
|
||||
<p>To display multiple geoms in the same plot, add multiple geom functions to <code><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot()</a></code>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">ggplot(data = mpg) +
|
||||
geom_point(mapping = aes(x = displ, y = hwy)) +
|
||||
@@ -386,7 +386,7 @@ ggplot(data = mpg) +
|
||||
<p><img src="data-visualize_files/figure-html/unnamed-chunk-27-1.png" alt="Scatterplot of highway fuel efficiency versus engine size of cars with a smooth curve overlaid. A confidence interval around the smooth curves is also displayed." width="576"/></p>
|
||||
</div>
|
||||
</div>
|
||||
<p>This, however, introduces some duplication in our code. Imagine if you wanted to change the y-axis to display <code>cty</code> instead of <code>hwy</code>. You’d need to change the variable in two places, and you might forget to update one. You can avoid this type of repetition by passing a set of mappings to <code><a href="#chp-https://ggplot2.tidyverse.org/reference/ggplot" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/ggplot</a></code>. ggplot2 will treat these mappings as global mappings that apply to each geom in the graph. In other words, this code will produce the same plot as the previous code:</p>
|
||||
<p>This, however, introduces some duplication in our code. Imagine if you wanted to change the y-axis to display <code>cty</code> instead of <code>hwy</code>. You’d need to change the variable in two places, and you might forget to update one. You can avoid this type of repetition by passing a set of mappings to <code><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot()</a></code>. ggplot2 will treat these mappings as global mappings that apply to each geom in the graph. In other words, this code will produce the same plot as the previous code:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
|
||||
geom_point() +
|
||||
@@ -401,7 +401,7 @@ ggplot(data = mpg) +
|
||||
<p><img src="data-visualize_files/figure-html/unnamed-chunk-29-1.png" alt="Scatterplot of highway fuel efficiency versus engine size of cars, where points are colored according to the car class. A smooth curve following the trajectory of the relationship between highway fuel efficiency versus engine size of cars is overlaid along with a confidence interval around it." width="576"/></p>
|
||||
</div>
|
||||
</div>
|
||||
<p>You can use the same idea to specify different <code>data</code> for each layer. Here, our smooth line displays just a subset of the <code>mpg</code> dataset, the subcompact cars. The local data argument in <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_smooth" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_smooth</a></code> overrides the global data argument in <code><a href="#chp-https://ggplot2.tidyverse.org/reference/ggplot" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/ggplot</a></code> for that layer only.</p>
|
||||
<p>You can use the same idea to specify different <code>data</code> for each layer. Here, our smooth line displays just a subset of the <code>mpg</code> dataset, the subcompact cars. The local data argument in <code><a href="https://ggplot2.tidyverse.org/reference/geom_smooth.html">geom_smooth()</a></code> overrides the global data argument in <code><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot()</a></code> for that layer only.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
|
||||
geom_point(mapping = aes(color = class)) +
|
||||
@@ -410,7 +410,7 @@ ggplot(data = mpg) +
|
||||
<p><img src="data-visualize_files/figure-html/unnamed-chunk-30-1.png" alt="Scatterplot of highway fuel efficiency versus engine size of cars, where points are colored according to the car class. A smooth curve following the trajectory of the relationship between highway fuel efficiency versus engine size of subcompact cars is overlaid along with a confidence interval around it." width="576"/></p>
|
||||
</div>
|
||||
</div>
|
||||
<p>(You’ll learn how <code><a href="#chp-https://dplyr.tidyverse.org/reference/filter" data-type="xref">#chp-https://dplyr.tidyverse.org/reference/filter</a></code> works in the chapter on data transformations: for now, just know that this command selects only the subcompact cars.)</p>
|
||||
<p>(You’ll learn how <code><a href="https://dplyr.tidyverse.org/reference/filter.html">filter()</a></code> works in the chapter on data transformations: for now, just know that this command selects only the subcompact cars.)</p>
|
||||
|
||||
<section id="exercises-3" data-type="sect2">
|
||||
<h2>
|
||||
@@ -435,7 +435,7 @@ Exercises</h2>
|
||||
</div>
|
||||
<p>What does <code>show.legend = FALSE</code> do here? What happens if you remove it? Why do you think we used it earlier?</p>
|
||||
</li>
|
||||
<li><p>What does the <code>se</code> argument to <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_smooth" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_smooth</a></code> do?</p></li>
|
||||
<li><p>What does the <code>se</code> argument to <code><a href="https://ggplot2.tidyverse.org/reference/geom_smooth.html">geom_smooth()</a></code> do?</p></li>
|
||||
<li>
|
||||
<p>Will these two graphs look different? Why/why not?</p>
|
||||
<div class="cell">
|
||||
@@ -483,7 +483,7 @@ ggplot() +
|
||||
<section id="statistical-transformations" data-type="sect1">
|
||||
<h1>
|
||||
Statistical transformations</h1>
|
||||
<p>Next, let’s take a look at a bar chart. Bar charts seem simple, but they are interesting because they reveal something subtle about plots. Consider a basic bar chart, as drawn with <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_bar" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_bar</a></code> or <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_bar" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_bar</a></code>. The following chart displays the total number of diamonds in the <code>diamonds</code> dataset, grouped by <code>cut</code>. The <code>diamonds</code> dataset is in the ggplot2 package and contains information on ~54,000 diamonds, including the <code>price</code>, <code>carat</code>, <code>color</code>, <code>clarity</code>, and <code>cut</code> of each diamond. The chart shows that more diamonds are available with high quality cuts than with low quality cuts.</p>
|
||||
<p>Next, let’s take a look at a bar chart. Bar charts seem simple, but they are interesting because they reveal something subtle about plots. Consider a basic bar chart, as drawn with <code><a href="https://ggplot2.tidyverse.org/reference/geom_bar.html">geom_bar()</a></code> or <code><a href="https://ggplot2.tidyverse.org/reference/geom_bar.html">geom_col()</a></code>. The following chart displays the total number of diamonds in the <code>diamonds</code> dataset, grouped by <code>cut</code>. The <code>diamonds</code> dataset is in the ggplot2 package and contains information on ~54,000 diamonds, including the <code>price</code>, <code>carat</code>, <code>color</code>, <code>clarity</code>, and <code>cut</code> of each diamond. The chart shows that more diamonds are available with high quality cuts than with low quality cuts.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">ggplot(data = diamonds) +
|
||||
geom_bar(mapping = aes(x = cut))</pre>
|
||||
@@ -495,7 +495,7 @@ Statistical transformations</h1>
|
||||
<ul><li><p>bar charts, histograms, and frequency polygons bin your data and then plot bin counts, the number of points that fall in each bin.</p></li>
|
||||
<li><p>smoothers fit a model to your data and then plot predictions from the model.</p></li>
|
||||
<li><p>boxplots compute a robust summary of the distribution and then display that summary as a specially formatted box.</p></li>
|
||||
</ul><p>The algorithm used to calculate new values for a graph is called a <strong>stat</strong>, short for statistical transformation. <a href="#fig-vis-stat-bar" data-type="xref">#fig-vis-stat-bar</a> shows how this process works with <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_bar" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_bar</a></code>.</p>
|
||||
</ul><p>The algorithm used to calculate new values for a graph is called a <strong>stat</strong>, short for statistical transformation. <a href="#fig-vis-stat-bar" data-type="xref">#fig-vis-stat-bar</a> shows how this process works with <code><a href="https://ggplot2.tidyverse.org/reference/geom_bar.html">geom_bar()</a></code>.</p>
|
||||
<div class="cell">
|
||||
<div class="cell-output-display">
|
||||
|
||||
@@ -504,8 +504,8 @@ Statistical transformations</h1>
|
||||
</figure>
|
||||
</div>
|
||||
</div>
|
||||
<p>You can learn which stat a geom uses by inspecting the default value for the <code>stat</code> argument. For example, <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_bar" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_bar</a></code> shows that the default value for <code>stat</code> is “count”, which means that <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_bar" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_bar</a></code> uses <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_bar" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_bar</a></code>. <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_bar" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_bar</a></code> is documented on the same page as <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_bar" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_bar</a></code>. If you scroll down, the section called “Computed variables” explains that it computes two new variables: <code>count</code> and <code>prop</code>.</p>
|
||||
<p>You can generally use geoms and stats interchangeably. For example, you can recreate the previous plot using <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_bar" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_bar</a></code> instead of <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_bar" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_bar</a></code>:</p>
|
||||
<p>You can learn which stat a geom uses by inspecting the default value for the <code>stat</code> argument. For example, <code><a href="https://ggplot2.tidyverse.org/reference/geom_bar.html">?geom_bar</a></code> shows that the default value for <code>stat</code> is “count”, which means that <code><a href="https://ggplot2.tidyverse.org/reference/geom_bar.html">geom_bar()</a></code> uses <code><a href="https://ggplot2.tidyverse.org/reference/geom_bar.html">stat_count()</a></code>. <code><a href="https://ggplot2.tidyverse.org/reference/geom_bar.html">stat_count()</a></code> is documented on the same page as <code><a href="https://ggplot2.tidyverse.org/reference/geom_bar.html">geom_bar()</a></code>. If you scroll down, the section called “Computed variables” explains that it computes two new variables: <code>count</code> and <code>prop</code>.</p>
|
||||
<p>You can generally use geoms and stats interchangeably. For example, you can recreate the previous plot using <code><a href="https://ggplot2.tidyverse.org/reference/geom_bar.html">stat_count()</a></code> instead of <code><a href="https://ggplot2.tidyverse.org/reference/geom_bar.html">geom_bar()</a></code>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">ggplot(data = diamonds) +
|
||||
stat_count(mapping = aes(x = cut))</pre>
|
||||
@@ -515,7 +515,7 @@ Statistical transformations</h1>
|
||||
</div>
|
||||
<p>This works because every geom has a default stat; and every stat has a default geom. This means that you can typically use geoms without worrying about the underlying statistical transformation. However, there are three reasons why you might need to use a stat explicitly:</p>
|
||||
<ol type="1"><li>
|
||||
<p>You might want to override the default stat. In the code below, we change the stat of <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_bar" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_bar</a></code> from count (the default) to identity. This lets me map the height of the bars to the raw values of a <span class="math inline">\(y\)</span> variable. Unfortunately when people talk about bar charts casually, they might be referring to this type of bar chart, where the height of the bar is already present in the data, or the previous bar chart where the height of the bar is generated by counting rows.</p>
|
||||
<p>You might want to override the default stat. In the code below, we change the stat of <code><a href="https://ggplot2.tidyverse.org/reference/geom_bar.html">geom_bar()</a></code> from count (the default) to identity. This lets me map the height of the bars to the raw values of a <span class="math inline">\(y\)</span> variable. Unfortunately when people talk about bar charts casually, they might be referring to this type of bar chart, where the height of the bar is already present in the data, or the previous bar chart where the height of the bar is generated by counting rows.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">demo <- tribble(
|
||||
~cut, ~freq,
|
||||
@@ -532,7 +532,7 @@ ggplot(data = demo) +
|
||||
<p><img src="data-visualize_files/figure-html/unnamed-chunk-38-1.png" alt="Bar chart of number of each cut of diamond. There are roughly 1500 Fair, 5000 Good, 12000 Very Good, 14000 Premium, and 22000 Ideal cut diamonds." width="576"/></p>
|
||||
</div>
|
||||
</div>
|
||||
<p>(Don’t worry that you haven’t seen <code><-</code> or <code><a href="#chp-https://tibble.tidyverse.org/reference/tribble" data-type="xref">#chp-https://tibble.tidyverse.org/reference/tribble</a></code> before. You might be able to guess their meaning from the context, and you’ll learn exactly what they do soon!)</p>
|
||||
<p>(Don’t worry that you haven’t seen <code><-</code> or <code><a href="https://tibble.tidyverse.org/reference/tribble.html">tribble()</a></code> before. You might be able to guess their meaning from the context, and you’ll learn exactly what they do soon!)</p>
|
||||
</li>
|
||||
<li>
|
||||
<p>You might want to override the default mapping from transformed variables to aesthetics. For example, you might want to display a bar chart of proportions, rather than counts:</p>
|
||||
@@ -543,10 +543,10 @@ ggplot(data = demo) +
|
||||
<p><img src="data-visualize_files/figure-html/unnamed-chunk-39-1.png" alt="Bar chart of proportion of each cut of diamond. Roughly, Fair diamonds make up 0.03, Good 0.09, Very Good 0.22, Premium 26, and Ideal 0.40." width="576"/></p>
|
||||
</div>
|
||||
</div>
|
||||
<p>To find the variables computed by the stat, look for the section titled “computed variables” in the help for <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_bar" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_bar</a></code>.</p>
|
||||
<p>To find the variables computed by the stat, look for the section titled “computed variables” in the help for <code><a href="https://ggplot2.tidyverse.org/reference/geom_bar.html">geom_bar()</a></code>.</p>
|
||||
</li>
|
||||
<li>
|
||||
<p>You might want to draw greater attention to the statistical transformation in your code. For example, you might use <code><a href="#chp-https://ggplot2.tidyverse.org/reference/stat_summary" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/stat_summary</a></code>, which summarizes the y values for each unique x value, to draw attention to the summary that you’re computing:</p>
|
||||
<p>You might want to draw greater attention to the statistical transformation in your code. For example, you might use <code><a href="https://ggplot2.tidyverse.org/reference/stat_summary.html">stat_summary()</a></code>, which summarizes the y values for each unique x value, to draw attention to the summary that you’re computing:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">ggplot(data = diamonds) +
|
||||
stat_summary(
|
||||
@@ -560,15 +560,15 @@ ggplot(data = demo) +
|
||||
</div>
|
||||
</div>
|
||||
</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="#chp-https://ggplot2.tidyverse.org/reference/geom_histogram" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_histogram</a></code>. To see a complete list of stats, try the <a href="#chp-https://rstudio.com/resources/cheatsheets" data-type="xref">#chp-https://rstudio.com/resources/cheatsheets</a>.</p>
|
||||
</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>. To see a complete list of stats, try the <a href="https://rstudio.com/resources/cheatsheets">ggplot2 cheatsheet</a>.</p>
|
||||
|
||||
<section id="exercises-4" data-type="sect2">
|
||||
<h2>
|
||||
Exercises</h2>
|
||||
<ol type="1"><li><p>What is the default geom associated with <code><a href="#chp-https://ggplot2.tidyverse.org/reference/stat_summary" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/stat_summary</a></code>? How could you rewrite the previous plot to use that geom function instead of the stat function?</p></li>
|
||||
<li><p>What does <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_bar" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_bar</a></code> do? How is it different from <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_bar" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_bar</a></code>?</p></li>
|
||||
<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>
|
||||
<li><p>What does <code><a href="https://ggplot2.tidyverse.org/reference/geom_bar.html">geom_col()</a></code> do? How is it different from <code><a href="https://ggplot2.tidyverse.org/reference/geom_bar.html">geom_bar()</a></code>?</p></li>
|
||||
<li><p>Most geoms and stats come in pairs that are almost always used in concert. Read through the documentation and make a list of all the pairs. What do they have in common?</p></li>
|
||||
<li><p>What variables does <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_smooth" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_smooth</a></code> compute? What parameters control its behaviour?</p></li>
|
||||
<li><p>What variables does <code><a href="https://ggplot2.tidyverse.org/reference/geom_smooth.html">stat_smooth()</a></code> compute? What parameters control its behaviour?</p></li>
|
||||
<li>
|
||||
<p>In our proportion bar chart, we need to set <code>group = 1</code>. Why? In other words, what is the problem with these two graphs?</p>
|
||||
<div class="cell">
|
||||
@@ -665,8 +665,8 @@ ggplot(data = diamonds, mapping = aes(x = cut, color = clarity)) +
|
||||
<p><img src="data-visualize_files/figure-html/unnamed-chunk-48-1.png" alt="Jittered scatterplot of highway fuel efficiency versus engine size of cars. The plot shows a negative association." width="576"/></p>
|
||||
</div>
|
||||
</div>
|
||||
<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="#chp-https://ggplot2.tidyverse.org/reference/geom_jitter" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/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="#chp-https://ggplot2.tidyverse.org/reference/position_dodge" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/position_dodge</a></code>, <code><a href="#chp-https://ggplot2.tidyverse.org/reference/position_stack" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/position_stack</a></code>, <code><a href="#chp-https://ggplot2.tidyverse.org/reference/position_identity" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/position_identity</a></code>, <code><a href="#chp-https://ggplot2.tidyverse.org/reference/position_jitter" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/position_jitter</a></code>, and <code><a href="#chp-https://ggplot2.tidyverse.org/reference/position_stack" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/position_stack</a></code>.</p>
|
||||
<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-5" data-type="sect2">
|
||||
<h2>
|
||||
@@ -681,9 +681,9 @@ Exercises</h2>
|
||||
</div>
|
||||
</div>
|
||||
</li>
|
||||
<li><p>What parameters to <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_jitter" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_jitter</a></code> control the amount of jittering?</p></li>
|
||||
<li><p>Compare and contrast <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_jitter" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_jitter</a></code> with <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_count" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_count</a></code>.</p></li>
|
||||
<li><p>What’s the default position adjustment for <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_boxplot" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_boxplot</a></code>? Create a visualization of the <code>mpg</code> dataset that demonstrates it.</p></li>
|
||||
<li><p>What parameters to <code><a href="https://ggplot2.tidyverse.org/reference/geom_jitter.html">geom_jitter()</a></code> control the amount of jittering?</p></li>
|
||||
<li><p>Compare and contrast <code><a href="https://ggplot2.tidyverse.org/reference/geom_jitter.html">geom_jitter()</a></code> with <code><a href="https://ggplot2.tidyverse.org/reference/geom_count.html">geom_count()</a></code>.</p></li>
|
||||
<li><p>What’s the default position adjustment for <code><a href="https://ggplot2.tidyverse.org/reference/geom_boxplot.html">geom_boxplot()</a></code>? Create a visualization of the <code>mpg</code> dataset that demonstrates it.</p></li>
|
||||
</ol></section>
|
||||
</section>
|
||||
|
||||
@@ -692,7 +692,7 @@ Exercises</h2>
|
||||
Coordinate systems</h1>
|
||||
<p>Coordinate systems are probably the most complicated part of ggplot2. The default coordinate system is the Cartesian coordinate system where the x and y positions act independently to determine the location of each point. There are three other coordinate systems that are occasionally helpful.</p>
|
||||
<ul><li>
|
||||
<p><code><a href="#chp-https://ggplot2.tidyverse.org/reference/coord_flip" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/coord_flip</a></code> switches the x and y axes. This is useful (for example), if you want horizontal boxplots. It’s also useful for long labels: it’s hard to get them to fit without overlapping on the x-axis.</p>
|
||||
<p><code><a href="https://ggplot2.tidyverse.org/reference/coord_flip.html">coord_flip()</a></code> switches the x and y axes. This is useful (for example), if you want horizontal boxplots. It’s also useful for long labels: it’s hard to get them to fit without overlapping on the x-axis.</p>
|
||||
<div>
|
||||
<pre data-type="programlisting" data-code-language="downlit">ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
|
||||
geom_boxplot()
|
||||
@@ -720,7 +720,7 @@ ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
|
||||
</div>
|
||||
</li>
|
||||
<li>
|
||||
<p><code><a href="#chp-https://ggplot2.tidyverse.org/reference/coord_map" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/coord_map</a></code> sets the aspect ratio correctly for maps. This is very important if you’re plotting spatial data with ggplot2. We don’t have the space to discuss maps in this book, but you can learn more in the <a href="#chp-https://ggplot2-book.org/maps" data-type="xref">#chp-https://ggplot2-book.org/maps</a> of <em>ggplot2: Elegant graphics for data analysis</em>.</p>
|
||||
<p><code><a href="https://ggplot2.tidyverse.org/reference/coord_map.html">coord_quickmap()</a></code> sets the aspect ratio correctly for maps. This is very important if you’re plotting spatial data with ggplot2. We don’t have the space to discuss maps in this book, but you can learn more in the <a href="https://ggplot2-book.org/maps.html">Maps chapter</a> of <em>ggplot2: Elegant graphics for data analysis</em>.</p>
|
||||
<div>
|
||||
<pre data-type="programlisting" data-code-language="downlit">nz <- map_data("nz")
|
||||
|
||||
@@ -743,7 +743,7 @@ ggplot(nz, aes(long, lat, group = group)) +
|
||||
</div>
|
||||
</li>
|
||||
<li>
|
||||
<p><code><a href="#chp-https://ggplot2.tidyverse.org/reference/coord_polar" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/coord_polar</a></code> uses polar coordinates. Polar coordinates reveal an interesting connection between a bar chart and a Coxcomb chart.</p>
|
||||
<p><code><a href="https://ggplot2.tidyverse.org/reference/coord_polar.html">coord_polar()</a></code> uses polar coordinates. Polar coordinates reveal an interesting connection between a bar chart and a Coxcomb chart.</p>
|
||||
<div>
|
||||
<pre data-type="programlisting" data-code-language="downlit">bar <- ggplot(data = diamonds) +
|
||||
geom_bar(
|
||||
@@ -772,11 +772,11 @@ bar + coord_polar()</pre>
|
||||
<section id="exercises-6" data-type="sect2">
|
||||
<h2>
|
||||
Exercises</h2>
|
||||
<ol type="1"><li><p>Turn a stacked bar chart into a pie chart using <code><a href="#chp-https://ggplot2.tidyverse.org/reference/coord_polar" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/coord_polar</a></code>.</p></li>
|
||||
<li><p>What does <code><a href="#chp-https://ggplot2.tidyverse.org/reference/labs" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/labs</a></code> do? Read the documentation.</p></li>
|
||||
<li><p>What’s the difference between <code><a href="#chp-https://ggplot2.tidyverse.org/reference/coord_map" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/coord_map</a></code> and <code><a href="#chp-https://ggplot2.tidyverse.org/reference/coord_map" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/coord_map</a></code>?</p></li>
|
||||
<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>
|
||||
<li><p>What does <code><a href="https://ggplot2.tidyverse.org/reference/labs.html">labs()</a></code> do? Read the documentation.</p></li>
|
||||
<li><p>What’s the difference between <code><a href="https://ggplot2.tidyverse.org/reference/coord_map.html">coord_quickmap()</a></code> and <code><a href="https://ggplot2.tidyverse.org/reference/coord_map.html">coord_map()</a></code>?</p></li>
|
||||
<li>
|
||||
<p>What does the plot below tell you about the relationship between city and highway mpg? Why is <code><a href="#chp-https://ggplot2.tidyverse.org/reference/coord_fixed" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/coord_fixed</a></code> important? What does <code><a href="#chp-https://ggplot2.tidyverse.org/reference/geom_abline" data-type="xref">#chp-https://ggplot2.tidyverse.org/reference/geom_abline</a></code> do?</p>
|
||||
<p>What does the plot below tell you about the relationship between city and highway mpg? Why is <code><a href="https://ggplot2.tidyverse.org/reference/coord_fixed.html">coord_fixed()</a></code> important? What does <code><a href="https://ggplot2.tidyverse.org/reference/geom_abline.html">geom_abline()</a></code> do?</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">ggplot(data = mpg, mapping = aes(x = cty, y = hwy)) +
|
||||
geom_point() +
|
||||
@@ -823,14 +823,14 @@ The layered grammar of graphics</h1>
|
||||
</div>
|
||||
</div>
|
||||
<p>You could use this method to build <em>any</em> plot that you imagine. In other words, you can use the code template that you’ve learned in this chapter to build hundreds of thousands of unique plots.</p>
|
||||
<p>If you’d like to learn more about this theoretical underpinnings of ggplot2, you might enjoy reading “<a href="#chp-https://vita.had.co.nz/papers/layered-grammar" data-type="xref">#chp-https://vita.had.co.nz/papers/layered-grammar</a>”, the scientific paper that describes the theory of ggplot2 in detail.</p>
|
||||
<p>If you’d like to learn more about this 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">
|
||||
<h1>
|
||||
Summary</h1>
|
||||
<p>In this chapter, you’ve learn the basics of data visualization with ggplot2. We started with the basic idea that underpins ggplot2: a visualization is a mapping from variables in your data to aesthetic properties like position, colour, size and shape. You then learned about facets, which allow you to create small multiples, where each panel contains a subgroup from your data. We then gave you a whirlwind tour of the geoms and stats which control the “type” of graph you get, whether it’s a scatterplot, line plot, histogram, or something else. Position adjustment control 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.</p>
|
||||
<p>We’ll use visualizations again and again through out this book, introducing new techniques as we need them. If you want to get a comprehensive understand of ggplot2, we recommend reading the book, <a href="#chp-https://ggplot2-book" data-type="xref">#chp-https://ggplot2-book</a>. Other useful resources are the <a href="#chp-https://r-graphics" data-type="xref">#chp-https://r-graphics</a> by Winston Chang and <a href="#chp-https://clauswilke.com/dataviz/" data-type="xref">#chp-https://clauswilke.com/dataviz/</a> by Claus Wilke.</p>
|
||||
<p>We’ll use visualizations again and again through out this book, introducing new techniques as we need them. If you want to get a comprehensive understand of ggplot2, we recommend reading the book, <a href="https://ggplot2-book.org"><em>ggplot2: Elegant Graphics for Data Analysis</em></a>. Other useful resources are the <a href="https://r-graphics.org"><em>R Graphics Cookbook</em></a> by Winston Chang and <a href="https://clauswilke.com/dataviz/"><em>Fundamentals of Data Visualization</em></a> by Claus Wilke.</p>
|
||||
<p>With the basics of visualization under your belt, in the next chapter we’re going to switch gears a little and give you some practical workflow advice. We intersperse workflow advice with data science tools throughout this part of the book because it’ll help you stay organize as you write increasing amounts of R code.</p>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user