{ "hash": "1f512aedde0c78d18d7113364c7ce0db", "result": { "engine": "knitr", "markdown": "---\ntitle: \"数据可视化\"\nsubtitle: 《区域水环境污染数据分析实践》
Data analysis practice of regional water environment pollution\nauthor: 苏命、王为东
中国科学院大学资源与环境学院
中国科学院生态环境研究中心\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
\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
\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
\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
ComponentFunctionExplanation
Dataggplot(data)         *The raw data that you want to visualise.*
Aesthetics          aes()*Aesthetic mappings between variables and visual properties.*
Geometriesgeom_*()*The geometric shapes representing the data.*
\n\n\n\n## The Grammar of {ggplot2}\n\n\n
\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
ComponentFunctionExplanation
Dataggplot(data)         *The raw data that you want to visualise.*
Aesthetics          aes()*Aesthetic mappings between variables and visual properties.*
Geometriesgeom_*()*The geometric shapes representing the data.*
Statisticsstat_*()*The statistical transformations applied to the data.*
Scalesscale_*()*Maps between the data and the aesthetic dimensions.*
Coordinate Systemcoord_*()*Maps data into the plane of the data rectangle.*
Facetsfacet_*()*The arrangement of the data into a grid of plots.*
Visual Themestheme() / theme_*()*The overall visual defaults of a plot.*
\n\n\n\n## The Data\n\nBike sharing counts in London, UK, powered by [TfL Open Data](https://tfl.gov.uk/modes/cycling/santander-cycles)\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
\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\n \n \n \n \n \n \n \n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n
Variable Description Class
date Date encoded as `YYYY-MM-DD` date
day_night `day` (6:00am–5:59pm) or `night` (6:00pm–5:59am) character
year `2015` or `2016` factor
month `1` (January) to `12` (December) factor
season `winter`, `spring`, `summer`, or `autumn` factor
count Sum of reported bikes rented integer
is_workday `TRUE` being Monday to Friday and no bank holiday logical
is_weekend `TRUE` being Saturday or Sunday logical
is_holiday `TRUE` being a bank holiday in the UK logical
temp Average air temperature (°C) double
temp_feel Average feels like temperature (°C) double
humidity Average air humidity (%) double
wind_speed Average wind speed (km/h) double
weather_type Most common weather type character
\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
\n\n= link variables to graphical properties

\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## aesthetics\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
\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
\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
\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
\n\n= interpret aesthetics as graphical representations

\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 \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 , temp , temp_feel ,\n# humidity , wind_speed , weather_type \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
\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
\n\n= split variables to multiple panels

\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
\n\n= translate between variable ranges and property ranges

\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
aesthetic dimensions mapped to the data.\n\n
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
aesthetic dimensions mapped to the data.\n\n
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:
quantitative or numerical data\n\n- height\n- weight\n- age\n- counts\n\n## Discrete:
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:
quantitative or numerical data\n\n- height (continuous)\n- weight (continuous)\n- age (continuous or discrete)\n- counts (discrete)\n\n## Discrete:
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
\n\n= interpret the position aesthetics

\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 ullamco laboris nisi 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 cupidatat non proident, 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 \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 , year \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[\n \n[苏命|https://drwater.rcees.ac.cn](https://drwater.rcees.ac.cn); \n \n[https://drwater.rcees.ac.cn/bcard](https://drwater.rcees.ac.cn/bcard); \n \n[Slides](https://drwater.rcees.ac.cn/course/public/RWEP/@PUB/SD/)]{.r-fit-text}

[\n \n[mingsu@rcees.ac.cn](mailto:mingsu@rcees.ac.cn); ]{.r-fit-text}
\n\n", "supporting": [], "filters": [ "rmarkdown/pagebreak.lua" ], "includes": { "include-in-header": [ "\n\n\n\n\n\n\n\n\n" ], "include-after-body": [ "\n\n\n" ] }, "engineDependencies": {}, "preserve": {}, "postProcess": true } }