RWEP/_freeze/SD/20240328_3_datavisualize/index/execute-results/html.json

22 lines
93 KiB
JSON
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"hash": "1f512aedde0c78d18d7113364c7ce0db",
"result": {
"engine": "knitr",
"markdown": "---\ntitle: \"数据可视化\"\nsubtitle: 《区域水环境污染数据分析实践》<br>Data analysis practice of regional water environment pollution\nauthor: 苏命、王为东<br>中国科学院大学资源与环境学院<br>中国科学院生态环境研究中心\ndate: today\nlang: zh\nformat:\n revealjs:\n theme: dark\n slide-number: true\n chalkboard:\n buttons: true\n preview-links: auto\n lang: zh\n toc: true\n toc-depth: 1\n toc-title: 大纲\n logo: ./_extensions/inst/img/ucaslogo.png\n css: ./_extensions/inst/css/revealjs.css\n pointer:\n key: \"p\"\n color: \"#32cd32\"\n pointerSize: 18\nrevealjs-plugins:\n - pointer\nfilters:\n - d2\nknitr:\n opts_chunk:\n dev: \"svg\"\n retina: 3\nexecute:\n freeze: auto\n cache: true\n echo: true\n fig-width: 5\n fig-height: 6\n---\n\n\n\n\n\n\n## {background-image=\"../../img/concepts/tidyverse-packages-ggplot.png\" background-position=\"center\" background-size=\"100%\"}\n\n\n\n## The ggplot2 Package\n\n<br>\n\n... is an **R package to visualize data** created by Hadley Wickham in 2005\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# install.packages(\"ggplot2\")\nlibrary(ggplot2)\n```\n:::\n\n\n<br>\n\n::: fragment\n... is part of the [`{tidyverse}`](https://www.tidyverse.org/)\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# install.packages(\"tidyverse\")\nlibrary(tidyverse)\n```\n:::\n\n:::\n\n# The Grammar of {ggplot2}\n\n\n\n## The Grammar of {ggplot2}\n\n<br>\n<table style='width:100%;font-size:14pt;'>\n <tr>\n <th>Component</th>\n <th>Function</th>\n <th>Explanation</th>\n </tr>\n <tr>\n <td><b style='color:#67676;'>Data</b></td>\n <td><code>ggplot(data)</code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>\n <td>*The raw data that you want to visualise.*</td>\n </tr>\n <tr>\n <td><b style='color:#67676;'>Aesthetics&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></td>\n <td><code>aes()</code></td>\n <td>*Aesthetic mappings between variables and visual properties.*</td>\n <tr>\n <td><b style='color:#67676;'>Geometries</b></td>\n <td><code>geom_*()</code></td>\n <td>*The geometric shapes representing the data.*</td>\n </tr>\n</table>\n\n\n\n## The Grammar of {ggplot2}\n\n\n<br>\n<table style='width:100%;font-size:14pt;'>\n <tr>\n <th>Component</th>\n <th>Function</th>\n <th>Explanation</th>\n </tr>\n <tr>\n <td><b style='color:#67676;'>Data</b></td>\n <td><code>ggplot(data)</code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>\n <td>*The raw data that you want to visualise.*</td>\n </tr>\n <tr>\n <td><b style='color:#67676;'>Aesthetics&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></td>\n <td><code>aes()</code></td>\n <td>*Aesthetic mappings between variables and visual properties.*</td>\n <tr>\n <td><b style='color:#67676;'>Geometries</b></td>\n <td><code>geom_*()</code></td>\n <td>*The geometric shapes representing the data.*</td>\n </tr>\n <tr>\n <td><b style='color:#67676;'>Statistics</b></td>\n <td><code>stat_*()</code></td>\n <td>*The statistical transformations applied to the data.*</td>\n </tr>\n <tr>\n <td><b style='color:#67676;'>Scales</b></td>\n <td><code>scale_*()</code></td>\n <td>*Maps between the data and the aesthetic dimensions.*</td>\n </tr>\n <tr>\n <td><b style='color:#67676;'>Coordinate System</b></td>\n <td><code>coord_*()</code></td>\n <td>*Maps data into the plane of the data rectangle.*</td>\n </tr>\n <tr>\n <td><b style='color:#67676;'>Facets</b></td>\n <td><code>facet_*()</code></td>\n <td>*The arrangement of the data into a grid of plots.*</td>\n </tr>\n <tr>\n <td><b style='color:#67676;'>Visual Themes</b></td>\n <td><code>theme() / theme_*()</code></td>\n <td>*The overall visual defaults of a plot.*</td>\n </tr>\n</table>\n\n\n\n## The Data\n\n<b style='font-size:2.3rem;'>Bike sharing counts in London, UK, powered by [TfL Open Data](https://tfl.gov.uk/modes/cycling/santander-cycles)</b>\n\n::: incremental\n- covers the years 2015 and 2016\n- incl. weather data acquired from [freemeteo.com](https://freemeteo.com)\n- prepared by Hristo Mavrodiev for [Kaggle](https://www.kaggle.com/hmavrodiev/london-bike-sharing-dataset)\n:::\n\n<br>\n\n::: fragment\n\n::: {.cell}\n\n```{.r .cell-code}\nbikes <- readr::read_csv(\"../../data/ggplot2/london-bikes-custom.csv\",\n ## or: \"https://raw.githubusercontent.com/z3tt/graphic-design-ggplot2/main/data/london-bikes-custom.csv\"\n col_types = \"Dcfffilllddddc\"\n)\n\nbikes$season <- forcats::fct_inorder(bikes$season)\n```\n:::\n\n:::\n\n------------------------------------------------------------------------\n\n\n::: {.cell}\n::: {.cell-output-display}\n`````{=html}\n<table class=\"table lightable-minimal lightable-hover\" style=\"font-size: 20px; margin-left: auto; margin-right: auto; font-family: Spline Sans Mono; \">\n <thead>\n <tr>\n <th style=\"text-align:left;\"> Variable </th>\n <th style=\"text-align:left;\"> Description </th>\n <th style=\"text-align:left;\"> Class </th>\n </tr>\n </thead>\n<tbody>\n <tr>\n <td style=\"text-align:left;\"> date </td>\n <td style=\"text-align:left;\"> Date encoded as `YYYY-MM-DD` </td>\n <td style=\"text-align:left;\"> date </td>\n </tr>\n <tr>\n <td style=\"text-align:left;\"> day_night </td>\n <td style=\"text-align:left;\"> `day` (6:00am5:59pm) or `night` (6:00pm5:59am) </td>\n <td style=\"text-align:left;\"> character </td>\n </tr>\n <tr>\n <td style=\"text-align:left;\"> year </td>\n <td style=\"text-align:left;\"> `2015` or `2016` </td>\n <td style=\"text-align:left;\"> factor </td>\n </tr>\n <tr>\n <td style=\"text-align:left;\"> month </td>\n <td style=\"text-align:left;\"> `1` (January) to `12` (December) </td>\n <td style=\"text-align:left;\"> factor </td>\n </tr>\n <tr>\n <td style=\"text-align:left;\"> season </td>\n <td style=\"text-align:left;\"> `winter`, `spring`, `summer`, or `autumn` </td>\n <td style=\"text-align:left;\"> factor </td>\n </tr>\n <tr>\n <td style=\"text-align:left;\"> count </td>\n <td style=\"text-align:left;\"> Sum of reported bikes rented </td>\n <td style=\"text-align:left;\"> integer </td>\n </tr>\n <tr>\n <td style=\"text-align:left;\"> is_workday </td>\n <td style=\"text-align:left;\"> `TRUE` being Monday to Friday and no bank holiday </td>\n <td style=\"text-align:left;\"> logical </td>\n </tr>\n <tr>\n <td style=\"text-align:left;\"> is_weekend </td>\n <td style=\"text-align:left;\"> `TRUE` being Saturday or Sunday </td>\n <td style=\"text-align:left;\"> logical </td>\n </tr>\n <tr>\n <td style=\"text-align:left;\"> is_holiday </td>\n <td style=\"text-align:left;\"> `TRUE` being a bank holiday in the UK </td>\n <td style=\"text-align:left;\"> logical </td>\n </tr>\n <tr>\n <td style=\"text-align:left;\"> temp </td>\n <td style=\"text-align:left;\"> Average air temperature (°C) </td>\n <td style=\"text-align:left;\"> double </td>\n </tr>\n <tr>\n <td style=\"text-align:left;\"> temp_feel </td>\n <td style=\"text-align:left;\"> Average feels like temperature (°C) </td>\n <td style=\"text-align:left;\"> double </td>\n </tr>\n <tr>\n <td style=\"text-align:left;\"> humidity </td>\n <td style=\"text-align:left;\"> Average air humidity (%) </td>\n <td style=\"text-align:left;\"> double </td>\n </tr>\n <tr>\n <td style=\"text-align:left;\"> wind_speed </td>\n <td style=\"text-align:left;\"> Average wind speed (km/h) </td>\n <td style=\"text-align:left;\"> double </td>\n </tr>\n <tr>\n <td style=\"text-align:left;\"> weather_type </td>\n <td style=\"text-align:left;\"> Most common weather type </td>\n <td style=\"text-align:left;\"> character </td>\n </tr>\n</tbody>\n</table>\n\n`````\n:::\n:::\n\n\n## `ggplot2::ggplot()`\n\n\n::: {.cell}\n\n:::\n\n\n![](../../img/concepts/ggplot-fun-help.png){fig-alt=\"The help page of the ggplot() function.\" fig-width=\"175%\"}\n\n## Data\n\n\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code}\nggplot(data = bikes)\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/ggplot-data-1.svg){width=480}\n:::\n:::\n\n\n## Aesthetic Mapping视觉映射`aes(.)`\n\n<br>\n\n<b class='simple-highlight-grn' style='font-size:2.6rem;'>= link variables to graphical properties</b><br><br>\n\n::: incremental\n- positions (`x`, `y`)\n- colors (`color`, `fill`)\n- shapes (`shape`, `linetype`)\n- size (`size`)\n- transparency (`alpha`)\n- groupings (`group`)\n:::\n\n## Aesthetic Mapping视觉映射`aes(.)`\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"2|1,2\"}\nggplot(data = bikes) +\n aes(x = temp_feel, y = count)\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/ggplot-aesthetics-outside-1.svg){width=480}\n:::\n:::\n\n\n## <span style='color:#4758AB;'>aes</span>thetics\n\n`aes()` outside as component\n\n\n::: {.cell}\n\n```{.r .cell-code}\nggplot(data = bikes) +\n aes(x = temp_feel, y = count)\n```\n:::\n\n\n<br>\n\n::: fragment\n`aes()` inside, explicit matching\n\n\n::: {.cell}\n\n```{.r .cell-code}\nggplot(data = bikes, mapping = aes(x = temp_feel, y = count))\n```\n:::\n\n\n<br>\n:::\n\n::: fragment\n`aes()` inside, implicit matching\n\n\n::: {.cell}\n\n```{.r .cell-code}\nggplot(bikes, aes(temp_feel, count))\n```\n:::\n\n\n<br>\n:::\n\n::: fragment\n`aes()` inside, mixed matching\n\n\n::: {.cell}\n\n```{.r .cell-code}\nggplot(bikes, aes(x = temp_feel, y = count))\n```\n:::\n\n:::\n\n# Geometrical Layers \n\n## Geometries几何图层geom_*\n\n<br>\n\n<b class='simple-highlight-grn' style='font-size:2.6rem;'>= interpret aesthetics as graphical representations</b><br><br>\n\n::: incremental\n- points\n- lines\n- polygons\n- text labels\n- ...\n:::\n\n## Geometries几何图层geom_*\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"1,2,3,4|5\"}\nggplot(\n bikes,\n aes(x = temp_feel, y = count)\n ) +\n geom_point()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/geom-point-1.svg){width=480}\n:::\n:::\n\n\n## Visual Properties of Layers图层属性\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"5,6,7,8,9,10,11|6,7,8,9,10\"}\nggplot(\n bikes,\n aes(x = temp_feel, y = count)\n ) +\n geom_point(\n color = \"#28a87d\",\n alpha = .5,\n shape = \"X\",\n stroke = 1,\n size = 4\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/geom-point-properties-1.svg){width=480}\n:::\n:::\n\n\n## Setting vs Mapping of Visual Properties\n\n::: {layout-ncol=\"2\"}\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"6\"}\nggplot(\n bikes,\n aes(x = temp_feel, y = count)\n ) +\n geom_point(\n color = \"#28a87d\",\n alpha = .5\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/geom-point-properties-set-1.svg){width=480}\n:::\n:::\n\n\n::: fragment\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"6\"}\nggplot(\n bikes,\n aes(x = temp_feel, y = count)\n ) +\n geom_point(\n aes(color = season),\n alpha = .5\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/geom-point-properties-map-1.svg){width=480}\n:::\n:::\n\n:::\n:::\n\n## Mapping Expressions\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"6\"}\nggplot(\n bikes,\n aes(x = temp_feel, y = count)\n ) +\n geom_point(\n aes(color = temp_feel > 20),\n alpha = .5\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/geom-point-aes-expression-1.svg){width=480}\n:::\n:::\n\n\n## Filter Data\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"2\"}\nggplot(\n filter(bikes, !is.na(weather_type)),\n aes(x = temp, y = temp_feel)\n ) +\n geom_point(\n aes(color = weather_type == \"clear\",\n size = count),\n shape = 18,\n alpha = .5\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/geom-point-aes-expression-exercise-na-1.svg){width=480}\n:::\n:::\n\n\n## Filter Data\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"2\"}\nggplot(\n bikes %>% filter(!is.na(weather_type)),\n aes(x = temp, y = temp_feel)\n ) +\n geom_point(\n aes(color = weather_type == \"clear\",\n size = count),\n shape = 18,\n alpha = .5\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/geom-point-aes-expression-exercise-na-pipe-1.svg){width=480}\n:::\n:::\n\n\n\n\n## Local vs. Global应用至当前图层或所有图层\n\n::: {layout-ncol=\"2\"}\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"3,6\"}\nggplot(\n bikes,\n aes(x = temp_feel, y = count)\n ) +\n geom_point(\n aes(color = season),\n alpha = .5\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/geom-point-aes-geom-1.svg){width=480}\n:::\n:::\n\n\n::: fragment\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"3,4\"}\nggplot(\n bikes,\n aes(x = temp_feel, y = count,\n color = season)\n ) +\n geom_point(\n alpha = .5\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/geom-point-aes-global-1.svg){width=480}\n:::\n:::\n\n:::\n:::\n\n## Adding More Layers\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"9,10,11\"}\nggplot(\n bikes,\n aes(x = temp_feel, y = count,\n color = season)\n ) +\n geom_point(\n alpha = .5\n ) +\n geom_smooth(\n method = \"lm\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/geom-smooth-1.svg){width=480}\n:::\n:::\n\n\n## Global Color Encoding\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"3,4,9,10,11\"}\nggplot(\n bikes,\n aes(x = temp_feel, y = count,\n color = season)\n ) +\n geom_point(\n alpha = .5\n ) +\n geom_smooth(\n method = \"lm\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/geom-smooth-aes-global-1.svg){width=480}\n:::\n:::\n\n\n## Local Color Encoding\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"6,9,10,11\"}\nggplot(\n bikes,\n aes(x = temp_feel, y = count)\n ) +\n geom_point(\n aes(color = season),\n alpha = .5\n ) +\n geom_smooth(\n method = \"lm\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/geom-smooth-aes-fixed-1.svg){width=480}\n:::\n:::\n\n\n## The \\`group\\` Aesthetic\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"10\"}\nggplot(\n bikes,\n aes(x = temp_feel, y = count)\n ) +\n geom_point(\n aes(color = season),\n alpha = .5\n ) +\n geom_smooth(\n aes(group = day_night),\n method = \"lm\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/geom-smooth-aes-grouped-1.svg){width=480}\n:::\n:::\n\n\n## Set Both as Global Aesthetics\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"4,5\"}\nggplot(\n bikes,\n aes(x = temp_feel, y = count,\n color = season,\n group = day_night)\n ) +\n geom_point(\n alpha = .5\n ) +\n geom_smooth(\n method = \"lm\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/geom-smooth-aes-global-grouped-1.svg){width=480}\n:::\n:::\n\n\n## Overwrite Global Aesthetics\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"4,12\"}\nggplot(\n bikes,\n aes(x = temp_feel, y = count,\n color = season,\n group = day_night)\n ) +\n geom_point(\n alpha = .5\n ) +\n geom_smooth(\n method = \"lm\",\n color = \"black\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/geom-smooth-aes-global-grouped-overwrite-1.svg){width=480}\n:::\n:::\n\n\n\n\n# Statistical Layers\n\n\n## \\`stat_\\*()\\` and \\`geom_\\*()\\`\n\n::: {layout-ncol=\"2\"}\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"2\"}\nggplot(bikes, aes(x = temp_feel, y = count)) +\n stat_smooth(geom = \"smooth\")\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/stat-geom-1.svg){width=480}\n:::\n:::\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"2\"}\nggplot(bikes, aes(x = temp_feel, y = count)) +\n geom_smooth(stat = \"smooth\")\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/geom-stat-1.svg){width=480}\n:::\n:::\n\n:::\n\n\n## \\`stat_\\*()\\` and \\`geom_\\*()\\`\n\n::: {layout-ncol=\"2\"}\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"2\"}\nggplot(bikes, aes(x = season)) +\n stat_count(geom = \"bar\")\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/stat-geom-2-1.svg){width=480}\n:::\n:::\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"2\"}\nggplot(bikes, aes(x = season)) +\n geom_bar(stat = \"count\")\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/geom-stat-2-1.svg){width=480}\n:::\n:::\n\n:::\n\n\n## \\`stat_\\*()\\` and \\`geom_\\*()\\`\n\n::: {layout-ncol=\"2\"}\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"2\"}\nggplot(bikes, aes(x = date, y = temp_feel)) +\n stat_identity(geom = \"point\")\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/stat-geom-3-1.svg){width=480}\n:::\n:::\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"2\"}\nggplot(bikes, aes(x = date, y = temp_feel)) +\n geom_point(stat = \"identity\")\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/geom-stat-3-1.svg){width=480}\n:::\n:::\n\n:::\n\n## Statistical Summaries\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"5|3\"}\nggplot(\n bikes, \n aes(x = season, y = temp_feel)\n ) +\n stat_summary() \n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/stat-summary-1.svg){width=480}\n:::\n:::\n\n\n\n## Statistical Summaries\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"6,7\"}\nggplot(\n bikes, \n aes(x = season, y = temp_feel)\n ) +\n stat_summary(\n fun.data = mean_se, ## the default\n geom = \"pointrange\" ## the default\n ) \n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/stat-summary-defaults-1.svg){width=480}\n:::\n:::\n\n\n\n## Statistical Summaries\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"5|5,6,11|6,7,8,9,10,11|7,8\"}\nggplot(\n bikes, \n aes(x = season, y = temp_feel)\n ) +\n geom_boxplot() +\n stat_summary(\n fun = mean,\n geom = \"point\",\n color = \"#28a87d\",\n size = 3\n ) \n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/stat-summary-median-1.svg){width=480}\n:::\n:::\n\n\n\n## Statistical Summaries\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"5,6,7,8,9|7,8\"}\nggplot(\n bikes, \n aes(x = season, y = temp_feel)\n ) +\n stat_summary(\n fun = mean, \n fun.max = function(y) mean(y) + sd(y), \n fun.min = function(y) mean(y) - sd(y) \n ) \n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/stat-summary-custom-1.svg){width=480}\n:::\n:::\n\n\n\n\n# Extending a ggplot\n\n## Store a ggplot as Object\n\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"1,16\"}\ng <-\n ggplot(\n bikes,\n aes(x = temp_feel, y = count,\n color = season,\n group = day_night)\n ) +\n geom_point(\n alpha = .5\n ) +\n geom_smooth(\n method = \"lm\",\n color = \"black\"\n )\n\nclass(g)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] \"gg\" \"ggplot\"\n```\n\n\n:::\n:::\n\n\n## Inspect a ggplot Object\n\n\n::: {.cell}\n\n```{.r .cell-code}\ng$data\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n# A tibble: 1,454 × 14\n date day_night year month season count is_workday is_weekend\n <date> <chr> <fct> <fct> <fct> <int> <lgl> <lgl> \n 1 2015-01-04 day 2015 1 winter 6830 FALSE TRUE \n 2 2015-01-04 night 2015 1 winter 2404 FALSE TRUE \n 3 2015-01-05 day 2015 1 winter 14763 TRUE FALSE \n 4 2015-01-05 night 2015 1 winter 5609 TRUE FALSE \n 5 2015-01-06 day 2015 1 winter 14501 TRUE FALSE \n 6 2015-01-06 night 2015 1 winter 6112 TRUE FALSE \n 7 2015-01-07 day 2015 1 winter 16358 TRUE FALSE \n 8 2015-01-07 night 2015 1 winter 4706 TRUE FALSE \n 9 2015-01-08 day 2015 1 winter 9971 TRUE FALSE \n10 2015-01-08 night 2015 1 winter 5630 TRUE FALSE \n# 1,444 more rows\n# 6 more variables: is_holiday <lgl>, temp <dbl>, temp_feel <dbl>,\n# humidity <dbl>, wind_speed <dbl>, weather_type <chr>\n```\n\n\n:::\n:::\n\n\n## Inspect a ggplot Object\n\n\n::: {.cell}\n\n```{.r .cell-code}\ng$mapping\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\nAesthetic mapping: \n* `x` -> `temp_feel`\n* `y` -> `count`\n* `colour` -> `season`\n* `group` -> `day_night`\n```\n\n\n:::\n:::\n\n\n## Extend a ggplot Object: Add Layers\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code}\ng +\n geom_rug(\n alpha = .2\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/ggplot-object-extend-geom-1.svg){width=480}\n:::\n:::\n\n\n## Remove a Layer from the Legend\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"4\"}\ng +\n geom_rug(\n alpha = .2,\n show.legend = FALSE\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/geom-guide-none-1.svg){width=480}\n:::\n:::\n\n\n## Extend a ggplot Object: Add Labels\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"2,3,4\"}\ng +\n xlab(\"Feels-like temperature (°F)\") +\n ylab(\"Reported bike shares\") +\n ggtitle(\"TfL bike sharing trends\")\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/ggplot-labs-individual-1.svg){width=480}\n:::\n:::\n\n\n## Extend a ggplot Object: Add Labels\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"2,3,4,5,6\"}\ng +\n labs(\n x = \"Feels-like temperature (°F)\",\n y = \"Reported bike shares\",\n title = \"TfL bike sharing trends\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/ggplot-labs-bundled-1.svg){width=480}\n:::\n:::\n\n\n## Extend a ggplot Object: Add Labels\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"6\"}\ng <- g +\n labs(\n x = \"Feels-like temperature (°F)\",\n y = \"Reported bike shares\",\n title = \"TfL bike sharing trends\",\n color = \"Season:\"\n )\n\ng\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/ggplot-labs-bundled-color-1.svg){width=480}\n:::\n:::\n\n\n## Extend a ggplot Object: Add Labels\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"6,7,9\"}\ng +\n labs(\n x = \"Feels-like temperature (°F)\",\n y = \"Reported bike shares\",\n title = \"TfL bike sharing trends\",\n subtitle = \"Reported bike rents versus feels-like temperature in London\",\n caption = \"Data: TfL\",\n color = \"Season:\",\n tag = \"Fig. 1\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/ggplot-labs-bundled-extended-1.svg){width=480}\n:::\n:::\n\n\n## Extend a ggplot Object: Add Labels\n\n::: {layout-ncol=\"2\"}\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"3\"}\ng +\n labs(\n x = \"\",\n caption = \"Data: TfL\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/ggplot-labs-empty-vs-null-A-1.svg){width=480}\n:::\n:::\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"3\"}\ng +\n labs(\n x = NULL,\n caption = \"Data: TfL\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/ggplot-labs-empty-vs-null-B-1.svg){width=480}\n:::\n:::\n\n:::\n\n## Extend a ggplot Object: Themes\n\n::: {layout-ncol=\"2\"}\n\n::: {.cell}\n\n```{.r .cell-code}\ng + theme_light()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/ggplot-object-extend-theme-light-1.svg){width=480}\n:::\n:::\n\n\n::: fragment\n\n::: {.cell}\n\n```{.r .cell-code}\ng + theme_minimal()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/ggplot-object-extend-theme-minimal-1.svg){width=480}\n:::\n:::\n\n:::\n:::\n\n## Change the Theme Base Settings\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"2,3|1,2,3,4\"}\ng + theme_light(\n base_size = 14\n)\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/ggplot-theme-extend-theme-base-1.svg){width=480}\n:::\n:::\n\n\n## Set a Theme Globally\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code}\ntheme_set(theme_light())\n\ng\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/ggplot-theme-global-1.svg){width=480}\n:::\n:::\n\n\n## Change the Theme Base Settings\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"2,3|1,2,3,4\"}\ntheme_set(theme_light(\n base_size = 14\n))\n\ng\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/ggplot-theme-global-base-1.svg){width=480}\n:::\n:::\n\n\n\n## Overwrite Specific Theme Settings\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"2|3\"}\ng +\n theme(\n panel.grid.minor = element_blank()\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/ggplot-theme-settings-individual-1-1.svg){width=480}\n:::\n:::\n\n\n## Overwrite Specific Theme Settings\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"4\"}\ng +\n theme(\n panel.grid.minor = element_blank(),\n plot.title = element_text(face = \"bold\")\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/ggplot-theme-settings-individual-2-1.svg){width=480}\n:::\n:::\n\n\n## Overwrite Specific Theme Settings\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"5\"}\ng +\n theme(\n panel.grid.minor = element_blank(),\n plot.title = element_text(face = \"bold\"),\n legend.position = \"top\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/ggplot-theme-settings-individual-3-1.svg){width=480}\n:::\n:::\n\n\n## Overwrite Specific Theme Settings\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"5\"}\ng +\n theme(\n panel.grid.minor = element_blank(),\n plot.title = element_text(face = \"bold\"),\n legend.position = \"none\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/ggplot-theme-settings-individual-legend-none-1.svg){width=480}\n:::\n:::\n\n\n## Overwrite Specific Theme Settings\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"6|2,3,4,6,7\"}\ng +\n theme(\n panel.grid.minor = element_blank(),\n plot.title = element_text(face = \"bold\"),\n legend.position = \"top\",\n plot.title.position = \"plot\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/ggplot-theme-settings-individual-4-1.svg){width=480}\n:::\n:::\n\n\n## Overwrite Theme Settings Globally\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"1|2,3,4,5|1,2,3,4,5,6\"}\ntheme_update(\n panel.grid.minor = element_blank(),\n plot.title = element_text(face = \"bold\"),\n legend.position = \"top\",\n plot.title.position = \"plot\"\n)\n\ng\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/ggplot-theme-settings-global-1.svg){width=480}\n:::\n:::\n\n\n## Save the Graphic\n\n\n::: {.cell}\n\n```{.r .cell-code}\nggsave(g, filename = \"my_plot.png\")\n```\n:::\n\n\n::: fragment\n\n::: {.cell}\n\n```{.r .cell-code}\nggsave(\"my_plot.png\")\n```\n:::\n\n:::\n\n::: fragment\n\n::: {.cell}\n\n```{.r .cell-code}\nggsave(\"my_plot.png\", width = 8, height = 5, dpi = 600)\n```\n:::\n\n:::\n\n::: fragment\n\n::: {.cell}\n\n```{.r .cell-code}\nggsave(\"my_plot.pdf\", width = 20, height = 12, unit = \"cm\", device = cairo_pdf)\n```\n:::\n\n:::\n\n::: fragment\n\n::: {.cell}\n\n```{.r .cell-code}\ngrDevices::cairo_pdf(\"my_plot.pdf\", width = 10, height = 7)\ng\ndev.off()\n```\n:::\n\n:::\n\n------------------------------------------------------------------------\n\n<br>\n\n![Modified from canva.com](../../img/concepts/vector-raster-canva.png){fig-alt=\"A comparison of vector and raster graphics.\" fig-width=\"150%\"}\n\n# Facets\n\n## Facets\n\n<br>\n\n<b class='simple-highlight-grn' style='font-size:2.6rem;'>= split variables to multiple panels</b><br><br>\n\n::: fragment\nFacets are also known as:\n\n- small multiples\n- trellis graphs\n- lattice plots\n- conditioning\n:::\n\n------------------------------------------------------------------------\n\n::: {layout-ncol=\"2\"}\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/facet-types-wrap-1.svg){width=480}\n:::\n:::\n\n\n::: fragment\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/facet-types-grid-1.svg){width=480}\n:::\n:::\n\n:::\n:::\n\n## Setup\n\n\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"1,2,3,4,5,6,7,8,9,10|12\"}\ng <-\n ggplot(\n bikes,\n aes(x = temp_feel, y = count,\n color = season)\n ) +\n geom_point(\n alpha = .3,\n guide = \"none\"\n )\n\ng\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/facet-setup-1.svg){width=480}\n:::\n:::\n\n\n## Wrapped Facets\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"1,2,3,4|2,4|3\"}\ng +\n facet_wrap(\n vars(day_night)\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/facet-wrap-1.svg){width=480}\n:::\n:::\n\n\n## Wrapped Facets\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"3\"}\ng +\n facet_wrap(\n ~ day_night\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/facet-wrap-circumflex-1.svg){width=480}\n:::\n:::\n\n\n## Facet Multiple Variables\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"3\"}\ng +\n facet_wrap(\n ~ is_workday + day_night\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/facet-wrap-multiple-1.svg){width=480}\n:::\n:::\n\n\n## Facet Options: Cols + Rows\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"4\"}\ng +\n facet_wrap(\n ~ day_night,\n ncol = 1\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/facet-wrap-options-ncol-1.svg){width=480}\n:::\n:::\n\n\n## Facet Options: Free Scaling\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"5\"}\ng +\n facet_wrap(\n ~ day_night,\n ncol = 1,\n scales = \"free\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/facet-wrap-options-scales-1.svg){width=480}\n:::\n:::\n\n\n## Facet Options: Free Scaling\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"5\"}\ng +\n facet_wrap(\n ~ day_night,\n ncol = 1,\n scales = \"free_y\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/facet-wrap-options-freey-1.svg){width=480}\n:::\n:::\n\n\n## Facet Options: Switch Labels\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"5\"}\ng +\n facet_wrap(\n ~ day_night,\n ncol = 1,\n switch = \"x\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/facet-wrap-options-switch-1.svg){width=480}\n:::\n:::\n\n\n## Gridded Facets\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"2,5|3,4\"}\ng +\n facet_grid(\n rows = vars(day_night),\n cols = vars(is_workday)\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/facet-grid-1.svg){width=480}\n:::\n:::\n\n\n## Gridded Facets\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"3\"}\ng +\n facet_grid(\n day_night ~ is_workday\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/facet-grid-circumflex-1.svg){width=480}\n:::\n:::\n\n\n## Facet Multiple Variables\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"3\"}\ng +\n facet_grid(\n day_night ~ is_workday + season\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/facet-grid-multiple-1.svg){width=480}\n:::\n:::\n\n\n## Facet Options: Free Scaling\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"4\"}\ng +\n facet_grid(\n day_night ~ is_workday,\n scales = \"free\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/facet-grid-options-scales-1.svg){width=480}\n:::\n:::\n\n\n## Facet Options: Switch Labels\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"5\"}\ng +\n facet_grid(\n day_night ~ is_workday,\n scales = \"free\",\n switch = \"y\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/facet-grid-options-switch-1.svg){width=480}\n:::\n:::\n\n\n## Facet Options: Proportional Spacing\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"4,5|5\"}\ng +\n facet_grid(\n day_night ~ is_workday,\n scales = \"free\",\n space = \"free\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/facet-grid-options-space-1.svg){width=480}\n:::\n:::\n\n\n## Facet Options: Proportional Spacing\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"4,5\"}\ng +\n facet_grid(\n day_night ~ is_workday,\n scales = \"free_y\",\n space = \"free_y\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/facet-grid-options-space-y-1.svg){width=480}\n:::\n:::\n\n\n## Diamonds Facet\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"1,2,3,4,5,6,7,8,9,10,11,12|8,9,10\"}\nggplot(\n diamonds,\n aes(x = carat, y = price)\n ) +\n geom_point(\n alpha = .3\n ) +\n geom_smooth(\n method = \"lm\",\n se = FALSE,\n color = \"dodgerblue\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/diamonds-facet-start-1.svg){width=480}\n:::\n:::\n\n\n## Diamonds Facet\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"13,14,15,16,17\"}\nggplot(\n diamonds,\n aes(x = carat, y = price)\n ) +\n geom_point(\n alpha = .3\n ) +\n geom_smooth(\n method = \"lm\",\n se = FALSE,\n color = \"dodgerblue\"\n ) +\n facet_grid(\n cut ~ clarity,\n space = \"free_x\",\n scales = \"free_x\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/diamonds-facet-1.svg){width=480}\n:::\n:::\n\n\n## Diamonds Facet (Dark Theme Bonus)\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"19,20,21,22\"}\nggplot(\n diamonds,\n aes(x = carat, y = price)\n ) +\n geom_point(\n alpha = .3,\n color = \"white\"\n ) +\n geom_smooth(\n method = \"lm\",\n se = FALSE,\n color = \"dodgerblue\"\n ) +\n facet_grid(\n cut ~ clarity,\n space = \"free_x\",\n scales = \"free_x\"\n ) +\n theme_dark(\n base_size = 14\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/diamonds-facet-dark-1.svg){width=480}\n:::\n:::\n\n\n# Scales尺度\n\n\n\n\n\n## Scales\n\n<br>\n\n<b class='simple-highlight-grn' style='font-size:2.6rem;'>= translate between variable ranges and property ranges</b><br><br>\n\n::: incremental\n- feels-like temperature  ⇄  x\n- reported bike shares  ⇄  y\n- season  ⇄  color\n- year  ⇄  shape\n- ...\n:::\n\n## Scales\n\nThe `scale_*()` components control the properties of all the<br><b class='simple-highlight-ylw'>aesthetic dimensions mapped to the data.</b>\n\n<br>Consequently, there are `scale_*()` functions for all aesthetics such as:\n\n- **positions** via `scale_x_*()` and `scale_y_*()`\n\n- **colors** via `scale_color_*()` and `scale_fill_*()`\n\n- **sizes** via `scale_size_*()` and `scale_radius_*()`\n\n- **shapes** via `scale_shape_*()` and `scale_linetype_*()`\n\n- **transparency** via `scale_alpha_*()`\n\n## Scales\n\nThe `scale_*()` components control the properties of all the<br><b class='simple-highlight-ylw'>aesthetic dimensions mapped to the data.</b>\n\n<br>The extensions (`*`) can be filled by e.g.:\n\n- `continuous()`, `discrete()`, `reverse()`, `log10()`, `sqrt()`, `date()` for positions\n\n- `continuous()`, `discrete()`, `manual()`, `gradient()`, `gradient2()`, `brewer()` for colors\n\n- `continuous()`, `discrete()`, `manual()`, `ordinal()`, `area()`, `date()` for sizes\n\n- `continuous()`, `discrete()`, `manual()`, `ordinal()` for shapes\n\n- `continuous()`, `discrete()`, `manual()`, `ordinal()`, `date()` for transparency\n\n------------------------------------------------------------------------\n\n![Illustration by [Allison Horst](https://github.com/allisonhorst/stats-illustrations)](../../img/concepts/continuous_discrete.png){fig-size=\"120%\" fig-align=\"center\" fig-alt=\"Allison Horsts illustration ofthe correct use of continuous versus discrete; however, in {ggplot2} these are interpeted in a different way: as quantitative and qualitative.\"}\n\n## Continuous vs. Discrete in {ggplot2}\n\n::: {layout-ncol=\"2\"}\n## Continuous:<br>quantitative or numerical data\n\n- height\n- weight\n- age\n- counts\n\n## Discrete:<br>qualitative or categorical data\n\n- species\n- sex\n- study sites\n- age group\n:::\n\n## Continuous vs. Discrete in {ggplot2}\n\n::: {layout-ncol=\"2\"}\n## Continuous:<br>quantitative or numerical data\n\n- height (continuous)\n- weight (continuous)\n- age (continuous or discrete)\n- counts (discrete)\n\n## Discrete:<br>qualitative or categorical data\n\n- species (nominal)\n- sex (nominal)\n- study site (nominal or ordinal)\n- age group (ordinal)\n:::\n\n## Aesthetics + Scales\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"3,4\"}\nggplot(\n bikes,\n aes(x = date, y = count,\n color = season)\n ) +\n geom_point()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-default-invisible-1.svg){width=480}\n:::\n:::\n\n\n## Aesthetics + Scales\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"3,4,7,8,9|7,8,9\"}\nggplot(\n bikes,\n aes(x = date, y = count,\n color = season)\n ) +\n geom_point() +\n scale_x_date() +\n scale_y_continuous() +\n scale_color_discrete()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-default-1.svg){width=480}\n:::\n:::\n\n\n## Scales\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"7\"}\nggplot(\n bikes,\n aes(x = date, y = count,\n color = season)\n ) +\n geom_point() +\n scale_x_continuous() +\n scale_y_continuous() +\n scale_color_discrete()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-overwrite-1-1.svg){width=480}\n:::\n:::\n\n\n## Scales\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"8\"}\nggplot(\n bikes,\n aes(x = date, y = count,\n color = season)\n ) +\n geom_point() +\n scale_x_continuous() +\n scale_y_log10() +\n scale_color_discrete()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-overwrite-2-1.svg){width=480}\n:::\n:::\n\n\n## Scales\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"9\"}\nggplot(\n bikes,\n aes(x = date, y = count,\n color = season)\n ) +\n geom_point() +\n scale_x_continuous() +\n scale_y_log10() +\n scale_color_viridis_d()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-overwrite-3-1.svg){width=480}\n:::\n:::\n\n\n## \\`scale_x\\|y_continuous\\`\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"8,9,10|9\"}\nggplot(\n bikes,\n aes(x = date, y = count,\n color = season)\n ) + \n geom_point() +\n scale_y_continuous(\n trans = \"log10\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-xy-continuous-trans-1.svg){width=480}\n:::\n:::\n\n\n## \\`scale_x\\|y_continuous\\`\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"7,8,9|8\"}\nggplot(\n bikes,\n aes(x = date, y = count,\n color = season)\n ) +\n geom_point() +\n scale_y_continuous(\n name = \"Reported bike shares\"\n ) \n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-xy-continuous-name-1.svg){width=480}\n:::\n:::\n\n\n## \\`scale_x\\|y_continuous\\`\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"9\"}\nggplot(\n bikes,\n aes(x = date, y = count,\n color = season)\n ) +\n geom_point() +\n scale_y_continuous(\n name = \"Reported bike shares\",\n breaks = seq(0, 60000, by = 15000)\n ) \n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-xy-continuous-breaks-seq-1.svg){width=480}\n:::\n:::\n\n\n## \\`scale_x\\|y_continuous\\`\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"9\"}\nggplot(\n bikes,\n aes(x = date, y = count,\n color = season)\n ) +\n geom_point() +\n scale_y_continuous(\n name = \"Reported bike shares\",\n breaks = 0:4*15000\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-xy-continuous-breaks-short-1.svg){width=480}\n:::\n:::\n\n\n## \\`scale_x\\|y_continuous\\`\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"9\"}\nggplot(\n bikes,\n aes(x = date, y = count,\n color = season)\n ) +\n geom_point() +\n scale_y_continuous(\n name = \"Reported bike shares\",\n breaks = c(0, 2:12*2500, 40000, 50000)\n ) \n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-xy-continuous-breaks-irregular-1.svg){width=480}\n:::\n:::\n\n\n## \\`scale_x\\|y_continuous\\`\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"8,10\"}\nggplot(\n bikes,\n aes(x = date, y = count,\n color = season)\n ) +\n geom_point() +\n scale_y_continuous(\n name = \"Reported bike shares in thousands\",\n breaks = 0:4*15000,\n labels = 0:4*15\n ) \n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-xy-continuous-labels-1.svg){width=480}\n:::\n:::\n\n\n## \\`scale_x\\|y_continuous\\`\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"10\"}\nggplot(\n bikes,\n aes(x = date, y = count,\n color = season)\n ) +\n geom_point() +\n scale_y_continuous(\n name = \"Reported bike shares in thousands\",\n breaks = 0:4*15000,\n labels = paste(0:4*15000, \"bikes\")\n ) \n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-xy-continuous-labels-paste-1.svg){width=480}\n:::\n:::\n\n\n## \\`scale_x\\|y_continuous\\`\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"10\"}\nggplot(\n bikes,\n aes(x = date, y = count,\n color = season)\n ) +\n geom_point() +\n scale_y_continuous(\n name = \"Reported bike shares\",\n breaks = 0:4*15000,\n limits = c(NA, 60000)\n ) \n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-xy-continuous-limits-1.svg){width=480}\n:::\n:::\n\n\n## \\`scale_x\\|y_continuous\\`\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"10\"}\nggplot(\n bikes,\n aes(x = date, y = count,\n color = season)\n ) +\n geom_point() +\n scale_y_continuous(\n name = \"Reported bike shares\",\n breaks = 0:4*15000,\n expand = c(0, 0)\n ) \n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-xy-continuous-expand.no-1.svg){width=480}\n:::\n:::\n\n\n## \\`scale_x\\|y_continuous\\`\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"10\"}\nggplot(\n bikes,\n aes(x = date, y = count,\n color = season)\n ) +\n geom_point() +\n scale_y_continuous(\n name = \"Reported bike shares\",\n breaks = -1:5*15000,\n expand = c(.5, .5) ## c(add, mult)\n ) \n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-xy-continuous-expand-1.svg){width=480}\n:::\n:::\n\n\n## \\`scale_x\\|y_continuous\\`\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"10\"}\nggplot(\n bikes,\n aes(x = date, y = count,\n color = season)\n ) +\n geom_point() +\n scale_y_continuous(\n name = \"Reported bike shares\",\n breaks = -1:5*15000,\n expand = expansion(add = 2000)\n ) \n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-xy-continuous-expand-add-explicit-1.svg){width=480}\n:::\n:::\n\n\n## \\`scale_x\\|y_continuous\\`\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"10\"}\nggplot(\n bikes,\n aes(x = date, y = count,\n color = season)\n ) +\n geom_point() +\n scale_y_continuous(\n name = \"Reported bike shares\",\n breaks = 0:4*15000,\n guide = \"none\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-xy-continuous-guide-none-1.svg){width=480}\n:::\n:::\n\n\n## \\`scale_x\\|y_date\\`\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"7,10|7,8,9,10|9\"}\nggplot(\n bikes,\n aes(x = date, y = count,\n color = season)\n ) +\n geom_point() +\n scale_x_date(\n name = NULL,\n date_breaks = \"4 months\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-xy-date-breaks-months-1.svg){width=480}\n:::\n:::\n\n\n## \\`scale_x\\|y_date\\`\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"9\"}\nggplot(\n bikes,\n aes(x = date, y = count,\n color = season)\n ) +\n geom_point() +\n scale_x_date(\n name = NULL,\n date_breaks = \"20 weeks\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-xy-date-breaks-weeks-1.svg){width=480}\n:::\n:::\n\n\n## \\`scale_x\\|y_date\\` with \\`strftime()\\`\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"9,10\"}\nggplot(\n bikes,\n aes(x = date, y = count,\n color = season)\n ) +\n geom_point() +\n scale_x_date(\n name = NULL,\n date_breaks = \"6 months\",\n date_labels = \"%Y/%m/%d\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-xy-date-labels-1.svg){width=480}\n:::\n:::\n\n\n## \\`scale_x\\|y_date\\` with \\`strftime()\\`\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"10\"}\nggplot(\n bikes,\n aes(x = date, y = count,\n color = season)\n ) +\n geom_point() +\n scale_x_date(\n name = NULL,\n date_breaks = \"6 months\",\n date_labels = \"%b '%y\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-xy-date-labels-special-1.svg){width=480}\n:::\n:::\n\n\n## \\`scale_x\\|y_discrete\\`\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"3,6,9|6,7,8,9|7,8\"}\nggplot(\n bikes,\n aes(x = season, y = count)\n ) +\n geom_boxplot() +\n scale_x_discrete(\n name = \"Period\",\n labels = c(\"Dec-Feb\", \"Mar-May\", \"Jun-Aug\", \"Sep-Nov\")\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-xy-discrete-1.svg){width=480}\n:::\n:::\n\n\n## \\`scale_x\\|y_discrete\\`\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"8\"}\nggplot(\n bikes,\n aes(x = season, y = count)\n ) +\n geom_boxplot() +\n scale_x_discrete(\n name = \"Season\",\n expand = c(.5, 0) ## add, mult\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-xy-discrete-expand-1.svg){width=480}\n:::\n:::\n\n\n## Discrete or Continuous?\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"3,5,6,7\"}\nggplot(\n bikes,\n aes(x = as.numeric(season), y = count)\n ) +\n geom_boxplot(\n aes(group = season)\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-xy-fake-discrete-visible-1.svg){width=480}\n:::\n:::\n\n\n## Discrete or Continuous?\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"9,10,11,12,13|11|12\"}\nggplot(\n bikes,\n aes(x = as.numeric(season),\n y = count)\n ) +\n geom_boxplot(\n aes(group = season)\n ) +\n scale_x_continuous(\n name = \"Season\",\n breaks = 1:4,\n labels = levels(bikes$season)\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-xy-fake-discrete-1.svg){width=480}\n:::\n:::\n\n\n## Discrete or Continuous?\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"3,4\"}\nggplot(\n bikes,\n aes(x = as.numeric(season) + \n as.numeric(season) / 8,\n y = count)\n ) +\n geom_boxplot(\n aes(group = season)\n ) +\n scale_x_continuous(\n name = \"Season\",\n breaks = 1:4,\n labels = levels(bikes$season)\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-xy-fake-discrete-shift-1.svg){width=480}\n:::\n:::\n\n\n## \\`scale_color\\|fill_discrete\\`\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"7,10|7,8,9,10|8,9\"}\nggplot(\n bikes,\n aes(x = date, y = count,\n color = season)\n ) +\n geom_point() +\n scale_color_discrete(\n name = \"Season:\",\n type = c(\"#69b0d4\", \"#00CB79\", \"#F7B01B\", \"#a78f5f\")\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-color-discrete-type-vector-1.svg){width=480}\n:::\n:::\n\n\n## Inspect Assigned Colors\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"1|12|14\"}\ng <- ggplot(\n bikes,\n aes(x = date, y = count,\n color = season)\n ) +\n geom_point() +\n scale_color_discrete(\n name = \"Season:\",\n type = c(\"#3ca7d9\", \"#1ec99b\", \"#F7B01B\", \"#bb7e8f\")\n )\n\ngb <- ggplot_build(g)\n\ngb$data[[1]][c(1:5, 200:205, 400:405), 1:5]\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n colour x y PANEL group\n1 #3ca7d9 16439 6830 1 1\n2 #3ca7d9 16439 2404 1 1\n3 #3ca7d9 16440 14763 1 1\n4 #3ca7d9 16440 5609 1 1\n5 #3ca7d9 16441 14501 1 1\n200 #1ec99b 16538 8830 1 2\n201 #1ec99b 16539 24019 1 2\n202 #1ec99b 16539 10500 1 2\n203 #1ec99b 16540 25640 1 2\n204 #1ec99b 16540 11830 1 2\n205 #1ec99b 16541 22216 1 2\n400 #F7B01B 16638 12079 1 3\n401 #F7B01B 16639 26646 1 3\n402 #F7B01B 16639 12446 1 3\n403 #F7B01B 16640 11312 1 3\n404 #F7B01B 16640 4722 1 3\n405 #F7B01B 16641 22748 1 3\n```\n\n\n:::\n:::\n\n\n## \\`scale_color\\|fill_discrete\\`\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"1,2,3,4,5,6|1,16\"}\nmy_colors <- c(\n `winter` = \"#3c89d9\",\n `spring` = \"#1ec99b\",\n `summer` = \"#F7B01B\",\n `autumn` = \"#a26e7c\"\n)\n\nggplot(\n bikes,\n aes(x = date, y = count,\n color = season)\n ) +\n geom_point() +\n scale_color_discrete(\n name = \"Season:\",\n type = my_colors\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-color-discrete-type-vector-named-1.svg){width=480}\n:::\n:::\n\n\n## \\`scale_color\\|fill_discrete\\`\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"2,5|1,16\"}\nmy_colors_alphabetical <- c(\n `autumn` = \"#a26e7c\",\n `spring` = \"#1ec99b\",\n `summer` = \"#F7B01B\",\n `winter` = \"#3c89d9\"\n)\n\nggplot(\n bikes,\n aes(x = date, y = count,\n color = season)\n ) +\n geom_point() +\n scale_color_discrete(\n name = \"Season:\",\n type = my_colors_alphabetical\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-color-discrete-type-vector-named-shuffled-1.svg){width=480}\n:::\n:::\n\n\n## \\`scale_color\\|fill_discrete\\`\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"1|11,12,13\"}\nlibrary(RColorBrewer)\n\nggplot(\n bikes,\n aes(x = date, y = count,\n color = season)\n ) +\n geom_point() +\n scale_color_discrete(\n name = \"Season:\",\n type = brewer.pal(\n n = 4, name = \"Dark2\"\n )\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-color-discrete-type-palette-1.svg){width=480}\n:::\n:::\n\n\n## \\`scale_color\\|fill_manual\\`\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"4,9,10\"}\nggplot(\n bikes,\n aes(x = date, y = count,\n color = weather_type)\n ) +\n geom_point() +\n scale_color_manual(\n name = \"Season:\",\n values = brewer.pal(n = 6, name = \"Pastel1\"),\n na.value = \"black\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-color-manual-na-1.svg){width=480}\n:::\n:::\n\n\n## \\`scale_color\\|fill_carto_d\\`\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"7,8,9,10\"}\nggplot(\n bikes,\n aes(x = date, y = count,\n color = weather_type)\n ) +\n geom_point() +\n rcartocolor::scale_color_carto_d(\n name = \"Season:\",\n palette = \"Pastel\",\n na.value = \"black\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scales-color-discrete-carto-1.svg){width=480}\n:::\n:::\n\n\n## Diamonds Facet\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"1|10|20\"}\nfacet <-\n ggplot(\n diamonds,\n aes(x = carat, y = price)\n ) +\n geom_point(\n alpha = .3\n ) +\n geom_smooth(\n aes(color = cut),\n method = \"lm\",\n se = FALSE\n ) +\n facet_grid(\n cut ~ clarity,\n space = \"free_x\",\n scales = \"free_x\"\n )\n\nfacet\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/diamonds-facet-store-1.svg){width=480}\n:::\n:::\n\n\n## Diamonds Facet\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code}\nfacet +\n scale_x_continuous(\n breaks = 0:5\n ) +\n scale_y_continuous(\n limits = c(0, 30000),\n breaks = 0:3*10000,\n labels = c(\"$0\", \"$10,000\", \"$20,000\", \"$30,000\")\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/diamonds-facet-scales-xy-1.svg){width=480}\n:::\n:::\n\n\n## Diamonds Facet\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"8,9,10,11,12,13,14\"}\nfacet +\n scale_x_continuous(\n breaks = 0:5\n ) +\n scale_y_continuous(\n limits = c(0, 30000),\n breaks = 0:3*10000,\n labels = paste0(\n \"$\", format(\n 0:3*10000, \n big.mark = \",\", \n trim = TRUE\n )\n )\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/diamonds-facet-scales-y-paste-format-1.svg){width=480}\n:::\n:::\n\n\n## Diamonds Facet\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"8,9,10,11,12,13\"}\nfacet +\n scale_x_continuous(\n breaks = 0:5\n ) +\n scale_y_continuous(\n limits = c(0, 30000),\n breaks = 0:3*10000,\n labels = function(y) paste0(\n \"$\", format(\n y, big.mark = \",\",\n trim = TRUE\n )\n )\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/diamonds-facet-scales-y-function-1.svg){width=480}\n:::\n:::\n\n\n## Diamonds Facet\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"8\"}\nfacet +\n scale_x_continuous(\n breaks = 0:5\n ) +\n scale_y_continuous(\n limits = c(0, 30000),\n breaks = 0:3*10000,\n labels = scales::dollar_format()\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/diamonds-facet-scales-y-dollar-format-1.svg){width=480}\n:::\n:::\n\n\n## Diamonds Facet\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"10,11,12,13\"}\nfacet +\n scale_x_continuous(\n breaks = 0:5\n ) +\n scale_y_continuous(\n limits = c(0, 30000),\n breaks = 0:3*10000,\n labels = scales::dollar_format()\n ) +\n scale_color_brewer(\n palette = \"Set2\",\n guide = \"none\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/diamonds-facet-scales-color-1.svg){width=480}\n:::\n:::\n\n\n## Diamonds Facet\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"13,14,15\"}\nfacet +\n scale_x_continuous(\n breaks = 0:5\n ) +\n scale_y_continuous(\n limits = c(0, 30000),\n breaks = 0:3*10000,\n labels = scales::dollar_format()\n ) +\n scale_color_brewer(\n palette = \"Set2\"\n ) +\n theme(\n legend.position = \"none\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/diamonds-facet-scales-no-legend-1.svg){width=480}\n:::\n:::\n\n\n# Coordinate Systems投影\n\n## Coordinate Systems\n\n<br>\n\n<b class='simple-highlight-grn' style='font-size:2.6rem;'>= interpret the position aesthetics</b><br><br>\n\n::: incremental\n- **linear coordinate systems:** preserve the geometrical shapes\n - `coord_cartesian()`\n - `coord_fixed()`\n - `coord_flip()`\n- **non-linear coordinate systems:** likely change the geometrical shapes\n - `coord_polar()`\n - `coord_map()` and `coord_sf()`\n - `coord_trans()`\n:::\n\n## Cartesian Coordinate System\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"6\"}\nggplot(\n bikes,\n aes(x = season, y = count)\n ) +\n geom_boxplot() +\n coord_cartesian()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/coord-cartesian-1.svg){width=480}\n:::\n:::\n\n\n## Cartesian Coordinate System\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"6,7,8\"}\nggplot(\n bikes,\n aes(x = season, y = count)\n ) +\n geom_boxplot() +\n coord_cartesian(\n ylim = c(NA, 15000)\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/coord-cartesian-zoom-1.svg){width=480}\n:::\n:::\n\n\n## Changing Limits\n\n::: {layout-ncol=\"2\"}\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"6,7,8\"}\nggplot(\n bikes,\n aes(x = season, y = count)\n ) +\n geom_boxplot() +\n coord_cartesian(\n ylim = c(NA, 15000)\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/coord-cartesian-ylim-1.svg){width=480}\n:::\n:::\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"6,7,8\"}\nggplot(\n bikes,\n aes(x = season, y = count)\n ) +\n geom_boxplot() +\n scale_y_continuous(\n limits = c(NA, 15000)\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/scale-y-limits-1.svg){width=480}\n:::\n:::\n\n:::\n\n## Clipping\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"8\"}\nggplot(\n bikes,\n aes(x = season, y = count)\n ) +\n geom_boxplot() +\n coord_cartesian(\n ylim = c(NA, 15000),\n clip = \"off\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/coord-clip-1.svg){width=480}\n:::\n:::\n\n\n## Clipping\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"2,3|6,7,8,9,10|12\"}\nggplot(\n filter(bikes, is_holiday == TRUE),\n aes(x = temp_feel, y = count)\n ) +\n geom_point() +\n geom_text(\n aes(label = season),\n nudge_x = .3,\n hjust = 0\n ) +\n coord_cartesian(\n clip = \"off\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/coord-clip-text-1.svg){width=480}\n:::\n:::\n\n\n## ... or better use {ggrepel}\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"6\"}\nggplot(\n filter(bikes, is_holiday == TRUE),\n aes(x = temp_feel, y = count)\n ) +\n geom_point() +\n ggrepel::geom_text_repel(\n aes(label = season),\n nudge_x = .3,\n hjust = 0\n ) +\n coord_cartesian(\n clip = \"off\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/coord-clip-text-repel-1.svg){width=480}\n:::\n:::\n\n\n## Remove All Padding\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"7|8\"}\nggplot(\n bikes,\n aes(x = temp_feel, y = count)\n ) +\n geom_point() +\n coord_cartesian(\n expand = FALSE,\n clip = \"off\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/coord-expand-off-clip-1.svg){width=480}\n:::\n:::\n\n\n## Fixed Coordinate System\n\n::: {layout-ncol=\"2\"}\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"6\"}\nggplot(\n bikes,\n aes(x = temp_feel, y = temp)\n ) +\n geom_point() +\n coord_fixed()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/coord-fixed-1.svg){width=480}\n:::\n:::\n\n\n::: fragment\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"6\"}\nggplot(\n bikes,\n aes(x = temp_feel, y = temp)\n ) +\n geom_point() +\n coord_fixed(ratio = 4)\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/coord-fixed-custom-1.svg){width=480}\n:::\n:::\n\n:::\n:::\n\n## Flipped Coordinate System\n\n::: {layout-ncol=\"2\"}\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"6\"}\nggplot(\n bikes,\n aes(x = weather_type)\n ) +\n geom_bar() +\n coord_cartesian()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/coord-cartesian-comp-flip-1.svg){width=480}\n:::\n:::\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"6\"}\nggplot(\n bikes,\n aes(x = weather_type)\n ) +\n geom_bar() +\n coord_flip()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/coord-flip-1.svg){width=480}\n:::\n:::\n\n:::\n\n## Flipped Coordinate System\n\n::: {layout-ncol=\"2\"}\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"3,6\"}\nggplot(\n bikes,\n aes(y = weather_type)\n ) +\n geom_bar() +\n coord_cartesian()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/coord-cartesian-switch-x-y-1.svg){width=480}\n:::\n:::\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"3,6\"}\nggplot(\n bikes,\n aes(x = weather_type)\n ) +\n geom_bar() +\n coord_flip()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/coord-flip-again-1.svg){width=480}\n:::\n:::\n\n:::\n\n## Reminder: Sort Your Bars!\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"3|2\"}\nggplot(\n filter(bikes, !is.na(weather_type)),\n aes(y = fct_infreq(weather_type))\n ) +\n geom_bar()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/forcats-sort-infreq-1.svg){width=480}\n:::\n:::\n\n\n## Reminder: Sort Your Bars!\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"3,4,5\"}\nggplot(\n filter(bikes, !is.na(weather_type)),\n aes(y = fct_rev(\n fct_infreq(weather_type)\n ))\n ) +\n geom_bar()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/forcats-sort-infreq-rev-1.svg){width=480}\n:::\n:::\n\n\n## Circular Corrdinate System\n\n::: {layout-ncol=\"2\"}\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"7\"}\nggplot(\n filter(bikes, !is.na(weather_type)),\n aes(x = weather_type,\n fill = weather_type)\n ) +\n geom_bar() +\n coord_polar()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/coord-polar-1.svg){width=480}\n:::\n:::\n\n\n::: fragment\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"7\"}\nggplot(\n filter(bikes, !is.na(weather_type)),\n aes(x = weather_type,\n fill = weather_type)\n ) +\n geom_bar() +\n coord_cartesian()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/coord-cartesian-comp-polar-1.svg){width=480}\n:::\n:::\n\n:::\n:::\n\n## Circular Cordinate System\n\n::: {layout-ncol=\"2\"}\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"6,7\"}\nggplot(\n filter(bikes, !is.na(weather_type)),\n aes(x = fct_infreq(weather_type),\n fill = weather_type)\n ) +\n geom_bar(width = 1) +\n coord_polar()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/coord-polar-coxcomb-1.svg){width=480}\n:::\n:::\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"6,7\"}\nggplot(\n filter(bikes, !is.na(weather_type)),\n aes(x = fct_infreq(weather_type),\n fill = weather_type)\n ) +\n geom_bar(width = 1) +\n coord_cartesian()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/coord-cartesian-comp-polar-no-padding-1.svg){width=480}\n:::\n:::\n\n:::\n\n## Circular Corrdinate System\n\n::: {layout-ncol=\"2\"}\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"7\"}\nggplot(\n filter(bikes, !is.na(weather_type)),\n aes(x = fct_infreq(weather_type),\n fill = weather_type)\n ) +\n geom_bar() +\n coord_polar(theta = \"x\")\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/coord-polar-theta-x-1.svg){width=480}\n:::\n:::\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"7\"}\nggplot(\n filter(bikes, !is.na(weather_type)),\n aes(x = fct_infreq(weather_type),\n fill = weather_type)\n ) +\n geom_bar() +\n coord_polar(theta = \"y\")\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/coord-polar-theta-y-1.svg){width=480}\n:::\n:::\n\n:::\n\n## Circular Corrdinate System\n\n::: {layout-ncol=\"2\"}\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"5\"}\nggplot(\n filter(bikes, !is.na(weather_type)),\n aes(x = 1, fill = weather_type)\n ) +\n geom_bar(position = \"stack\") +\n coord_polar(theta = \"y\") \n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/coord-polar-pie-1.svg){width=480}\n:::\n:::\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"5\"}\nggplot(\n filter(bikes, !is.na(weather_type)),\n aes(x = 1, fill = weather_type)\n ) +\n geom_bar(position = \"stack\") +\n coord_cartesian() \n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/coord-cartesian-comp-polar-stacked-1.svg){width=480}\n:::\n:::\n\n:::\n\n## Circular Corrdinate System\n\n::: {layout-ncol=\"2\"}\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"4,6,7\"}\nggplot(\n filter(bikes, !is.na(weather_type)),\n aes(x = 1,\n fill = fct_rev(fct_infreq(weather_type)))\n ) +\n geom_bar(position = \"stack\") +\n coord_polar(theta = \"y\") +\n scale_fill_discrete(name = NULL)\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/coord-polar-pie-sorted-1.svg){width=480}\n:::\n:::\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"4,6,7\"}\nggplot(\n filter(bikes, !is.na(weather_type)),\n aes(x = 1,\n fill = fct_rev(fct_infreq(weather_type)))\n ) +\n geom_bar(position = \"stack\") +\n coord_cartesian() +\n scale_fill_discrete(name = NULL)\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/coord-cartesian-comp-polar-stacked-sorted-1.svg){width=480}\n:::\n:::\n\n:::\n\n## Transform a Coordinate System\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"6\"}\nggplot(\n bikes,\n aes(x = temp, y = count)\n ) +\n geom_point() +\n coord_trans(y = \"log10\")\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/coord-trans-log-1.svg){width=480}\n:::\n:::\n\n\n## Transform a Coordinate System\n\n::: {layout-ncol=\"2\"}\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"6\"}\nggplot(\n bikes,\n aes(x = temp, y = count,\n group = day_night)\n ) +\n geom_point() +\n geom_smooth(method = \"lm\") +\n coord_trans(y = \"log10\")\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/trans-log-via-coord-1.svg){width=480}\n:::\n:::\n\n\n::: fragment\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"6\"}\nggplot(\n bikes,\n aes(x = temp, y = count,\n group = day_night)\n ) +\n geom_point() +\n geom_smooth(method = \"lm\") +\n scale_y_log10()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/trans-log-via-scale-1.svg){width=480}\n:::\n:::\n\n:::\n:::\n\n\n# 图形组合\n\n------------------------------------------------------------------------\n\n![Illustration by [Allison Horst](https://github.com/allisonhorst/stats-illustrations)](../../img/layout/ah_patchwork.jpg){fig-align=\"center\" fig-alt=\"Allison Horsts monster illustration of the patchwork extension package.\"}\n\n::: footer\n:::\n\n------------------------------------------------------------------------\n\n::: panel-tabset\n### Graphic\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/patchwork-p1-1.svg){width=960}\n:::\n:::\n\n\n### Code\n\n\n::: {.cell}\n\n```{.r .cell-code}\ntheme_std <- theme_set(theme_minimal(base_size = 18))\ntheme_update(\n # text = element_text(family = \"Pally\"),\n panel.grid = element_blank(),\n axis.text = element_text(color = \"grey50\", size = 12),\n axis.title = element_text(color = \"grey40\", face = \"bold\"),\n axis.title.x = element_text(margin = margin(t = 12)),\n axis.title.y = element_text(margin = margin(r = 12)),\n axis.line = element_line(color = \"grey80\", size = .4),\n legend.text = element_text(color = \"grey50\", size = 12),\n plot.tag = element_text(size = 40, margin = margin(b = 15)),\n plot.background = element_rect(fill = \"white\", color = \"white\")\n)\n\nbikes_sorted <-\n bikes %>%\n filter(!is.na(weather_type)) %>%\n group_by(weather_type) %>%\n mutate(sum = sum(count)) %>%\n ungroup() %>%\n mutate(\n weather_type = forcats::fct_reorder(\n str_to_title(str_wrap(weather_type, 5)), sum\n )\n )\n\np1 <- ggplot(\n bikes_sorted,\n aes(x = weather_type, y = count, color = weather_type)\n ) +\n geom_hline(yintercept = 0, color = \"grey80\", size = .4) +\n stat_summary(\n geom = \"point\", fun = \"sum\", size = 12\n ) +\n stat_summary(\n geom = \"linerange\", ymin = 0, fun.max = function(y) sum(y),\n size = 2, show.legend = FALSE\n ) +\n coord_flip(ylim = c(0, NA), clip = \"off\") +\n scale_y_continuous(\n expand = c(0, 0), limits = c(0, 8500000),\n labels = scales::comma_format(scale = .0001, suffix = \"K\")\n ) +\n scale_color_viridis_d(\n option = \"magma\", direction = -1, begin = .1, end = .9, name = NULL,\n guide = guide_legend(override.aes = list(size = 7))\n ) +\n labs(\n x = NULL, y = \"Sum of reported bike shares\", tag = \"P1\",\n ) +\n theme(\n axis.line.y = element_blank(),\n axis.text.y = element_text(family = \"Pally\", color = \"grey50\", face = \"bold\",\n margin = margin(r = 15), lineheight = .9)\n )\n\np1\n```\n:::\n\n:::\n\n------------------------------------------------------------------------\n\n::: panel-tabset\n### Graphic\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/patchwork-p2-1.svg){width=960}\n:::\n:::\n\n\n### Code\n\n\n::: {.cell}\n\n```{.r .cell-code}\np2 <- bikes_sorted %>%\n filter(season == \"winter\", is_weekend == TRUE, day_night == \"night\") %>%\n group_by(weather_type, .drop = FALSE) %>%\n mutate(id = row_number()) %>%\n ggplot(\n aes(x = weather_type, y = id, color = weather_type)\n ) +\n geom_point(size = 4.5) +\n scale_color_viridis_d(\n option = \"magma\", direction = -1, begin = .1, end = .9, name = NULL,\n guide = guide_legend(override.aes = list(size = 7))\n ) +\n labs(\n x = NULL, y = \"Reported bike shares on\\nweekend winter nights\", tag = \"P2\",\n ) +\n coord_cartesian(ylim = c(.5, NA), clip = \"off\")\n\np2\n```\n:::\n\n:::\n\n------------------------------------------------------------------------\n\n::: panel-tabset\n### Graphic\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/patchwork-p3-1.svg){width=960}\n:::\n:::\n\n\n### Code\n\n\n::: {.cell}\n\n```{.r .cell-code}\nmy_colors <- c(\"#cc0000\", \"#000080\")\n\np3 <- bikes %>%\n group_by(week = lubridate::week(date), day_night, year) %>%\n summarize(count = sum(count)) %>%\n group_by(week, day_night) %>%\n mutate(avg = mean(count)) %>%\n ggplot(aes(x = week, y = count, group = interaction(day_night, year))) +\n geom_line(color = \"grey65\", size = 1) +\n geom_line(aes(y = avg, color = day_night), stat = \"unique\", size = 1.7) +\n annotate(\n geom = \"text\", label = c(\"Day\", \"Night\"), color = my_colors,\n x = c(5, 18), y = c(125000, 29000), size = 8, fontface = \"bold\", family = \"Pally\"\n ) +\n scale_x_continuous(breaks = c(1, 1:10*5)) +\n scale_y_continuous(labels = scales::comma_format()) +\n scale_color_manual(values = my_colors, guide = \"none\") +\n labs(\n x = \"Week of the Year\", y = \"Reported bike shares\\n(cumulative # per week)\", tag = \"P3\",\n )\n\np3\n```\n:::\n\n:::\n\n## {patchwork}\n\n\n::: {.cell layout-align=\"center\"}\n\n```{.r .cell-code code-line-numbers=\"3|2,3\"}\n# install.packages(\"patchwork\")\nrequire(patchwork)\n(p1 + p2) / p3\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/patchwork-composition-1.svg){fig-align='center' width=1440}\n:::\n:::\n\n\n## \"Collect Guides\"\n\n\n::: {.cell layout-align=\"center\"}\n\n```{.r .cell-code}\n(p1 + p2) / p3 + plot_layout(guides = \"collect\")\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/patchwork-composition-guides-1.svg){fig-align='center' width=1440}\n:::\n:::\n\n\n## Apply Theming\n\n\n::: {.cell layout-align=\"center\"}\n\n```{.r .cell-code}\n((p1 + p2) / p3 & theme(legend.justification = \"top\")) +\nplot_layout(guides = \"collect\")\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/patchwork-composition-guides-just-1.svg){fig-align='center' width=1440}\n:::\n:::\n\n\n## Apply Theming\n\n\n::: {.cell layout-align=\"center\"}\n\n```{.r .cell-code}\n(p1 + p2) / p3 & theme(legend.position = \"none\",\n plot.background = element_rect(color = \"black\", size = 3))\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/patchwork-composition-legend-off-1.svg){fig-align='center' width=1440}\n:::\n:::\n\n\n## Adjust Widths and Heights\n\n\n::: {.cell layout-align=\"center\"}\n\n```{.r .cell-code code-line-numbers=\"2\"}\n((p1 + p2) / p3 & theme(legend.position = \"none\")) +\n plot_layout(heights = c(.2, .1), widths = c(2, 1))\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/patchwork-composition-heights-widths-1.svg){fig-align='center' width=1440}\n:::\n:::\n\n\n## Use A Custom Layout\n\n\n::: {.cell layout-align=\"center\"}\n\n```{.r .cell-code code-line-numbers=\"1,2,3,4|5\"}\npicasso <- \"\nAAAAAA#BBBB\nCCCCCCCCC##\nCCCCCCCCC##\"\n(p1 + p2 + p3 & theme(legend.position = \"none\")) +\nplot_layout(design = picasso)\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/patchwork-composition-design-1.svg){fig-align='center' width=1440}\n:::\n:::\n\n\n## Add Labels\n\n\n::: {.cell}\n\n```{.r .cell-code}\npl1 <- p1 + labs(tag = NULL, title = \"Plot One\") + theme(legend.position = \"none\")\npl2 <- p2 + labs(tag = NULL, title = \"Plot Two\") + theme(legend.position = \"none\")\npl3 <- p3 + labs(tag = NULL, title = \"Plot Three\") + theme(legend.position = \"none\")\n```\n:::\n\n\n## Add Labels\n\n\n::: {.cell layout-align=\"center\"}\n\n```{.r .cell-code code-line-numbers=\"2\"}\n(pl1 + pl2) / pl3 +\n plot_annotation(tag_levels = \"1\",\n tag_prefix = \"P\",\n title = \"An overarching title for all 3 plots, placed on the very top while all other titles are sitting below the tags.\")\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/patchwork-composition-labs-1.svg){fig-align='center' width=1440}\n:::\n:::\n\n\n## Add Text\n\n::: panel-tabset\n### Graphic\n\n\n::: {.cell layout-align=\"center\"}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/patchwork-composition-textbox-prep-1.svg){fig-align='center' width=864}\n:::\n:::\n\n\n### Code\n\n\n::: {.cell}\n\n```{.r .cell-code}\ntext <- tibble::tibble(\n x = 0, y = 0, label = \"Lorem ipsum dolor sit amet, **consectetur adipiscing elit**, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation <b style='color:#000080;'>ullamco laboris nisi</b> ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat <b style='color:#cc0000;'>cupidatat non proident</b>, sunt in culpa qui officia deserunt mollit anim id est laborum.\"\n)\npt <- ggplot(text, aes(x = x, y = y)) +\n ggtext::geom_textbox(\n aes(label = label),\n box.color = NA, width = unit(23, \"lines\"),\n color = \"grey40\", size = 6.5, lineheight = 1.4\n ) +\n coord_cartesian(expand = FALSE, clip = \"off\") +\n theme_void()\npt\n```\n:::\n\n:::\n\n## Add Text\n\n\n::: {.cell layout-align=\"center\"}\n\n```{.r .cell-code}\n(p1 + pt) / p3\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/patchwork-composition-textbox-1.svg){fig-align='center' width=1440}\n:::\n:::\n\n\n## Add Inset Plots\n\n\n::: {.cell layout-align=\"center\"}\n\n```{.r .cell-code}\npl1 + inset_element(pl2,\n l = .6, b = .1, r = 1, t = .6)\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/patchwork-composition-inset-1-1.svg){fig-align='center' width=1152}\n:::\n:::\n\n\n## Add Inset Plots\n\n\n::: {.cell layout-align=\"center\"}\n\n```{.r .cell-code}\npl1 + inset_element(pl2,\n l = .6, b = 0, r = 1, t = .5, align_to = 'full')\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/patchwork-composition-inset-2-1.svg){fig-align='center' width=1152}\n:::\n:::\n\n\n## Add Inset Plots\n\n\n::: {.cell layout-align=\"center\"}\n\n```{.r .cell-code}\n(pl1 + inset_element(pl2,\n l = .6, b = .1, r = 1, t = .6) + pt) / pl3\n```\n\n::: {.cell-output-display}\n![](index_files/figure-revealjs/patchwork-composition-inset-3-1.svg){fig-align='center' width=1440}\n:::\n:::\n\n\n\n## 练习\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(palmerpenguins)\nlibrary(ggthemes)\npenguins\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n# A tibble: 344 × 8\n species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g\n <fct> <fct> <dbl> <dbl> <int> <int>\n 1 Adelie Torgersen 39.1 18.7 181 3750\n 2 Adelie Torgersen 39.5 17.4 186 3800\n 3 Adelie Torgersen 40.3 18 195 3250\n 4 Adelie Torgersen NA NA NA NA\n 5 Adelie Torgersen 36.7 19.3 193 3450\n 6 Adelie Torgersen 39.3 20.6 190 3650\n 7 Adelie Torgersen 38.9 17.8 181 3625\n 8 Adelie Torgersen 39.2 19.6 195 4675\n 9 Adelie Torgersen 34.1 18.1 193 3475\n10 Adelie Torgersen 42 20.2 190 4250\n# 334 more rows\n# 2 more variables: sex <fct>, year <int>\n```\n\n\n:::\n:::\n\n\n## 效果\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/unnamed-chunk-179-1.svg){width=768}\n:::\n:::\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n## 练习\n\n:::: {.panel-tabset}\n\n### Code\n\n\n::: {.cell}\n\n```{.r .cell-code}\np <- ggplot(\n data = penguins,\n mapping = aes(x = flipper_length_mm, y = body_mass_g)\n) +\n geom_point(aes(color = species, shape = species)) +\n geom_smooth(method = \"lm\") +\n labs(\n title = \"Body mass and flipper length\",\n subtitle = \"Dimensions for Adelie, Chinstrap, and Gentoo Penguins\",\n x = \"Flipper length (mm)\", y = \"Body mass (g)\",\n color = \"Species\", shape = \"Species\"\n ) +\n scale_color_colorblind()\n```\n:::\n\n\n\n\n\n### Graphic\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/unnamed-chunk-188-1.svg){width=768}\n:::\n:::\n\n\n::::\n\n## 练习\n\n:::: {.panel-tabset}\n\n### Code\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\np <- ggplot(\n data = penguins,\n mapping = aes(x = flipper_length_mm, y = body_mass_g)) +\n geom_point(aes(color = bill_depth_mm)) +\n geom_smooth()\n```\n:::\n\n\n### Graphic\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/unnamed-chunk-190-1.svg){width=768}\n:::\n:::\n\n\n::::\n\n## 练习\n\n:::: {.panel-tabset}\n\n### Code\n\n\n::: {.cell}\n\n```{.r .cell-code}\np <- ggplot(\n data = penguins,\n mapping = aes(x = flipper_length_mm, y = body_mass_g, color = island)\n) +\n geom_point() +\n geom_smooth(se = FALSE)\n```\n:::\n\n\n### Graphic\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/unnamed-chunk-192-1.svg){width=768}\n:::\n:::\n\n\n::::\n\n## 分层展示\n\n:::: {.panel-tabset}\n\n### Code\n\n\n::: {.cell}\n\n```{.r .cell-code}\np <- ggplot(\n data = penguins,\n mapping = aes(x = flipper_length_mm, y = body_mass_g)\n) +\n geom_point() +\n geom_smooth()\n```\n:::\n\n\n### Graphic\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/unnamed-chunk-194-1.svg){width=768}\n:::\n:::\n\n\n::::\n\n## 柱状图\n\n:::: {.panel-tabset}\n\n### Code\n\n\n::: {.cell}\n\n```{.r .cell-code}\np <- ggplot(penguins, aes(x = species)) +\n geom_bar()\n```\n:::\n\n### Graphic\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/unnamed-chunk-196-1.svg){width=768}\n:::\n:::\n\n\n::::\n\n## 柱状图\n\n:::: {.panel-tabset}\n\n### Code\n\n\n::: {.cell}\n\n```{.r .cell-code}\np <- ggplot(penguins, aes(x = fct_infreq(species))) +\n geom_bar()\n```\n:::\n\n\n### Graphic\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/unnamed-chunk-198-1.svg){width=768}\n:::\n:::\n\n\n::::\n\n\n## 直方图\n\n\n:::: {.panel-tabset}\n\n### Code\n\n\n::: {.cell}\n\n```{.r .cell-code}\np <- ggplot(penguins, aes(x = body_mass_g)) +\n geom_histogram(binwidth = 200)\n```\n:::\n\n\n\n### Graphic\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/unnamed-chunk-200-1.svg){width=768}\n:::\n:::\n\n\n::::\n\n## 直方图\n\n:::: {.panel-tabset}\n\n### Code\n\n\n::: {.cell layout-ncol=\"2\"}\n\n```{.r .cell-code}\np1 <- ggplot(penguins, aes(x = body_mass_g)) +\n geom_histogram(binwidth = 20)\np2 <- ggplot(penguins, aes(x = body_mass_g)) +\n geom_histogram(binwidth = 2000)\np <- p1 + p2\n```\n:::\n\n\n\n### Graphic\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/unnamed-chunk-202-1.svg){width=960}\n:::\n:::\n\n\n::::\n\n## 密度图\n\n:::: {.panel-tabset}\n\n### Code\n\n\n::: {.cell}\n\n```{.r .cell-code}\np <- ggplot(penguins, aes(x = body_mass_g)) +\n geom_density()\n```\n:::\n\n\n\n### Graphic\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/unnamed-chunk-204-1.svg){width=768}\n:::\n:::\n\n\n::::\n\n\n## 箱图\n\n\n:::: {.panel-tabset}\n\n### Code\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\np <- ggplot(penguins,\n aes(x = species, y = body_mass_g)) +\n geom_boxplot()\n```\n:::\n\n\n### Graphic\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/unnamed-chunk-206-1.svg){width=768}\n:::\n:::\n\n\n::::\n\n## 分组\n\n:::: {.panel-tabset}\n\n### Code\n\n\n::: {.cell}\n\n```{.r .cell-code}\np <- ggplot(penguins,\n aes(x = body_mass_g, color = species)) +\n geom_density(linewidth = 0.75)\n```\n:::\n\n\n### Graphic\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/unnamed-chunk-208-1.svg){width=768}\n:::\n:::\n\n\n::::\n\n## 分组\n\n:::: {.panel-tabset}\n\n### Code\n\n\n::: {.cell}\n\n```{.r .cell-code}\np <- ggplot(penguins,\n aes(x = body_mass_g, color = species, fill = species)) +\n geom_density(alpha = 0.5)\n```\n:::\n\n\n\n### Graphic\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/unnamed-chunk-210-1.svg){width=768}\n:::\n:::\n\n\n::::\n\n\n## 分组\n\n:::: {.panel-tabset}\n\n### Code\n\n\n::: {.cell}\n\n```{.r .cell-code}\np <- ggplot(penguins,\n aes(x = island, fill = species)) +\n geom_bar()\n```\n:::\n\n\n\n### Graphic\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/unnamed-chunk-212-1.svg){width=768}\n:::\n:::\n\n\n::::\n\n\n## 分组\n\n:::: {.panel-tabset}\n\n### Code\n\n\n::: {.cell}\n\n```{.r .cell-code}\np <- ggplot(penguins,\n aes(x = island, fill = species)) +\n geom_bar(position = \"fill\")\n```\n:::\n\n\n\n### Graphic\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/unnamed-chunk-214-1.svg){width=768}\n:::\n:::\n\n\n::::\n\n## 分组\n\n:::: {.panel-tabset}\n\n### Code\n\n\n::: {.cell}\n\n```{.r .cell-code}\np <- ggplot(penguins,\n aes(x = flipper_length_mm, y = body_mass_g)) +\n geom_point()\n```\n:::\n\n\n### Graphic\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/unnamed-chunk-216-1.svg){width=768}\n:::\n:::\n\n\n::::\n\n## 分组\n\n:::: {.panel-tabset}\n\n### Code\n\n\n::: {.cell}\n\n```{.r .cell-code}\np <- ggplot(penguins,\n aes(x = flipper_length_mm, y = body_mass_g)) +\n geom_point(aes(color = species, shape = island))\n```\n:::\n\n### Graphic\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/unnamed-chunk-218-1.svg){width=768}\n:::\n:::\n\n\n::::\n\n\n## 分面\n\n:::: {.panel-tabset}\n\n### Code\n\n\n::: {.cell}\n\n```{.r .cell-code}\np <- ggplot(penguins,\n aes(x = flipper_length_mm, y = body_mass_g)) +\n geom_point(aes(color = species, shape = species)) +\n facet_wrap(~island)\n```\n:::\n\n\n### Graphic\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/unnamed-chunk-220-1.svg){width=768}\n:::\n:::\n\n\n::::\n\n## 分面\n\n:::: {.panel-tabset}\n\n### Code\n\n\n::: {.cell}\n\n```{.r .cell-code}\np <- ggplot(\n data = penguins,\n mapping = aes(\n x = bill_length_mm, y = bill_depth_mm, \n color = species, shape = species\n )\n) +\n geom_point() +\n labs(color = \"Species\")\n```\n:::\n\n\n### Graphic\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/unnamed-chunk-222-1.svg){width=768}\n:::\n:::\n\n\n::::\n\n## 练习\n\n:::: {.panel-tabset}\n\n### Code\n\n\n::: {.cell layout-ncol=\"2\"}\n\n```{.r .cell-code}\np1 <- ggplot(penguins, aes(x = island, fill = species)) +\n geom_bar(position = \"fill\")\np2 <- ggplot(penguins, aes(x = species, fill = island)) +\n geom_bar(position = \"fill\")\np <- p1 + p2\n```\n:::\n\n\n\n### Graphic\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/unnamed-chunk-224-1.svg){width=960}\n:::\n:::\n\n\n::::\n\n## 练习\n\n:::: {.panel-tabset}\n\n### Code\n\n\n::: {.cell}\n\n```{.r .cell-code}\np <- ggplot(penguins,\n aes(x = flipper_length_mm, y = body_mass_g)) +\n geom_point()\n```\n:::\n\n\n### Graphic\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/unnamed-chunk-226-1.svg){width=768}\n:::\n:::\n\n\n::::\n\n## 练习\n\n\n:::: {.panel-tabset}\n\n### Code\n\n\n::: {.cell}\n\n```{.r .cell-code}\np <- ggplot(mpg, aes(x = class)) +\ngeom_bar()\n```\n:::\n\n\n### Graphic\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/unnamed-chunk-228-1.svg){width=768}\n:::\n:::\n\n\n::::\n\n## 练习\n\n:::: {.panel-tabset}\n\n### Code\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\np <- ggplot(mpg, aes(x = cty, y = hwy)) +\ngeom_point()\n```\n:::\n\n\n### Graphic\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/unnamed-chunk-230-1.svg){width=768}\n:::\n:::\n\n\n::::\n\n## 练习\n\n:::: {.panel-tabset}\n\n### Code\n\n\n::: {.cell}\n\n```{.r .cell-code}\np <- ggplot(data = mpg) +\ngeom_point(mapping = aes(x = displ, y = hwy))\n```\n:::\n\n\n### Graphic\n\n\n::: {.cell}\n::: {.cell-output-display}\n![](index_files/figure-revealjs/unnamed-chunk-232-1.svg){width=768}\n:::\n:::\n\n\n::::\n\n## 欢迎讨论!{.center}\n\n\n[<svg viewBox=\"0 0 576 512\" style=\"height:1em;position:relative;display:inline-block;top:.1em;\" xmlns=\"http://www.w3.org/2000/svg\">\n <path d=\"M280.37 148.26L96 300.11V464a16 16 0 0 0 16 16l112.06-.29a16 16 0 0 0 15.92-16V368a16 16 0 0 1 16-16h64a16 16 0 0 1 16 16v95.64a16 16 0 0 0 16 16.05L464 480a16 16 0 0 0 16-16V300L295.67 148.26a12.19 12.19 0 0 0-15.3 0zM571.6 251.47L488 182.56V44.05a12 12 0 0 0-12-12h-56a12 12 0 0 0-12 12v72.61L318.47 43a48 48 0 0 0-61 0L4.34 251.47a12 12 0 0 0-1.6 16.9l25.5 31A12 12 0 0 0 45.15 301l235.22-193.74a12.19 12.19 0 0 1 15.3 0L530.9 301a12 12 0 0 0 16.9-1.6l25.5-31a12 12 0 0 0-1.7-16.93z\"></path>\n</svg>[苏命|https://drwater.rcees.ac.cn](https://drwater.rcees.ac.cn); <svg viewBox=\"0 0 576 512\" style=\"height:1em;position:relative;display:inline-block;top:.1em;\" xmlns=\"http://www.w3.org/2000/svg\">\n <path d=\"M528 32H48C21.5 32 0 53.5 0 80v352c0 26.5 21.5 48 48 48h480c26.5 0 48-21.5 48-48V80c0-26.5-21.5-48-48-48zm0 400H48V80h480v352zM208 256c35.3 0 64-28.7 64-64s-28.7-64-64-64-64 28.7-64 64 28.7 64 64 64zm-89.6 128h179.2c12.4 0 22.4-8.6 22.4-19.2v-19.2c0-31.8-30.1-57.6-67.2-57.6-10.8 0-18.7 8-44.8 8-26.9 0-33.4-8-44.8-8-37.1 0-67.2 25.8-67.2 57.6v19.2c0 10.6 10 19.2 22.4 19.2zM360 320h112c4.4 0 8-3.6 8-8v-16c0-4.4-3.6-8-8-8H360c-4.4 0-8 3.6-8 8v16c0 4.4 3.6 8 8 8zm0-64h112c4.4 0 8-3.6 8-8v-16c0-4.4-3.6-8-8-8H360c-4.4 0-8 3.6-8 8v16c0 4.4 3.6 8 8 8zm0-64h112c4.4 0 8-3.6 8-8v-16c0-4.4-3.6-8-8-8H360c-4.4 0-8 3.6-8 8v16c0 4.4 3.6 8 8 8z\"></path>\n</svg>[https://drwater.rcees.ac.cn/bcard](https://drwater.rcees.ac.cn/bcard); <svg viewBox=\"0 0 512 512\" style=\"height:1em;position:relative;display:inline-block;top:.1em;\" xmlns=\"http://www.w3.org/2000/svg\">\n <path d=\"M326.612 185.391c59.747 59.809 58.927 155.698.36 214.59-.11.12-.24.25-.36.37l-67.2 67.2c-59.27 59.27-155.699 59.262-214.96 0-59.27-59.26-59.27-155.7 0-214.96l37.106-37.106c9.84-9.84 26.786-3.3 27.294 10.606.648 17.722 3.826 35.527 9.69 52.721 1.986 5.822.567 12.262-3.783 16.612l-13.087 13.087c-28.026 28.026-28.905 73.66-1.155 101.96 28.024 28.579 74.086 28.749 102.325.51l67.2-67.19c28.191-28.191 28.073-73.757 0-101.83-3.701-3.694-7.429-6.564-10.341-8.569a16.037 16.037 0 0 1-6.947-12.606c-.396-10.567 3.348-21.456 11.698-29.806l21.054-21.055c5.521-5.521 14.182-6.199 20.584-1.731a152.482 152.482 0 0 1 20.522 17.197zM467.547 44.449c-59.261-59.262-155.69-59.27-214.96 0l-67.2 67.2c-.12.12-.25.25-.36.37-58.566 58.892-59.387 154.781.36 214.59a152.454 152.454 0 0 0 20.521 17.196c6.402 4.468 15.064 3.789 20.584-1.731l21.054-21.055c8.35-8.35 12.094-19.239 11.698-29.806a16.037 16.037 0 0 0-6.947-12.606c-2.912-2.005-6.64-4.875-10.341-8.569-28.073-28.073-28.191-73.639 0-101.83l67.2-67.19c28.239-28.239 74.3-28.069 102.325.51 27.75 28.3 26.872 73.934-1.155 101.96l-13.087 13.087c-4.35 4.35-5.769 10.79-3.783 16.612 5.864 17.194 9.042 34.999 9.69 52.721.509 13.906 17.454 20.446 27.294 10.606l37.106-37.106c59.271-59.259 59.271-155.699.001-214.959z\"></path>\n</svg>[Slides](https://drwater.rcees.ac.cn/course/public/RWEP/@PUB/SD/)]{.r-fit-text}<br><br><div class=\"columns\"><div class=\"column\" style=\"width:30%;\">[<svg viewBox=\"0 0 512 512\" style=\"height:1em;position:relative;display:inline-block;top:.1em;\" xmlns=\"http://www.w3.org/2000/svg\">\n <path d=\"M464 64H48C21.49 64 0 85.49 0 112v288c0 26.51 21.49 48 48 48h416c26.51 0 48-21.49 48-48V112c0-26.51-21.49-48-48-48zm0 48v40.805c-22.422 18.259-58.168 46.651-134.587 106.49-16.841 13.247-50.201 45.072-73.413 44.701-23.208.375-56.579-31.459-73.413-44.701C106.18 199.465 70.425 171.067 48 152.805V112h416zM48 400V214.398c22.914 18.251 55.409 43.862 104.938 82.646 21.857 17.205 60.134 55.186 103.062 54.955 42.717.231 80.509-37.199 103.053-54.947 49.528-38.783 82.032-64.401 104.947-82.653V400H48z\"></path>\n</svg>[mingsu@rcees.ac.cn](mailto:mingsu@rcees.ac.cn); ]{.r-fit-text}<br></div></div>\n\n",
"supporting": [],
"filters": [
"rmarkdown/pagebreak.lua"
],
"includes": {
"include-in-header": [
"<script src=\"../../site_libs/jquery-3.6.0/jquery-3.6.0.min.js\"></script>\n<link href=\"../../site_libs/font-awesome-6.4.2/css/all.min.css\" rel=\"stylesheet\" />\n<link href=\"../../site_libs/font-awesome-6.4.2/css/v4-shims.min.css\" rel=\"stylesheet\" />\n<script src=\"../../site_libs/bootbox-5.5.2/bootbox.min.js\"></script>\n<script src=\"../../site_libs/idb-keyvalue-3.2.0/idb-keyval-iife-compat.min.js\"></script>\n<link href=\"../../site_libs/tutorial-0.11.5.9000/tutorial.css\" rel=\"stylesheet\" />\n<script src=\"../../site_libs/tutorial-0.11.5.9000/tutorial.js\"></script>\n<script src=\"../../site_libs/kePrint-0.0.1/kePrint.js\"></script>\n<link href=\"../../site_libs/lightable-0.0.1/lightable.css\" rel=\"stylesheet\" />\n"
],
"include-after-body": [
"\n<script>\n // htmlwidgets need to know to resize themselves when slides are shown/hidden.\n // Fire the \"slideenter\" event (handled by htmlwidgets.js) when the current\n // slide changes (different for each slide format).\n (function () {\n // dispatch for htmlwidgets\n function fireSlideEnter() {\n const event = window.document.createEvent(\"Event\");\n event.initEvent(\"slideenter\", true, true);\n window.document.dispatchEvent(event);\n }\n\n function fireSlideChanged(previousSlide, currentSlide) {\n fireSlideEnter();\n\n // dispatch for shiny\n if (window.jQuery) {\n if (previousSlide) {\n window.jQuery(previousSlide).trigger(\"hidden\");\n }\n if (currentSlide) {\n window.jQuery(currentSlide).trigger(\"shown\");\n }\n }\n }\n\n // hookup for slidy\n if (window.w3c_slidy) {\n window.w3c_slidy.add_observer(function (slide_num) {\n // slide_num starts at position 1\n fireSlideChanged(null, w3c_slidy.slides[slide_num - 1]);\n });\n }\n\n })();\n</script>\n\n"
]
},
"engineDependencies": {},
"preserve": {},
"postProcess": true
}
}