-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5050335
commit b796030
Showing
106 changed files
with
24,869 additions
and
7,006 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/sh | ||
|
||
quarto render | ||
git add ./_site/ |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
17 changes: 17 additions & 0 deletions
17
_freeze/worksheets/aesthetic-mappings/execute-results/html.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"hash": "9f2190e288a31cf117ebd0da0fc766bb", | ||
"result": { | ||
"engine": "knitr", | ||
"markdown": "---\ntitle: \"Aesthetic mappings\"\nauthor: \"Claus O. Wilke\"\nformat: live-html\nengine: knitr\nwebr:\n render-df: gt-interactive\n---\n\n::: {.cell}\n\n:::\n\n\n\n\n\n## Introduction\n\nIn this worksheet, we will discuss a core concept of ggplot, the mapping of data values onto aesthetics.\n\nFirst we need to load the required R packages. Please wait a moment until the live R session is fully set up and all packages are loaded.\n\n\n\n\n::: {.cell edit='false'}\n```{webr}\n#| warning: false\n#| edit: false\nlibrary(tidyverse)\n```\n:::\n\n\n\n\nNext we set up the data.\n\n\n\n\n::: {.cell edit='false'}\n```{webr}\n#| edit: false\n#| warning: false\ntemperatures <- read_csv(\"https://wilkelab.org/SDS375/datasets/tempnormals.csv\") |>\n mutate(\n location = factor(\n location, levels = c(\"Death Valley\", \"Houston\", \"San Diego\", \"Chicago\")\n )\n ) |>\n select(location, day_of_year, month, temperature)\n\ntemps_houston <- filter(temperatures, location == \"Houston\")\n```\n:::\n\n\n\n\nWe will first work with the dataset `temps_houston` which contains the average temperature for each day of the year for Houston, TX.\n\n\n\n::: {.cell edit='false' autorun='true'}\n```{webr}\n#| edit: false\n#| autorun: true\ntemps_houston\n```\n:::\n\n\n\n\n## Basic use of ggplot\n\nIn the most basic use of ggplot, we call the `ggplot()` function with a dataset and an aesthetic mapping (created with `aes()`), and then we add a geom, such as `geom_line()` to draw lines or `geom_point()` to draw points.\n\nTry this for yourself. Map the column `day_of_year` onto the x axis and the column `temperature` onto the y axis, and use `geom_line()` to display the data.\n\n\n\n\n::: {.cell exercise='ggplot'}\n```{webr}\n#| exercise: ggplot\nggplot(temps_houston, aes(x = ___, y = ___)) +\n ___()\n```\n:::\n\n\n\n\n::: { .solution exercise=\"ggplot\" }\n::: { .callout-tip title=\"Solution\" collapse=\"false\"}\n```r\nggplot(temps_houston, aes(x = day_of_year, y = temperature)) +\n geom_line()\n```\n:::\n:::\n\nTry again. Now use `geom_point()` instead of `geom_line()`.\n\n\n\n\n::: {.cell exercise='ggplot2'}\n```{webr}\n#| exercise: ggplot2\nggplot(temps_houston, aes(x = ___, y = ___)) +\n ___()\n```\n:::\n\n\n\n\n::: { .solution exercise=\"ggplot2\" }\n::: { .callout-tip title=\"Solution\" collapse=\"false\"}\n```r\nggplot(temps_houston, aes(x = day_of_year, y = temperature)) +\n geom_point()\n```\n:::\n:::\n\nAnd now swap which column you map to x and which to y.\n\n\n\n\n::: {.cell exercise='ggplot3'}\n```{webr}\n#| exercise: ggplot3\nggplot(temps_houston, aes(x = ___, y = ___)) +\n ___()\n```\n:::\n\n\n\n\n::: { .solution exercise=\"ggplot3\" }\n::: { .callout-tip title=\"Solution\" collapse=\"false\"}\n```r\nggplot(temps_houston, aes(x = temperature, y = day_of_year)) +\n geom_point()\n```\n:::\n:::\n\n## More complex geoms\n\nYou can use other geoms to make different types of plots. For example, `geom_boxplot()` will make boxplots. For boxplots, we frequently want categorical data on the x or y axis. For example, we might want a separate boxplot for each month. Try this out. Puth `month` on the x axis, `temperature` on the y axis, and use `geom_boxplot()`.\n\n\n\n\n::: {.cell exercise='ggplot-boxplot'}\n```{webr}\n#| exercise: ggplot-boxplot\nggplot(temps_houston, aes(x = ___, y = ___)) +\n ___()\n```\n:::\n\n\n\n\n\n::: { .hint exercise=\"ggplot-boxplot\" }\n::: { .callout-tip title=\"Hint\" collapse=\"false\"}\n```r\nggplot(temps_houston, aes(x = month, y = temperature)) +\n ___()\n```\n:::\n:::\n\n::: { .solution exercise=\"ggplot-boxplot\" }\n::: { .callout-tip title=\"Solution\" collapse=\"false\"}\n```r\nggplot(temps_houston, aes(x = month, y = temperature)) +\n geom_boxplot()\n```\n:::\n:::\n\nNow put the month on the y axis and the temperature on the x axis.\n\n\n\n\n::: {.cell exercise='ggplot-boxplot2'}\n```{webr}\n#| exercise: ggplot-boxplot2\nggplot(___) +\n ___()\n```\n:::\n\n\n\n\n::: { .hint exercise=\"ggplot-boxplot2\" }\n::: { .callout-tip title=\"Hint\" collapse=\"false\"}\n```r\nggplot(temps_houston, aes(x = ___, y = ___)) +\n geom_boxplot()\n```\n:::\n:::\n\n::: { .solution exercise=\"ggplot-boxplot2\" }\n::: { .callout-tip title=\"Solution\" collapse=\"false\"}\n```r\nggplot(temps_houston, aes(x = temperature, y = month)) +\n geom_boxplot()\n```\n:::\n:::\n\n\n## Adding color\n\nNext we will be working with the dataset `temperatures`, which is similar to `temps_houston` but contains data for three more locations.\n\n\n\n\n::: {.cell edit='false' autorun='true'}\n```{webr}\n#| edit: false\n#| autorun: true\ntemperatures\n```\n:::\n\n\n\n\nMake a line plot of `temperature` against `day_of_year`, using the `color` aesthetic to color the lines by location. \n\n\n\n\n::: {.cell exercise='ggplot-color'}\n```{webr}\n#| exercise: ggplot-color\nggplot(temperatures, aes(x = ___, y = ___, color = ___)) +\n ___()\n```\n:::\n\n\n\n\n::: { .hint exercise=\"ggplot-color\" }\n::: { .callout-tip title=\"Hint\" collapse=\"false\"}\n```r\nggplot(temperatures, aes(x = day_of_year, y = temperature, color = ___)) +\n geom_line()\n```\n:::\n:::\n\n::: { .solution exercise=\"ggplot-color\" }\n::: { .callout-tip title=\"Solution\" collapse=\"false\"}\n```r\nggplot(temperatures, aes(x = day_of_year, y = temperature, color = location)) +\n geom_line()\n```\n:::\n:::\n\n\nTry again, this time using `location` as the location along the y axis and `temperature` for the color. This plot looks better with `geom_point()` than `geom_line()`. (Try it out to see why. Also, try `geom_point(size = 5)` to create larger points.)\n\n\n\n\n::: {.cell exercise='ggplot-color2'}\n```{webr}\n#| exercise: ggplot-color2\nggplot(___) +\n ___()\n```\n:::\n\n\n\n\n::: { .hint exercise=\"ggplot-color2\" }\n::: { .callout-tip title=\"Hint\" collapse=\"false\"}\n```r\nggplot(temperatures, aes(x = ___, y = ___, color = ___)) +\n geom_point()\n```\n:::\n:::\n\n::: { .solution exercise=\"ggplot-color2\" }\n::: { .callout-tip title=\"Solution\" collapse=\"false\"}\n```r\nggplot(temperatures, aes(x = day_of_year, y = location, color = temperature)) +\n geom_point()\n```\n:::\n:::\n\n\n\n## Using the `fill` aesthetic\n\nSome geoms use a `fill` aesthetic, which is similar to `color` but applies to shaded areas. (`color` applies to lines and points.) For example, we can use the `fill` aesthetic with `geom_boxplot()` to color the interior of the box. Try this yourself. Plot `month` on x, `temperature` on y, and color the interior of the box by location.\n\n\n\n\n::: {.cell exercise='ggplot-fill'}\n```{webr}\n#| exercise: ggplot-fill\nggplot(temperatures, ___) +\n ___()\n```\n:::\n\n\n\n\n::: { .hint exercise=\"ggplot-fill\" }\n::: { .callout-tip title=\"Hint\" collapse=\"false\"}\n```r\nggplot(temperatures, aes(x = month, y = ___, fill = ___)) +\n geom_boxplot()\n```\n:::\n:::\n\n::: { .solution exercise=\"ggplot-fill\" }\n::: { .callout-tip title=\"Solution\" collapse=\"false\"}\n```r\nggplot(temperatures, aes(x = month, y = temperature, fill = location)) +\n geom_boxplot()\n```\n:::\n:::\n\nCan you color the lines of the boxplot by location and the interior by month? Try it.\n\n\n\n\n::: {.cell exercise='ggplot-color-fill'}\n```{webr}\n#| exercise: ggplot-color-fill\nggplot(temperatures, ___) +\n ___()\n```\n:::\n\n\n\n\n::: { .hint exercise=\"ggplot-color-fill\" }\n::: { .callout-tip title=\"Hint\" collapse=\"false\"}\n```r\nggplot(temperatures, aes(x = month, y = temperature, color = ___, fill = ___)) +\n geom_boxplot()\n```\n:::\n:::\n\n::: { .solution exercise=\"ggplot-color-fill\" }\n::: { .callout-tip title=\"Solution\" collapse=\"false\"}\n```r\nggplot(temperatures, aes(x = month, y = temperature, color = location, fill = month)) +\n geom_boxplot()\n```\n:::\n:::\n\n\n## Using aesthetics as parameters\n\nMany of the aesthetics (such as `color`, `fill`, and also `size` to change line size or point thickness) can be used as parameters inside a geom rather than inside an `aes()` statement. The difference is that when you use an aesthetic as a parameter, you specify a specific value, such as `color = \"blue\"`, rather than a mapping, such as `aes(color = location)`. Notice the difference: Inside the `aes()` function, we don't actually specify the specific color values, ggplot does that for us. We only say that we want the data values of the `location` column to correspond to different colors. (We will learn later how to tell ggplot to use specific colors in this mapping.)\n\nTry this with the boxplot example from the previous section. Map `location` onto the `fill` aesthetic but set the color of the lines to `\"navyblue\"`.\n\n\n\n\n::: {.cell exercise='ggplot-params'}\n```{webr}\n#| exercise: ggplot-params\nggplot(temperatures, ___) +\n ___()\n```\n:::\n\n\n\n\n::: { .hint exercise=\"ggplot-params\" }\n::: { .callout-tip title=\"Hint\" collapse=\"false\"}\n```r\nggplot(temperatures, aes(x = month, y = temperature, fill = ___)) +\n geom_boxplot(color = ___)\n```\n:::\n:::\n\n::: { .solution exercise=\"ggplot-params\" }\n::: { .callout-tip title=\"Solution\" collapse=\"false\"}\n```r\nggplot(temperatures, aes(x = month, y = temperature, fill = location)) +\n geom_boxplot(color = \"navyblue\")\n```\n:::\n:::\n\n\nNow do the reverse. Map `location` onto the line colors but fill the box with the color `\"navyblue\"`.\n\n\n\n\n::: {.cell exercise='ggplot-params2'}\n```{webr}\n#| exercise: ggplot-params2\nggplot(temperatures, ___) +\n ___()\n```\n:::\n\n\n\n\n::: { .hint exercise=\"ggplot-params2\" }\n::: { .callout-tip title=\"Hint\" collapse=\"false\"}\n```r\nggplot(temperatures, aes(x = month, y = temperature, color = ___)) +\n geom_boxplot(fill = ___)\n```\n:::\n:::\n\n::: { .solution exercise=\"ggplot-params2\" }\n::: { .callout-tip title=\"Solution\" collapse=\"false\"}\n```r\nggplot(temperatures, aes(x = month, y = temperature, color = location)) +\n geom_boxplot(fill = \"navyblue\")\n```\n:::\n:::\n", | ||
"supporting": [ | ||
"aesthetic-mappings_files" | ||
], | ||
"filters": [ | ||
"rmarkdown/pagebreak.lua" | ||
], | ||
"includes": {}, | ||
"engineDependencies": {}, | ||
"preserve": {}, | ||
"postProcess": true | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.