-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe9f936
commit 3d5ca18
Showing
68 changed files
with
24,176 additions
and
492 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
_freeze/slides/coordinate-systems-axes/execute-results/html.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"hash": "4cb2b665dc3e3e6c80b5890b7dfdb21f", | ||
"result": { | ||
"engine": "knitr", | ||
"markdown": "---\ntitle: \"Coordinate systems and axes\"\nauthor: \"Claus O. Wilke\"\ndate: last-modified\nformat: \n revealjs:\n theme:\n - default\n - Wilke-theme.scss\n auto-stretch: false\n chalkboard: true\nexecute:\n fig-format: svg\n fig-asp: 0.618\n fig-width: 5.5\n---\n\n\n\n\n\n## Most data visualizations use Cartesian coordinates\n\n\n\n::: {.cell}\n::: {.cell-output-display}\n![ ](coordinate-systems-axes_files/figure-revealjs/cartesian-coord-1.svg){width=55%}\n:::\n:::\n\n\n\n## Changing units does not change the plot\n\n::: {.columns}\n\n::: {.column width=\"50%\"}\n\n\n::: {.cell}\n::: {.cell-output-display}\n![ ](coordinate-systems-axes_files/figure-revealjs/temperature-normals-Houston-San-Diego-1.svg){width=90%}\n:::\n:::\n\n\n:::\n\n::: {.fragment .column width=\"50%\"}\n\n\n::: {.cell}\n::: {.cell-output-display}\n![ ](coordinate-systems-axes_files/figure-revealjs/temperature-normals-Houston-San-Diego-Celsius-1.svg){width=90%}\n:::\n:::\n\n\n:::\n\n:::\n\n## If scale units are unrelated, aspect ratio is arbitrary\n\n\n\n::: {.cell}\n::: {.cell-output-display}\n![ ](coordinate-systems-axes_files/figure-revealjs/temperature-normals-Houston-1.svg){width=65%}\n:::\n:::\n\n\n\n# Non-linear scales and coordinate systems\n\n## Logarithmic scales (log scales)\n\n::: {.small-font .move-down-1em}\nVisualize these five values: 1, 3.16, 10, 31.6, 100\n:::\n\n<br>\n\n::: {.fragment .nogap}\n\n\n::: {.cell}\n::: {.cell-output-display}\n![ ](coordinate-systems-axes_files/figure-revealjs/linear-log-scales-1.svg){width=80%}\n:::\n:::\n\n\n:::\n\n::: {.fragment .nogap}\n\n\n::: {.cell}\n::: {.cell-output-display}\n![ ](coordinate-systems-axes_files/figure-revealjs/linear-log-scales2-1.svg){width=80%}\n:::\n:::\n\n\n:::\n\n::: {.fragment .nogap}\n\n\n::: {.cell}\n::: {.cell-output-display}\n![ ](coordinate-systems-axes_files/figure-revealjs/linear-log-scales3-1.svg){width=80%}\n:::\n:::\n\n\n:::\n\n## Example: Population number of Texas counties\n\n[A linear scale emphasizes large counties]{.small-font}\n\n\n\n::: {.cell}\n::: {.cell-output-display}\n![ ](coordinate-systems-axes_files/figure-revealjs/texas-counties-linear-1.svg){width=80%}\n:::\n:::\n\n\n## Example: Population number of Texas counties\n\n[A log scale shows symmetry around the median]{.small-font}\n\n\n\n::: {.cell}\n::: {.cell-output-display}\n![ ](coordinate-systems-axes_files/figure-revealjs/texas-counties-log-1.svg){width=80%}\n:::\n:::\n\n\n\n## Nonlinear coordinate systems: Polar coordinates\n\n::: {.columns}\n\n::: {.column width=\"50%\"}\n\n\n::: {.cell}\n::: {.cell-output-display}\n![ ](coordinate-systems-axes_files/figure-revealjs/cartesian-polar-left-1.svg){width=90%}\n:::\n:::\n\n\n:::\n\n::: {.fragment .column width=\"50%\"}\n\n\n::: {.cell}\n::: {.cell-output-display}\n![ ](coordinate-systems-axes_files/figure-revealjs/cartesian-polar-right-1.svg){width=90%}\n:::\n:::\n\n\n:::\n\n:::\n\n## Cartesian vs polar example\n\n<br>\n\n::: {.columns}\n\n::: {.column width=\"50%\"}\n\n\n::: {.cell}\n::: {.cell-output-display}\n![ ](coordinate-systems-axes_files/figure-revealjs/temp-normals-vs-time-cartesian-1.svg){width=100%}\n:::\n:::\n\n\n:::\n\n::: {.column width=\"50%\"}\n\n\n::: {.cell}\n::: {.cell-output-display}\n![ ](coordinate-systems-axes_files/figure-revealjs/temp-normals-vs-time-polar-1.svg){width=100%}\n:::\n:::\n\n\n:::\n\n:::\n\n<!-- Segment ends here -->\n\n# Scales and coordinate systems in **ggplot2**\n\n## Getting the data\n\n[The `boxoffice` dataset:]{.small-font}\n\n::: {.tiny-font}\n\n\n::: {.cell}\n\n```{.r .cell-code}\nboxoffice <- tibble(\n rank = 1:5,\n title = c(\"Star Wars\", \"Jumanji\", \"Pitch Perfect 3\", \"Greatest Showman\", \"Ferdinand\"),\n amount = c(71.57, 36.17, 19.93, 8.81, 7.32) # million USD\n)\n```\n:::\n\n\n:::\n\n::: {.fragment}\n[The `tx_counties` dataset:]{.small-font}\n\n::: {.tiny-font}\n\n\n::: {.cell}\n\n```{.r .cell-code}\ntx_counties <- read_csv(\"https://wilkelab.org/SDS375/datasets/US_census.csv\") |> \n filter(state == \"Texas\") |>\n mutate(popratio = pop2010/median(pop2010)) |>\n arrange(desc(popratio)) |>\n mutate(index = 1:n())\n```\n:::\n\n\n:::\n\n:::\n\n::: {.fragment}\n[The `temperatures` dataset:]{.small-font}\n\n::: {.tiny-font}\n\n\n::: {.cell}\n\n```{.r .cell-code}\ntemperatures <- read_csv(\"https://wilkelab.org/SDS375/datasets/tempnormals.csv\") |>\n mutate(\n location = factor(\n location, levels = c(\"Death Valley\", \"Houston\", \"San Diego\", \"Chicago\")\n )\n ) |>\n select(location, station_id, day_of_year, month, temperature)\n```\n:::\n\n\n:::\n\n:::\n\n## Scale functions customize the x and y axes\n\n[Recall the box-office example from a prior lecture:]{.small-font}\n\n::: {.tiny-font}\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code}\nggplot(boxoffice) +\n aes(amount, fct_reorder(title, amount)) +\n geom_col()\n```\n\n::: {.cell-output-display}\n![](coordinate-systems-axes_files/figure-revealjs/boxoffice-scale-progression1-1.svg)\n:::\n:::\n\n\n:::\n\n## Scale functions customize the x and y axes\n\n[Add scale functions (no change in figure so far):]{.small-font}\n\n::: {.tiny-font}\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"4-5\"}\nggplot(boxoffice) +\n aes(amount, fct_reorder(title, amount)) +\n geom_col() +\n scale_x_continuous() +\n scale_y_discrete()\n```\n\n::: {.cell-output-display}\n![](coordinate-systems-axes_files/figure-revealjs/boxoffice-scale-progression2-1.svg)\n:::\n:::\n\n\n:::\n\n## Scale functions customize the x and y axes\n\n[The parameter `name` sets the axis title:]{.small-font}\n\n::: {.tiny-font}\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"5,8\"}\nggplot(boxoffice) +\n aes(amount, fct_reorder(title, amount)) +\n geom_col() +\n scale_x_continuous(\n name = \"weekend gross (million USD)\"\n ) +\n scale_y_discrete(\n name = NULL # no axis title\n )\n```\n\n::: {.cell-output-display}\n![](coordinate-systems-axes_files/figure-revealjs/boxoffice-scale-progression3-1.svg)\n:::\n:::\n\n\n:::\n\n::: {.fragment .small-font}\nNote: We could do the same with `xlab()` and `ylab()`\n:::\n\n\n## Scale functions customize the x and y axes\n\n[The parameter `limits` sets the scale limits:]{.small-font}\n\n::: {.tiny-font}\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"6\"}\nggplot(boxoffice) +\n aes(amount, fct_reorder(title, amount)) +\n geom_col() +\n scale_x_continuous(\n name = \"weekend gross (million USD)\",\n limits = c(0, 80)\n ) +\n scale_y_discrete(\n name = NULL\n )\n```\n\n::: {.cell-output-display}\n![](coordinate-systems-axes_files/figure-revealjs/boxoffice-scale-progression4-1.svg)\n:::\n:::\n\n\n:::\n\n::: {.fragment .small-font}\nNote: We could do the same with `xlim()` and `ylim()` but I advise against it, as these functions can have unexpected side-effects\n:::\n\n\n## Scale functions customize the x and y axes\n\n[The parameter `breaks` sets the axis tick positions:]{.small-font}\n\n::: {.tiny-font}\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"7\"}\nggplot(boxoffice) +\n aes(amount, fct_reorder(title, amount)) +\n geom_col() +\n scale_x_continuous(\n name = \"weekend gross (million USD)\",\n limits = c(0, 80),\n breaks = c(0, 25, 50, 75)\n ) +\n scale_y_discrete(\n name = NULL\n )\n```\n\n::: {.cell-output-display}\n![](coordinate-systems-axes_files/figure-revealjs/boxoffice-scale-progression5-1.svg)\n:::\n:::\n\n\n:::\n\n## Scale functions customize the x and y axes\n\n[The parameter `labels` sets the axis tick labels:]{.small-font}\n\n::: {.tiny-font}\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"8\"}\nggplot(boxoffice) +\n aes(amount, fct_reorder(title, amount)) +\n geom_col() +\n scale_x_continuous(\n name = \"weekend gross (million USD)\",\n limits = c(0, 80),\n breaks = c(0, 25, 50, 75),\n labels = c(\"0\", \"$25M\", \"$50M\", \"$75M\")\n ) +\n scale_y_discrete(\n name = NULL\n )\n```\n\n::: {.cell-output-display}\n![](coordinate-systems-axes_files/figure-revealjs/boxoffice-scale-progression6-1.svg)\n:::\n:::\n\n\n:::\n\n## Scale functions customize the x and y axes\n\n[The parameter `expand` sets the axis expansion:]{.small-font}\n\n::: {.tiny-font}\n\n\n::: {.cell output-location='column'}\n\n```{.r .cell-code code-line-numbers=\"9\"}\nggplot(boxoffice) +\n aes(amount, fct_reorder(title, amount)) +\n geom_col() +\n scale_x_continuous(\n name = \"weekend gross (million USD)\",\n limits = c(0, 80),\n breaks = c(0, 25, 50, 75),\n labels = c(\"0\", \"$25M\", \"$50M\", \"$75M\"),\n expand = expansion(mult = c(0, 0.06))\n ) +\n scale_y_discrete(\n name = NULL\n )\n```\n\n::: {.cell-output-display}\n![](coordinate-systems-axes_files/figure-revealjs/boxoffice-scale-progression7-1.svg)\n:::\n:::\n\n\n:::\n\n## Scale functions define transformations\n\n::: {.columns}\n\n::: {.column width=\"50%\"}\n[Linear y scale:]{.small-font}\n\n::: {.tiny-font}\n\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"4\"}\nggplot(tx_counties) +\n aes(x = index, y = popratio) +\n geom_point() +\n scale_y_continuous() \n```\n\n::: {.cell-output-display}\n![ ](coordinate-systems-axes_files/figure-revealjs/tx-counties-ggplot-linear-1.svg){width=90%}\n:::\n:::\n\n\n:::\n:::\n\n::: {.fragment .column width=\"50%\"}\n[Log y scale:]{.small-font}\n\n::: {.tiny-font}\n\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"4\"}\nggplot(tx_counties) +\n aes(x = index, y = popratio) +\n geom_point() +\n scale_y_log10()\n```\n\n::: {.cell-output-display}\n![ ](coordinate-systems-axes_files/figure-revealjs/tx-counties-ggplot-log-1.svg){width=90%}\n:::\n:::\n\n\n:::\n:::\n\n:::\n\n## Parameters work the same for all scale functions\n\n::: {.columns}\n\n::: {.column width=\"50%\"}\n[Linear y scale:]{.small-font}\n\n::: {.tiny-font}\n\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"5-7\"}\nggplot(tx_counties) +\n aes(x = index, y = popratio) +\n geom_point() +\n scale_y_continuous(\n name = \"population number / median\",\n breaks = c(0, 100, 200),\n labels = c(\"0\", \"100\", \"200\")\n )\n```\n\n::: {.cell-output-display}\n![ ](coordinate-systems-axes_files/figure-revealjs/tx-counties-ggplot-linear2-1.svg){width=90%}\n:::\n:::\n\n\n:::\n:::\n\n::: {.column width=\"50%\"}\n[Log y scale:]{.small-font}\n\n::: {.tiny-font}\n\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"5-7\"}\nggplot(tx_counties) +\n aes(x = index, y = popratio) +\n geom_point() +\n scale_y_log10(\n name = \"population number / median\",\n breaks = c(0.01, 1, 100),\n labels = c(\"0.01\", \"1\", \"100\")\n )\n```\n\n::: {.cell-output-display}\n![ ](coordinate-systems-axes_files/figure-revealjs/tx-counties-ggplot-log2-1.svg){width=90%}\n:::\n:::\n\n\n:::\n:::\n\n:::\n\n## Coords define the coordinate system\n\n::: {.tiny-font}\n\n\n::: {.cell}\n\n```{.r .cell-code}\nggplot(temperatures, aes(day_of_year, temperature, color = location)) +\n geom_line() +\n coord_cartesian() # cartesian coords are the default\n```\n\n::: {.cell-output-display}\n![ ](coordinate-systems-axes_files/figure-revealjs/temperatures-cartesian-1.svg){width=60%}\n:::\n:::\n\n\n:::\n\n## Coords define the coordinate system\n\n::: {.tiny-font}\n\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"3\"}\nggplot(temperatures, aes(day_of_year, temperature, color = location)) +\n geom_line() +\n coord_polar() # polar coords\n```\n\n::: {.cell-output-display}\n![ ](coordinate-systems-axes_files/figure-revealjs/temperatures-polar-1.svg){width=60%}\n:::\n:::\n\n\n:::\n\n## Coords define the coordinate system\n\n::: {.tiny-font}\n\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"4\"}\nggplot(temperatures, aes(day_of_year, temperature, color = location)) +\n geom_line() +\n coord_polar() +\n scale_y_continuous(limits = c(0, 105)) # fix up temperature limits\n```\n\n::: {.cell-output-display}\n![ ](coordinate-systems-axes_files/figure-revealjs/temperatures-polar2-1.svg){width=60%}\n:::\n:::\n\n\n:::\n\n\n<!-- Segment ends here -->\n\n## Further reading\n\n::: {.small-font}\n- Fundamentals of Data Visualization: [Chapter 3: Coordinate systems and axes](https://clauswilke.com/dataviz/coordinate-systems-axes.html)\n- **ggplot2** reference documentation: [Scales](https://ggplot2.tidyverse.org/reference/index.html#section-scales)\n- **ggplot2** reference documentation: [Coordinate systems](https://ggplot2.tidyverse.org/reference/index.html#section-coordinate-systems)\n- **ggplot2** book: [Position scales](https://ggplot2-book.org/scale-position.html)\n- **ggplot2** book: [Coordinate systems](https://ggplot2-book.org/coord.html)\n:::", | ||
"supporting": [ | ||
"coordinate-systems-axes_files" | ||
], | ||
"filters": [ | ||
"rmarkdown/pagebreak.lua" | ||
], | ||
"includes": { | ||
"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 | ||
} | ||
} |
Oops, something went wrong.