From e7e89e86b53fbd22213911357e1a2cb69fa628a6 Mon Sep 17 00:00:00 2001 From: etiennebacher Date: Wed, 1 Jul 2026 14:18:47 +0100 Subject: [PATCH 1/4] init --- .github/workflows/format-lint-comment.yml | 105 ++++++ .github/workflows/format-lint.yml | 67 ++++ .github/workflows/templates/all-pass.md | 2 + .github/workflows/templates/jarl-only.md | 14 + .github/workflows/templates/panache-only.md | 8 + ggplot/index.qmd | 338 +++++++++++++++----- 6 files changed, 461 insertions(+), 73 deletions(-) create mode 100644 .github/workflows/format-lint-comment.yml create mode 100644 .github/workflows/format-lint.yml create mode 100644 .github/workflows/templates/all-pass.md create mode 100644 .github/workflows/templates/jarl-only.md create mode 100644 .github/workflows/templates/panache-only.md diff --git a/.github/workflows/format-lint-comment.yml b/.github/workflows/format-lint-comment.yml new file mode 100644 index 0000000..5421614 --- /dev/null +++ b/.github/workflows/format-lint-comment.yml @@ -0,0 +1,105 @@ +# This takes the results of "format-lint.yaml" and creates a comment on the PR to explain +# them. +# +# See "format-lint.yaml" for more details on why this workflow split is needed. + +name: PR comment + +on: + workflow_run: + workflows: [Format and lint] + types: [completed] + +jobs: + comment: + runs-on: ubuntu-latest + if: github.event.workflow_run.event == 'pull_request' + permissions: + pull-requests: write + actions: read + steps: + - uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6 + + - name: Download panache results + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 + continue-on-error: true + with: + name: panache-results + run-id: ${{ github.event.workflow_run.id }} + github-token: ${{ secrets.GITHUB_TOKEN }} + path: /tmp/panache + + - name: Download jarl results + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 + continue-on-error: true + with: + name: jarl-results + run-id: ${{ github.event.workflow_run.id }} + github-token: ${{ secrets.GITHUB_TOKEN }} + path: /tmp/jarl + + - name: Read results + id: results + run: | + echo "pr_number=$(cat /tmp/panache/pr-number.txt 2>/dev/null)" >> "$GITHUB_OUTPUT" + echo "panache_result=$(cat /tmp/panache/panache-result.txt 2>/dev/null)" >> "$GITHUB_OUTPUT" + echo "jarl_result=$(cat /tmp/jarl/jarl-result.txt 2>/dev/null)" >> "$GITHUB_OUTPUT" + + # After reading the results of Panache and Jarl, we have several possible outcomes: + # - both pass: if we had created a comment before because at least one of them was + # failing, then we update this comment. Otherwise, we don't open a comment just to + # say that this is passing. + # - Jarl, Panache, or both fail: we create a comment with a custom template to explain + # what failed and how to fix it locally. This should help new contributors when + # they see that CI for linting and formatting fails. + + # This is needed to know if we need to create or update a comment. + - name: Find existing comment + if: steps.results.outputs.pr_number != '' + uses: peter-evans/find-comment@b30e6a3c0ed37e7c023ccd3f1db5c6c0b0c23aad # v4.0.0 + id: find-comment + with: + issue-number: ${{ steps.results.outputs.pr_number }} + comment-author: 'github-actions[bot]' + body-includes: '' + + # Build the comment body depending on Panache and Jarl results. For the case where both + # fail, we concatenate the two single-failure templates. + # + # Note that the case with both succeeding produces a template only if we have + # "$COMMENT_ID", i.e. if we already created a comment before. If we didn't, then + # `steps.build.outputs.ready` is false and the last step isn't triggered. + - name: Build comment + id: build + if: steps.results.outputs.pr_number != '' + env: + AIR_RESULT: ${{ steps.results.outputs.panache_result }} + JARL_RESULT: ${{ steps.results.outputs.jarl_result }} + COMMENT_ID: ${{ steps.find-comment.outputs.comment-id }} + # The "echo" between the two "cat" ensures a new line between the two templates. + run: | + if [[ "$AIR_RESULT" == "failure" && "$JARL_RESULT" == "failure" ]]; then + { + cat .github/workflows/templates/panache-only.md + echo + echo "---" + echo + cat .github/workflows/templates/jarl-only.md + } > /tmp/comment.md + elif [[ "$AIR_RESULT" == "failure" ]]; then + cp .github/workflows/templates/panache-only.md /tmp/comment.md + elif [[ "$JARL_RESULT" == "failure" ]]; then + cp .github/workflows/templates/jarl-only.md /tmp/comment.md + elif [[ -n "$COMMENT_ID" ]]; then + cp .github/workflows/templates/all-pass.md /tmp/comment.md + fi + [[ -f /tmp/comment.md ]] && echo "ready=true" >> "$GITHUB_OUTPUT" || echo "ready=false" >> "$GITHUB_OUTPUT" + + - name: Create or update comment + if: steps.build.outputs.ready == 'true' && steps.results.outputs.pr_number != '' + uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9 # v5.0.0 + with: + comment-id: ${{ steps.find-comment.outputs.comment-id }} + issue-number: ${{ steps.results.outputs.pr_number }} + body-path: /tmp/comment.md + edit-mode: replace \ No newline at end of file diff --git a/.github/workflows/format-lint.yml b/.github/workflows/format-lint.yml new file mode 100644 index 0000000..5ff71e1 --- /dev/null +++ b/.github/workflows/format-lint.yml @@ -0,0 +1,67 @@ +# This workflow runs Panache for formatting and Jarl for linting. It stores artifacts about the +# outcome of each tool and the PR number. Those artifacts are then used in the workflow +# "format-lint-pr-comment.yaml" to post a comment so that it's clearer for external +# contributors. +# +# This could have been done in a single workflow with a "pull_request_target" trigger but +# this might have some security issues (this is unlikely in practice but still better to +# be ahead of it): +# https://securitylab.github.com/resources/github-actions-preventing-pwn-requests/ + +name: Format and lint + +on: + push: + branches: [main] + pull_request: + branches: [main] + +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref }} + cancel-in-progress: true + +jobs: + format-panache: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6 + - name: Install Panache + uses: jolars/panache-action@a1e4e68c74a41e6b9dade9c93095a262a23aa48c # v1 + with: + lint: "false" + continue-on-error: true + - name: Save results + if: github.event_name == 'pull_request' + run: | + echo "${{ steps.check.outcome }}" > /tmp/panache-result.txt + echo "${{ github.event.pull_request.number }}" > /tmp/pr-number.txt + - name: Upload artifacts + if: github.event_name == 'pull_request' + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 + with: + name: panache-results + path: | + /tmp/panache-result.txt + /tmp/pr-number.txt + - if: steps.check.outcome == 'failure' + run: exit 1 + + lint-jarl: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6 + - name: Check + id: check + uses: etiennebacher/setup-jarl@eabf7e572f2991d165059007531b9bbe2987e39c # v0.1.1 + continue-on-error: true + - name: Save result + if: github.event_name == 'pull_request' + run: echo "${{ steps.check.outcome }}" > /tmp/jarl-result.txt + - name: Upload artifact + if: github.event_name == 'pull_request' + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 + with: + name: jarl-results + path: /tmp/jarl-result.txt + - if: steps.check.outcome == 'failure' + run: exit 1 \ No newline at end of file diff --git a/.github/workflows/templates/all-pass.md b/.github/workflows/templates/all-pass.md new file mode 100644 index 0000000..0fa95eb --- /dev/null +++ b/.github/workflows/templates/all-pass.md @@ -0,0 +1,2 @@ + +:white_check_mark: All formatting and linting checks passed! diff --git a/.github/workflows/templates/jarl-only.md b/.github/workflows/templates/jarl-only.md new file mode 100644 index 0000000..24f9c0b --- /dev/null +++ b/.github/workflows/templates/jarl-only.md @@ -0,0 +1,14 @@ + +:x: This Pull Request failed our automated **linting checks**. Please resolve these prior to requesting review. We use Jarl to automatically lint R code. Please [install it](https://jarl.etiennebacher.com/#installation) (you may need to close and reopen your IDE, e.g. RStudio or Positron, after that) and run the following command in the terminal (not the R console) to find linting issues: + +```sh +jarl check . +``` + +Some of these issues might be automatically fixed with the following command: + +```sh +jarl check . --fix +``` + +This comment will be automatically updated once linting checks pass. diff --git a/.github/workflows/templates/panache-only.md b/.github/workflows/templates/panache-only.md new file mode 100644 index 0000000..8657307 --- /dev/null +++ b/.github/workflows/templates/panache-only.md @@ -0,0 +1,8 @@ + +:x: This Pull Request failed our automated **formatting checks**. Please resolve these prior to requesting review. We use Air to automatically format R code. Please [install it](https://posit-dev.github.io/air/cli.html) (you may need to close and reopen your IDE, e.g. RStudio or Positron, after that) and run the following command in the terminal (not the R console) to reformat the code: + +```sh +air format . +``` + +This comment will be automatically updated once formatting checks pass. diff --git a/ggplot/index.qmd b/ggplot/index.qmd index 68c72c7..7097dcf 100644 --- a/ggplot/index.qmd +++ b/ggplot/index.qmd @@ -14,100 +14,164 @@ execute: warning: false message: false freeze: auto -editor: - markdown: +editor: + markdown: wrap: 72 --- # Introduction -While you can make plots with just the packages that come bundled with base R, many R users make their visualizations entirely using the [`ggplot2`](https://ggplot2.tidyverse.org/index.html) package and an [ecosystem of packages](https://exts.ggplot2.tidyverse.org/gallery/) designed around it. +While you can make plots with just the packages that come bundled with base R, +many R users make their visualizations entirely using the +[`ggplot2`](https://ggplot2.tidyverse.org/index.html) package and an [ecosystem +of packages](https://exts.ggplot2.tidyverse.org/gallery/) designed around it. + +```{r} +#| label: ggplot2 -```{r ggplot2} # load the ggplot2 package library(ggplot2) ``` -As with the previous sessions, we'll be using the Palmer penguins dataset. While we built our own combined dataset in the introduction session, now we're going to use the built-in cleaned dataset. First, let's load the dataset. Then let's inspect the data using the `glimpse()` function. +As with the previous sessions, we'll be using the Palmer penguins dataset. While +we built our own combined dataset in the introduction session, now we're going +to use the built-in cleaned dataset. First, let's load the dataset. Then let's +inspect the data using the `glimpse()` function. + +```{r} +#| label: palmerpenguins -```{r palmerpenguins} data(penguins) dplyr::glimpse(penguins) ``` -As we discovered before, this dataset includes many different measurements for individual penguins from three different studies. The studies cover both sexes of three different species of penguins from three different islands in the Palmer Archipelago. +As we discovered before, this dataset includes many different measurements for +individual penguins from three different studies. The studies cover both sexes +of three different species of penguins from three different islands in the +Palmer Archipelago. # The ggplot2 basics -The most important function in the `ggplot2` package is `ggplot()`. Note that this function doesn't include the "2" of the package name. Let's go ahead and try using this function on our penguins data. +The most important function in the `ggplot2` package is `ggplot()`. Note that +this function doesn't include the "2" of the package name. Let's go ahead and +try using this function on our penguins data. + +```{r} +#| label: ggplot-raw -```{r ggplot-raw} ggplot(penguins) ``` -You'll notice that the `ggplot()` function doesn't actually do much by itself. Here, we provide it with the penguins dataset, but the result looks like someone started making a plot and then stopped after the first step of making the rectangle for the plot area. This is because `ggplot2` is designed around the "grammar of graphics". Therefore, it expects you to build a sentence-like structure out of its functions. A single word (i.e., the call to `ggplot()` above) doesn't make much of a sentence, so let's start building up a real sentence. +You'll notice that the `ggplot()` function doesn't actually do much by itself. +Here, we provide it with the penguins dataset, but the result looks like someone +started making a plot and then stopped after the first step of making the +rectangle for the plot area. This is because `ggplot2` is designed around the +"grammar of graphics". Therefore, it expects you to build a sentence-like +structure out of its functions. A single word (i.e., the call to `ggplot()` +above) doesn't make much of a sentence, so let's start building up a real +sentence. + +By using the `ggplot()` function, we are essentially stating that we are +beginning a plotting "sentence". We then combine this with other "words" +(function calls) using the `+` operator. The next component you usually want to +specify in a `ggplot` "sentence" is our "aesthetic" mappings. These specify the +columns of the dataset that correspond to each axis of the plot, including the +x/y axes, but also the axes of color, shape, etc. We do this with the `aes()` +function: -By using the `ggplot()` function, we are essentially stating that we are beginning a plotting "sentence". We then combine this with other "words" (function calls) using the `+` operator. The next component you usually want to specify in a `ggplot` "sentence" is our "aesthetic" mappings. These specify the columns of the dataset that correspond to each axis of the plot, including the x/y axes, but also the axes of color, shape, etc. We do this with the `aes()` function: +```{r} +#| label: ggplot-aes -```{r ggplot-aes} ggplot(penguins) + aes(x = body_mass, y = flipper_len) ``` -Hey, it's starting to look like a plot now! Except there isn't any actual data being plotted. Let's fix that. We'll start off with a simple scatter plot by using the `geom_point()` function: +Hey, it's starting to look like a plot now! Except there isn't any actual data +being plotted. Let's fix that. We'll start off with a simple scatter plot by +using the `geom_point()` function: + +```{r} +#| label: geom_point -```{r geom_point} ggplot(penguins) + aes(x = body_mass, y = flipper_len) + geom_point() ``` -And there we go! You'll notice that with just a few lines, we've already made a pretty nice visualization of this penguin data. `ggplot2` does most of the work for us once we specify our dataset and our `x` and `y` variables. +And there we go! You'll notice that with just a few lines, we've already made a +pretty nice visualization of this penguin data. `ggplot2` does most of the work +for us once we specify our dataset and our `x` and `y` variables. -:::: {.callout-note} +::: {.callout-note} ## Missing data -You may have noticed a warning that some rows of the dataset contain missing values. There are two penguins without any measurements in the dataset. How might we remove them using `dplyr` so we don't get this warning over and over? -::: {.callout-caution collapse="true" appearance="simple" icon="false"} +You may have noticed a warning that some rows of the dataset contain missing +values. There are two penguins without any measurements in the dataset. How +might we remove them using `dplyr` so we don't get this warning over and over? + +::::: {.callout-caution collapse="true" appearance="simple" icon="false"} ##### Solution + ```{r} penguins <- penguins |> dplyr::filter(!is.na(body_mass)) ``` +::::: ::: -:::: -Now, let's go a step further and color the points by another variable (e.g., the island of the penguins). With `ggplot2`, all that requires is specifying another aesthetic: +Now, let's go a step further and color the points by another variable (e.g., the +island of the penguins). With `ggplot2`, all that requires is specifying another +aesthetic: + +```{r} +#| label: geom_point-color -```{r geom_point-color} ggplot(penguins) + aes(x = body_mass, y = flipper_len, color = island) + geom_point() ``` -Notice that `ggplot2` comes with its own default color scheme. However, I would strongly discourage you from using the default colors, especially as the number of categories increases (with only 3 categories here it isn't too bad). Let's try out some of the more accessible color palettes that are also available in R. +Notice that `ggplot2` comes with its own default color scheme. However, I would +strongly discourage you from using the default colors, especially as the number +of categories increases (with only 3 categories here it isn't too bad). Let's +try out some of the more accessible color palettes that are also available in R. + +First, let's try one of the +[`viridis`](https://cran.r-project.org/web/packages/viridis/vignettes/intro-to-viridis.html) +color palettes. Since this palette is included in `ggplot2`, all we need to do +is add the proper "scale" to our `ggplot()` call. Scales tell ggplot how to +handle a particular aesthetic, and are usually of the form +`scale_[aesthetic]_[type]()`. -First, let's try one of the [`viridis`](https://cran.r-project.org/web/packages/viridis/vignettes/intro-to-viridis.html) color palettes. Since this palette is included in `ggplot2`, all we need to do is add the proper "scale" to our `ggplot()` call. Scales tell ggplot how to handle a particular aesthetic, and are usually of the form `scale_[aesthetic]_[type]()`. +```{r} +#| label: viridis -```{r viridis} ggplot(penguins) + aes(x = body_mass, y = flipper_len, color = island) + geom_point() + scale_color_viridis_d(end = 0.7) # avoid yellow at the end of the palette ``` -Now let's try one of the [brewer color palettes](https://r-graph-gallery.com/38-rcolorbrewers-palettes.html). +Now let's try one of the [brewer color +palettes](https://r-graph-gallery.com/38-rcolorbrewers-palettes.html). + +```{r} +#| label: brewer -```{r brewer} ggplot(penguins) + aes(x = body_mass, y = flipper_len, color = island) + geom_point() + scale_color_brewer(palette = "Set1") ``` -Outside of color, there are many other [aspects of the graph](https://ggplot2.tidyverse.org/articles/ggplot2-specs.html) that we can modify using aesthetics and "scale"s. For example, we can modify the shape of the points: +Outside of color, there are many other [aspects of the +graph](https://ggplot2.tidyverse.org/articles/ggplot2-specs.html) that we can +modify using aesthetics and "scale"s. For example, we can modify the shape of +the points: + +```{r} +#| label: shape-aes -```{r shape-aes} ggplot(penguins) + aes(x = body_mass, y = flipper_len, color = island, shape = sex) + @@ -117,9 +181,16 @@ ggplot(penguins) + ::: {.callout-note} ### More missing data -Urgh, more missing data! It appears that nine of the penguins are missing sex identification data. We can ignore the warning message, but that extra "NA" category in the legend is quite annoying. We can remove this category from the legend using `na.translate = FALSE` within `scale_shape_discrete()` (you could also use `scale_shape_manual()` if you wanted to supply your own shapes): -```{r na.translate} +Urgh, more missing data! It appears that nine of the penguins are missing sex +identification data. We can ignore the warning message, but that extra "NA" +category in the legend is quite annoying. We can remove this category from the +legend using `na.translate = FALSE` within `scale_shape_discrete()` (you could +also use `scale_shape_manual()` if you wanted to supply your own shapes): + +```{r} +#| label: na.translate + ggplot(penguins) + aes(x = body_mass, y = flipper_len, color = island, shape = sex) + @@ -131,7 +202,9 @@ ggplot(penguins) + And the x/y axes: -```{r axis-scales} +```{r} +#| label: axis-scales + ggplot(penguins) + aes(x = body_mass, y = flipper_len, color = island, shape = sex) + @@ -144,9 +217,15 @@ ggplot(penguins) + # Theming -The last basic thing you might want to do with `ggplot2` is modify the style of the visualization. This is extremely customizable, but the first place to start is with a [built-in theme](https://ggplot2.tidyverse.org/reference/ggtheme.html). I personally prefer the classic theme, which looks very similar to base R plots: +The last basic thing you might want to do with `ggplot2` is modify the style of +the visualization. This is extremely customizable, but the first place to start +is with a [built-in +theme](https://ggplot2.tidyverse.org/reference/ggtheme.html). I personally +prefer the classic theme, which looks very similar to base R plots: + +```{r} +#| label: theme_classic -```{r theme_classic} ggplot(penguins) + aes(x = body_mass, y = flipper_len, color = island, shape = sex) + @@ -158,9 +237,16 @@ ggplot(penguins) + theme_classic() ``` -Using this built-in theme has changed many visual aspects of the graph, including changing the plot background color, adding axis lines, and removing the internal grid lines. If you look very closely, however, the axis tick labels are still a slight grey. We can use the `theme()` function to further customize the appearance and change this. In this case, we'll make the axis text elements have a black color instead of the default gray. +Using this built-in theme has changed many visual aspects of the graph, +including changing the plot background color, adding axis lines, and removing +the internal grid lines. If you look very closely, however, the axis tick labels +are still a slight grey. We can use the `theme()` function to further customize +the appearance and change this. In this case, we'll make the axis text elements +have a black color instead of the default gray. + +```{r} +#| label: theming -```{r theming} ggplot(penguins) + aes(x = body_mass, y = flipper_len, color = island, shape = sex) + @@ -173,21 +259,33 @@ ggplot(penguins) + theme(axis.text = element_text(color = "black")) ``` -And there we have it! With just 10 lines we've created what I would say is a publication quality graph! `ggplot` does a lot of the tedious work for us, giving us time to focus on the more important aspects, such as labeling and color. Admittedly, I've spent a LOT of time on these aspects in the past... +And there we have it! With just 10 lines we've created what I would say is a +publication quality graph! `ggplot` does a lot of the tedious work for us, +giving us time to focus on the more important aspects, such as labeling and +color. Admittedly, I've spent a LOT of time on these aspects in the past... -More information about all of the hierarchical theme components that you can customize is available [here](https://ggplot2.tidyverse.org/reference/theme.html). In order to change many of these components, you need to use theme elements like we did above with `element_text()`. That and other theme elements are documented [here](https://ggplot2.tidyverse.org/reference/element.html). +More information about all of the hierarchical theme components that you can +customize is available +[here](https://ggplot2.tidyverse.org/reference/theme.html). In order to change +many of these components, you need to use theme elements like we did above with +`element_text()`. That and other theme elements are documented +[here](https://ggplot2.tidyverse.org/reference/element.html). # More complex features ## Other layers -There are many other [types of plots](https://ggplot2.tidyverse.org/reference/#layers) that we can make with `ggplot2`. +There are many other [types of +plots](https://ggplot2.tidyverse.org/reference/#layers) that we can make with +`ggplot2`. ### Histograms We can visualize the density of values for a single variable with a histogram: -```{r geom_histogram} +```{r} +#| label: geom_histogram + ggplot(penguins) + aes(x = body_mass, fill = species) + geom_histogram() + @@ -196,9 +294,13 @@ ggplot(penguins) + ``` ::: {.callout-note} -Histograms don't require a y-axis aesthetic by default. The counts are tabulated for you. If you specify a "fill" aesthetic, the default is to stack the bars which can sometimes be a bit misleading. You can also dodge them to fix this: +Histograms don't require a y-axis aesthetic by default. The counts are tabulated +for you. If you specify a "fill" aesthetic, the default is to stack the bars +which can sometimes be a bit misleading. You can also dodge them to fix this: + +```{r} +#| label: hist-dodge -```{r hist-dodge} ggplot(penguins) + aes(x = body_mass, fill = species) + geom_histogram(position = "dodge") + @@ -209,9 +311,12 @@ ggplot(penguins) + ### Boxplots and Violin Plotss -We can visualize the density of values for a single variable across a discrete variable with boxplots or violin plots: +We can visualize the density of values for a single variable across a discrete +variable with boxplots or violin plots: + +```{r} +#| label: geom_boxplot -```{r geom_boxplot} ggplot(penguins) + aes(x = island, y = bill_len) + geom_boxplot() + @@ -219,7 +324,9 @@ ggplot(penguins) + theme(axis.text = element_text(color = "black")) ``` -```{r geom_violin} +```{r} +#| label: geom_violin + ggplot(penguins) + aes(x = island, y = bill_len) + geom_violin(scale = "width", draw_quantiles = c(0.25, 0.5, 0.75)) + @@ -229,23 +336,34 @@ ggplot(penguins) + ::: {.callout-note} #### Geom options -Note that many of these "geom"s have lots of options. For example, here we've decided to `scale` all of the violin plots to the same width and to draw the quartiles on them (mimicking the boxplots above). You can see all of the options for a geom by checking out it's help page (`?geom_violin`) or on the ggplot [website](https://ggplot2.tidyverse.org/reference/geom_violin.html). + +Note that many of these "geom"s have lots of options. For example, here we've +decided to `scale` all of the violin plots to the same width and to draw the +quartiles on them (mimicking the boxplots above). You can see all of the options +for a geom by checking out it's help page (`?geom_violin`) or on the ggplot +[website](https://ggplot2.tidyverse.org/reference/geom_violin.html). ::: ### 2D Contours -We can also visualize the density of values across two continuous variables using a 2D contour: +We can also visualize the density of values across two continuous variables +using a 2D contour: + +```{r} +#| label: geom_density_2d -```{r geom_density_2d} ggplot(penguins) + aes(x = bill_len, y = bill_dep) + geom_density_2d(linewidth = 0.25, colour = "black") + theme_classic() + theme(axis.text = element_text(color = "black")) ``` -Note that sometimes you may need to expand the axes a little bit to better show the contours: +Note that sometimes you may need to expand the axes a little bit to better show +the contours: + +```{r} +#| label: geom_density_2d_expand -```{r geom_density_2d_expand} ggplot(penguins) + aes(x = bill_len, y = bill_dep) + geom_density_2d(linewidth = 0.25, colour = "black") + @@ -256,18 +374,26 @@ ggplot(penguins) + ### Time Series -Since there isn't really any time series data in the penguins dataset, we'll take a quick detour and use the built-in `economics` dataset to explore visualizing a time series. In this case, we are looking at unemployment over time: +Since there isn't really any time series data in the penguins dataset, we'll +take a quick detour and use the built-in `economics` dataset to explore +visualizing a time series. In this case, we are looking at unemployment over +time: + +```{r} +#| label: geom_line -```{r geom_line} ggplot(economics, aes(x = date, y = unemploy)) + geom_line() + theme_classic() + theme(axis.text = element_text(color = "black")) ``` -`geom_path()` lets you explore how two variables are related over time. For example, unemployment and personal savings rate: +`geom_path()` lets you explore how two variables are related over time. For +example, unemployment and personal savings rate: + +```{r} +#| label: geom_path -```{r geom_path} ggplot(economics, aes(x = unemploy / pop, y = psavert)) + geom_path(aes(colour = as.numeric(date))) + theme_classic() + @@ -276,14 +402,21 @@ ggplot(economics, aes(x = unemploy / pop, y = psavert)) + ::: {.callout-note} #### Multiple columns for individual aesthetics -Note how we've used multiple columns of the data to define the x-axis here. You can use any sort of mathematical operators to combine multiple columns into a single aesthetic, as long as you are doing row-wise math. + +Note how we've used multiple columns of the data to define the x-axis here. You +can use any sort of mathematical operators to combine multiple columns into a +single aesthetic, as long as you are doing row-wise math. ::: ## Combining layers -We can also combine multiple layers to show the same data in different ways in the same plot. For example, we could show the raw data for the above contour plot in addition to the contours: +We can also combine multiple layers to show the same data in different ways in +the same plot. For example, we could show the raw data for the above contour +plot in addition to the contours: + +```{r} +#| label: combined-layers -```{r combined-layers} ggplot(penguins) + aes(x = bill_len, y = bill_dep) + geom_point() + @@ -298,14 +431,22 @@ ggplot(penguins) + ::: {.callout-note} ### Layer order -When combining layers, the layers are added to the plot in order, so in this case the points are the bottom layer and the contour lines are the top layer. We changed the alpha of the middle layer to prevent the points from being blocked. I've also used the `coord_cartesian()` function to remove the default axis expansion. This way the background color reaches both axes and doesn't have a white gap. + +When combining layers, the layers are added to the plot in order, so in this +case the points are the bottom layer and the contour lines are the top layer. We +changed the alpha of the middle layer to prevent the points from being blocked. +I've also used the `coord_cartesian()` function to remove the default axis +expansion. This way the background color reaches both axes and doesn't have a +white gap. ::: ## Facetting Let's take our scatterplot example from earlier: -```{r scatter-full} +```{r} +#| label: scatter-full + ggplot(penguins) + aes(x = body_mass, y = flipper_len, color = island, shape = sex) + @@ -318,9 +459,15 @@ ggplot(penguins) + theme(axis.text = element_text(color = "black")) ``` -Now, what if we wanted to also split the data by the species of the penguins? We're already using color and shape, so what other aesthetic could we use? We could possible use some shapes that have both a fill and outline color, but that sounds messy. Instead of using another aesthetic, we could also use a `facet`. This splits the chart into multiple panels: +Now, what if we wanted to also split the data by the species of the penguins? +We're already using color and shape, so what other aesthetic could we use? We +could possible use some shapes that have both a fill and outline color, but that +sounds messy. Instead of using another aesthetic, we could also use a `facet`. +This splits the chart into multiple panels: + +```{r} +#| label: scatter-facet -```{r scatter-facet} ggplot(penguins) + aes(x = body_mass, y = flipper_len, color = island, shape = sex) + @@ -336,7 +483,9 @@ ggplot(penguins) + We can get even crazier by faceting by multiple variables: -```{r scatter-facet-grid} +```{r} +#| label: scatter-facet-grid + ggplot(penguins) + aes(x = body_mass, y = flipper_len, color = island, shape = sex) + @@ -353,15 +502,28 @@ ggplot(penguins) + OK, maybe we've gone a little too far here, but you get the picture! # Combining plots -When publishing results, often we need to combine multiple figures into a single visualization. There are lots of packages for accomplishing this (even my own [deeptime](https://williamgearty.com/deeptime) package has some functionality for it), but today we'll check out the [`patchwork`](https://patchwork.data-imaginist.com/) package which extends the "grammar of graphics" to combining plots (you may need to install it if you haven't done so already). -```{r patchwork} +When publishing results, often we need to combine multiple figures into a single +visualization. There are lots of packages for accomplishing this (even my own +[deeptime](https://williamgearty.com/deeptime) package has some functionality +for it), but today we'll check out the +[`patchwork`](https://patchwork.data-imaginist.com/) package which extends the +"grammar of graphics" to combining plots (you may need to install it if you +haven't done so already). + +```{r} +#| label: patchwork + library(patchwork) ``` -First, let's go ahead and make some plots. Instead of plotting them, though, we'll save them as objects in our environment. Note that when you save a plot to an object it isn't displayed in the "Plots" tab. +First, let's go ahead and make some plots. Instead of plotting them, though, +we'll save them as objects in our environment. Note that when you save a plot to +an object it isn't displayed in the "Plots" tab. + +```{r} +#| label: plot-objects -```{r plot-objects} g1 <- ggplot(penguins) + aes(x = body_mass, y = flipper_len) + geom_point() + @@ -380,15 +542,23 @@ g2 <- ggplot(penguins) + theme(axis.text = element_text(color = "black")) ``` -Now, in order to combine these, all we need to do is combine them using the `+` operator, like we did with the individual elements of the plots. +Now, in order to combine these, all we need to do is combine them using the `+` +operator, like we did with the individual elements of the plots. + +```{r} +#| label: add-plots -```{r add-plots} g1 + g2 ``` -You can see that `patchwork` does all of the work for us, lining up the different components of the plots. If we have more plots to combine, we can then use the `|` (side-by-side) and `/` (above-and-below) operators to make more complex arrangements of plots. +You can see that `patchwork` does all of the work for us, lining up the +different components of the plots. If we have more plots to combine, we can then +use the `|` (side-by-side) and `/` (above-and-below) operators to make more +complex arrangements of plots. + +```{r} +#| label: patchwork-complex -```{r patchwork-complex} g3 <- ggplot(penguins) + aes(x = island, y = bill_len) + geom_boxplot() + @@ -398,15 +568,37 @@ g3 <- ggplot(penguins) + (g1 | g2) / g3 ``` -There's a lot more you can do with `patchwork`, including adjusting the widths and heights, but we'll stop here for now. +There's a lot more you can do with `patchwork`, including adjusting the widths +and heights, but we'll stop here for now. # Saving plots -The last big thing you'll need to know about plotting is how to save your plots. `ggplot2` comes with a nifty `ggsave()` function which you can use to save your plots in a number of different formats. Here we'll save our most recent combined plot as both a PDF and a JPEG. The former is a vector format, meaning all of the elements of the figure as geometric shapes, and, because of this, none of the data is lost (aka "lossless"). The latter is a raster format, meaning the figure is converted to a 2-D array of colored pixels of a desired size, and because of this, some of the data is lost in the process (aka "lossy"). This results in the pixellation that you see when you zoom in on a JPEG. `ggsave()` detects what format you want based on the file extension, so we just need to specify the file location and the object to be saved (and optionally the dimensions of the output file). It's usually a good idea to keep all figures in their own folder (which was already created for you). -```{r ggsave, eval=FALSE} +The last big thing you'll need to know about plotting is how to save your plots. +`ggplot2` comes with a nifty `ggsave()` function which you can use to save your +plots in a number of different formats. Here we'll save our most recent combined +plot as both a PDF and a JPEG. The former is a vector format, meaning all of the +elements of the figure as geometric shapes, and, because of this, none of the +data is lost (aka "lossless"). The latter is a raster format, meaning the figure +is converted to a 2-D array of colored pixels of a desired size, and because of +this, some of the data is lost in the process (aka "lossy"). This results in the +pixellation that you see when you zoom in on a JPEG. `ggsave()` detects what +format you want based on the file extension, so we just need to specify the file +location and the object to be saved (and optionally the dimensions of the output +file). It's usually a good idea to keep all figures in their own folder (which +was already created for you). + +```{r} +#| label: ggsave +#| eval: false + gg <- (g1 | g2) / g3 ggsave("figures/penguins_1.pdf", gg, height = 10, width = 10) ggsave("figures/penguins_1.jpg", gg, height = 10, width = 10) ``` -Now we'll once again use GitHub Desktop to commit these files and push them to our GitHub repository. You should see all of the files that have been modified since we last committed. In this case, you should see your code file (modified from our last session) and your two new figures. Make sure the checkboxes are checked for these three files, write a succinct commit summary, then click the Commit button, then the Push button. And that's that! +Now we'll once again use GitHub Desktop to commit these files and push them to +our GitHub repository. You should see all of the files that have been modified +since we last committed. In this case, you should see your code file (modified +from our last session) and your two new figures. Make sure the checkboxes are +checked for these three files, write a succinct commit summary, then click the +Commit button, then the Push button. And that's that! From facc640ec9b4a487e1b651790db6b893234c2527 Mon Sep 17 00:00:00 2001 From: etiennebacher Date: Wed, 1 Jul 2026 14:23:59 +0100 Subject: [PATCH 2/4] tweak comment --- .github/workflows/format-lint-comment.yml | 6 +++--- .github/workflows/templates/panache-only.md | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/format-lint-comment.yml b/.github/workflows/format-lint-comment.yml index 5421614..be8bbaf 100644 --- a/.github/workflows/format-lint-comment.yml +++ b/.github/workflows/format-lint-comment.yml @@ -73,12 +73,12 @@ jobs: id: build if: steps.results.outputs.pr_number != '' env: - AIR_RESULT: ${{ steps.results.outputs.panache_result }} + PANACHE_RESULT: ${{ steps.results.outputs.panache_result }} JARL_RESULT: ${{ steps.results.outputs.jarl_result }} COMMENT_ID: ${{ steps.find-comment.outputs.comment-id }} # The "echo" between the two "cat" ensures a new line between the two templates. run: | - if [[ "$AIR_RESULT" == "failure" && "$JARL_RESULT" == "failure" ]]; then + if [[ "$PANACHE_RESULT" == "failure" && "$JARL_RESULT" == "failure" ]]; then { cat .github/workflows/templates/panache-only.md echo @@ -86,7 +86,7 @@ jobs: echo cat .github/workflows/templates/jarl-only.md } > /tmp/comment.md - elif [[ "$AIR_RESULT" == "failure" ]]; then + elif [[ "$PANACHE_RESULT" == "failure" ]]; then cp .github/workflows/templates/panache-only.md /tmp/comment.md elif [[ "$JARL_RESULT" == "failure" ]]; then cp .github/workflows/templates/jarl-only.md /tmp/comment.md diff --git a/.github/workflows/templates/panache-only.md b/.github/workflows/templates/panache-only.md index 8657307..b04478d 100644 --- a/.github/workflows/templates/panache-only.md +++ b/.github/workflows/templates/panache-only.md @@ -1,8 +1,8 @@ -:x: This Pull Request failed our automated **formatting checks**. Please resolve these prior to requesting review. We use Air to automatically format R code. Please [install it](https://posit-dev.github.io/air/cli.html) (you may need to close and reopen your IDE, e.g. RStudio or Positron, after that) and run the following command in the terminal (not the R console) to reformat the code: +:x: This Pull Request failed our automated **formatting checks**. Please resolve these prior to requesting review. We use Panache to automatically format R code chunks in Quarto files. Please [install it](https://panache.bz/getting-started.html#installation) (you may need to close and reopen your IDE, e.g. RStudio or Positron, after that) and run the following command in the terminal (not the R console) to reformat the code: ```sh -air format . +panache format **/*.qmd ``` This comment will be automatically updated once formatting checks pass. From 04d60ee4816460a9727b4b9c0e184e98b4a0de3c Mon Sep 17 00:00:00 2001 From: etiennebacher Date: Thu, 16 Jul 2026 14:52:51 +0100 Subject: [PATCH 3/4] fix step id --- .github/workflows/format-lint.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/format-lint.yml b/.github/workflows/format-lint.yml index 5ff71e1..610f881 100644 --- a/.github/workflows/format-lint.yml +++ b/.github/workflows/format-lint.yml @@ -25,7 +25,8 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6 - - name: Install Panache + - name: Run Panache + id: check uses: jolars/panache-action@a1e4e68c74a41e6b9dade9c93095a262a23aa48c # v1 with: lint: "false" From 71674b43a3ec839635614ec8c4fb212b445fe675 Mon Sep 17 00:00:00 2001 From: etiennebacher Date: Thu, 16 Jul 2026 14:53:01 +0100 Subject: [PATCH 4/4] do not wrap text, but still format code --- ggplot/index.qmd | 201 +++++++++-------------------------------------- panache.toml | 2 + 2 files changed, 41 insertions(+), 162 deletions(-) create mode 100644 panache.toml diff --git a/ggplot/index.qmd b/ggplot/index.qmd index 7097dcf..e78d40f 100644 --- a/ggplot/index.qmd +++ b/ggplot/index.qmd @@ -21,10 +21,7 @@ editor: # Introduction -While you can make plots with just the packages that come bundled with base R, -many R users make their visualizations entirely using the -[`ggplot2`](https://ggplot2.tidyverse.org/index.html) package and an [ecosystem -of packages](https://exts.ggplot2.tidyverse.org/gallery/) designed around it. +While you can make plots with just the packages that come bundled with base R, many R users make their visualizations entirely using the [`ggplot2`](https://ggplot2.tidyverse.org/index.html) package and an [ecosystem of packages](https://exts.ggplot2.tidyverse.org/gallery/) designed around it. ```{r} #| label: ggplot2 @@ -33,10 +30,7 @@ of packages](https://exts.ggplot2.tidyverse.org/gallery/) designed around it. library(ggplot2) ``` -As with the previous sessions, we'll be using the Palmer penguins dataset. While -we built our own combined dataset in the introduction session, now we're going -to use the built-in cleaned dataset. First, let's load the dataset. Then let's -inspect the data using the `glimpse()` function. +As with the previous sessions, we'll be using the Palmer penguins dataset. While we built our own combined dataset in the introduction session, now we're going to use the built-in cleaned dataset. First, let's load the dataset. Then let's inspect the data using the `glimpse()` function. ```{r} #| label: palmerpenguins @@ -45,16 +39,11 @@ data(penguins) dplyr::glimpse(penguins) ``` -As we discovered before, this dataset includes many different measurements for -individual penguins from three different studies. The studies cover both sexes -of three different species of penguins from three different islands in the -Palmer Archipelago. +As we discovered before, this dataset includes many different measurements for individual penguins from three different studies. The studies cover both sexes of three different species of penguins from three different islands in the Palmer Archipelago. # The ggplot2 basics -The most important function in the `ggplot2` package is `ggplot()`. Note that -this function doesn't include the "2" of the package name. Let's go ahead and -try using this function on our penguins data. +The most important function in the `ggplot2` package is `ggplot()`. Note that this function doesn't include the "2" of the package name. Let's go ahead and try using this function on our penguins data. ```{r} #| label: ggplot-raw @@ -62,22 +51,9 @@ try using this function on our penguins data. ggplot(penguins) ``` -You'll notice that the `ggplot()` function doesn't actually do much by itself. -Here, we provide it with the penguins dataset, but the result looks like someone -started making a plot and then stopped after the first step of making the -rectangle for the plot area. This is because `ggplot2` is designed around the -"grammar of graphics". Therefore, it expects you to build a sentence-like -structure out of its functions. A single word (i.e., the call to `ggplot()` -above) doesn't make much of a sentence, so let's start building up a real -sentence. - -By using the `ggplot()` function, we are essentially stating that we are -beginning a plotting "sentence". We then combine this with other "words" -(function calls) using the `+` operator. The next component you usually want to -specify in a `ggplot` "sentence" is our "aesthetic" mappings. These specify the -columns of the dataset that correspond to each axis of the plot, including the -x/y axes, but also the axes of color, shape, etc. We do this with the `aes()` -function: +You'll notice that the `ggplot()` function doesn't actually do much by itself. Here, we provide it with the penguins dataset, but the result looks like someone started making a plot and then stopped after the first step of making the rectangle for the plot area. This is because `ggplot2` is designed around the "grammar of graphics". Therefore, it expects you to build a sentence-like structure out of its functions. A single word (i.e., the call to `ggplot()` above) doesn't make much of a sentence, so let's start building up a real sentence. + +By using the `ggplot()` function, we are essentially stating that we are beginning a plotting "sentence". We then combine this with other "words" (function calls) using the `+` operator. The next component you usually want to specify in a `ggplot` "sentence" is our "aesthetic" mappings. These specify the columns of the dataset that correspond to each axis of the plot, including the x/y axes, but also the axes of color, shape, etc. We do this with the `aes()` function: ```{r} #| label: ggplot-aes @@ -86,9 +62,7 @@ ggplot(penguins) + aes(x = body_mass, y = flipper_len) ``` -Hey, it's starting to look like a plot now! Except there isn't any actual data -being plotted. Let's fix that. We'll start off with a simple scatter plot by -using the `geom_point()` function: +Hey, it's starting to look like a plot now! Except there isn't any actual data being plotted. Let's fix that. We'll start off with a simple scatter plot by using the `geom_point()` function: ```{r} #| label: geom_point @@ -98,16 +72,12 @@ ggplot(penguins) + geom_point() ``` -And there we go! You'll notice that with just a few lines, we've already made a -pretty nice visualization of this penguin data. `ggplot2` does most of the work -for us once we specify our dataset and our `x` and `y` variables. +And there we go! You'll notice that with just a few lines, we've already made a pretty nice visualization of this penguin data. `ggplot2` does most of the work for us once we specify our dataset and our `x` and `y` variables. ::: {.callout-note} ## Missing data -You may have noticed a warning that some rows of the dataset contain missing -values. There are two penguins without any measurements in the dataset. How -might we remove them using `dplyr` so we don't get this warning over and over? +You may have noticed a warning that some rows of the dataset contain missing values. There are two penguins without any measurements in the dataset. How might we remove them using `dplyr` so we don't get this warning over and over? ::::: {.callout-caution collapse="true" appearance="simple" icon="false"} ##### Solution @@ -119,9 +89,7 @@ penguins <- penguins |> ::::: ::: -Now, let's go a step further and color the points by another variable (e.g., the -island of the penguins). With `ggplot2`, all that requires is specifying another -aesthetic: +Now, let's go a step further and color the points by another variable (e.g., the island of the penguins). With `ggplot2`, all that requires is specifying another aesthetic: ```{r} #| label: geom_point-color @@ -131,17 +99,9 @@ ggplot(penguins) + geom_point() ``` -Notice that `ggplot2` comes with its own default color scheme. However, I would -strongly discourage you from using the default colors, especially as the number -of categories increases (with only 3 categories here it isn't too bad). Let's -try out some of the more accessible color palettes that are also available in R. +Notice that `ggplot2` comes with its own default color scheme. However, I would strongly discourage you from using the default colors, especially as the number of categories increases (with only 3 categories here it isn't too bad). Let's try out some of the more accessible color palettes that are also available in R. -First, let's try one of the -[`viridis`](https://cran.r-project.org/web/packages/viridis/vignettes/intro-to-viridis.html) -color palettes. Since this palette is included in `ggplot2`, all we need to do -is add the proper "scale" to our `ggplot()` call. Scales tell ggplot how to -handle a particular aesthetic, and are usually of the form -`scale_[aesthetic]_[type]()`. +First, let's try one of the [`viridis`](https://cran.r-project.org/web/packages/viridis/vignettes/intro-to-viridis.html) color palettes. Since this palette is included in `ggplot2`, all we need to do is add the proper "scale" to our `ggplot()` call. Scales tell ggplot how to handle a particular aesthetic, and are usually of the form `scale_[aesthetic]_[type]()`. ```{r} #| label: viridis @@ -152,8 +112,7 @@ ggplot(penguins) + scale_color_viridis_d(end = 0.7) # avoid yellow at the end of the palette ``` -Now let's try one of the [brewer color -palettes](https://r-graph-gallery.com/38-rcolorbrewers-palettes.html). +Now let's try one of the [brewer color palettes](https://r-graph-gallery.com/38-rcolorbrewers-palettes.html). ```{r} #| label: brewer @@ -164,10 +123,7 @@ ggplot(penguins) + scale_color_brewer(palette = "Set1") ``` -Outside of color, there are many other [aspects of the -graph](https://ggplot2.tidyverse.org/articles/ggplot2-specs.html) that we can -modify using aesthetics and "scale"s. For example, we can modify the shape of -the points: +Outside of color, there are many other [aspects of the graph](https://ggplot2.tidyverse.org/articles/ggplot2-specs.html) that we can modify using aesthetics and "scale"s. For example, we can modify the shape of the points: ```{r} #| label: shape-aes @@ -182,11 +138,7 @@ ggplot(penguins) + ::: {.callout-note} ### More missing data -Urgh, more missing data! It appears that nine of the penguins are missing sex -identification data. We can ignore the warning message, but that extra "NA" -category in the legend is quite annoying. We can remove this category from the -legend using `na.translate = FALSE` within `scale_shape_discrete()` (you could -also use `scale_shape_manual()` if you wanted to supply your own shapes): +Urgh, more missing data! It appears that nine of the penguins are missing sex identification data. We can ignore the warning message, but that extra "NA" category in the legend is quite annoying. We can remove this category from the legend using `na.translate = FALSE` within `scale_shape_discrete()` (you could also use `scale_shape_manual()` if you wanted to supply your own shapes): ```{r} #| label: na.translate @@ -217,11 +169,7 @@ ggplot(penguins) + # Theming -The last basic thing you might want to do with `ggplot2` is modify the style of -the visualization. This is extremely customizable, but the first place to start -is with a [built-in -theme](https://ggplot2.tidyverse.org/reference/ggtheme.html). I personally -prefer the classic theme, which looks very similar to base R plots: +The last basic thing you might want to do with `ggplot2` is modify the style of the visualization. This is extremely customizable, but the first place to start is with a [built-in theme](https://ggplot2.tidyverse.org/reference/ggtheme.html). I personally prefer the classic theme, which looks very similar to base R plots: ```{r} #| label: theme_classic @@ -237,12 +185,7 @@ ggplot(penguins) + theme_classic() ``` -Using this built-in theme has changed many visual aspects of the graph, -including changing the plot background color, adding axis lines, and removing -the internal grid lines. If you look very closely, however, the axis tick labels -are still a slight grey. We can use the `theme()` function to further customize -the appearance and change this. In this case, we'll make the axis text elements -have a black color instead of the default gray. +Using this built-in theme has changed many visual aspects of the graph, including changing the plot background color, adding axis lines, and removing the internal grid lines. If you look very closely, however, the axis tick labels are still a slight grey. We can use the `theme()` function to further customize the appearance and change this. In this case, we'll make the axis text elements have a black color instead of the default gray. ```{r} #| label: theming @@ -259,25 +202,15 @@ ggplot(penguins) + theme(axis.text = element_text(color = "black")) ``` -And there we have it! With just 10 lines we've created what I would say is a -publication quality graph! `ggplot` does a lot of the tedious work for us, -giving us time to focus on the more important aspects, such as labeling and -color. Admittedly, I've spent a LOT of time on these aspects in the past... +And there we have it! With just 10 lines we've created what I would say is a publication quality graph! `ggplot` does a lot of the tedious work for us, giving us time to focus on the more important aspects, such as labeling and color. Admittedly, I've spent a LOT of time on these aspects in the past... -More information about all of the hierarchical theme components that you can -customize is available -[here](https://ggplot2.tidyverse.org/reference/theme.html). In order to change -many of these components, you need to use theme elements like we did above with -`element_text()`. That and other theme elements are documented -[here](https://ggplot2.tidyverse.org/reference/element.html). +More information about all of the hierarchical theme components that you can customize is available [here](https://ggplot2.tidyverse.org/reference/theme.html). In order to change many of these components, you need to use theme elements like we did above with `element_text()`. That and other theme elements are documented [here](https://ggplot2.tidyverse.org/reference/element.html). # More complex features ## Other layers -There are many other [types of -plots](https://ggplot2.tidyverse.org/reference/#layers) that we can make with -`ggplot2`. +There are many other [types of plots](https://ggplot2.tidyverse.org/reference/#layers) that we can make with `ggplot2`. ### Histograms @@ -294,9 +227,7 @@ ggplot(penguins) + ``` ::: {.callout-note} -Histograms don't require a y-axis aesthetic by default. The counts are tabulated -for you. If you specify a "fill" aesthetic, the default is to stack the bars -which can sometimes be a bit misleading. You can also dodge them to fix this: +Histograms don't require a y-axis aesthetic by default. The counts are tabulated for you. If you specify a "fill" aesthetic, the default is to stack the bars which can sometimes be a bit misleading. You can also dodge them to fix this: ```{r} #| label: hist-dodge @@ -311,8 +242,7 @@ ggplot(penguins) + ### Boxplots and Violin Plotss -We can visualize the density of values for a single variable across a discrete -variable with boxplots or violin plots: +We can visualize the density of values for a single variable across a discrete variable with boxplots or violin plots: ```{r} #| label: geom_boxplot @@ -337,17 +267,12 @@ ggplot(penguins) + ::: {.callout-note} #### Geom options -Note that many of these "geom"s have lots of options. For example, here we've -decided to `scale` all of the violin plots to the same width and to draw the -quartiles on them (mimicking the boxplots above). You can see all of the options -for a geom by checking out it's help page (`?geom_violin`) or on the ggplot -[website](https://ggplot2.tidyverse.org/reference/geom_violin.html). +Note that many of these "geom"s have lots of options. For example, here we've decided to `scale` all of the violin plots to the same width and to draw the quartiles on them (mimicking the boxplots above). You can see all of the options for a geom by checking out it's help page (`?geom_violin`) or on the ggplot [website](https://ggplot2.tidyverse.org/reference/geom_violin.html). ::: ### 2D Contours -We can also visualize the density of values across two continuous variables -using a 2D contour: +We can also visualize the density of values across two continuous variables using a 2D contour: ```{r} #| label: geom_density_2d @@ -358,8 +283,7 @@ ggplot(penguins) + theme_classic() + theme(axis.text = element_text(color = "black")) ``` -Note that sometimes you may need to expand the axes a little bit to better show -the contours: +Note that sometimes you may need to expand the axes a little bit to better show the contours: ```{r} #| label: geom_density_2d_expand @@ -374,10 +298,7 @@ ggplot(penguins) + ### Time Series -Since there isn't really any time series data in the penguins dataset, we'll -take a quick detour and use the built-in `economics` dataset to explore -visualizing a time series. In this case, we are looking at unemployment over -time: +Since there isn't really any time series data in the penguins dataset, we'll take a quick detour and use the built-in `economics` dataset to explore visualizing a time series. In this case, we are looking at unemployment over time: ```{r} #| label: geom_line @@ -388,8 +309,7 @@ ggplot(economics, aes(x = date, y = unemploy)) + theme(axis.text = element_text(color = "black")) ``` -`geom_path()` lets you explore how two variables are related over time. For -example, unemployment and personal savings rate: +`geom_path()` lets you explore how two variables are related over time. For example, unemployment and personal savings rate: ```{r} #| label: geom_path @@ -403,16 +323,12 @@ ggplot(economics, aes(x = unemploy / pop, y = psavert)) + ::: {.callout-note} #### Multiple columns for individual aesthetics -Note how we've used multiple columns of the data to define the x-axis here. You -can use any sort of mathematical operators to combine multiple columns into a -single aesthetic, as long as you are doing row-wise math. +Note how we've used multiple columns of the data to define the x-axis here. You can use any sort of mathematical operators to combine multiple columns into a single aesthetic, as long as you are doing row-wise math. ::: ## Combining layers -We can also combine multiple layers to show the same data in different ways in -the same plot. For example, we could show the raw data for the above contour -plot in addition to the contours: +We can also combine multiple layers to show the same data in different ways in the same plot. For example, we could show the raw data for the above contour plot in addition to the contours: ```{r} #| label: combined-layers @@ -432,12 +348,7 @@ ggplot(penguins) + ::: {.callout-note} ### Layer order -When combining layers, the layers are added to the plot in order, so in this -case the points are the bottom layer and the contour lines are the top layer. We -changed the alpha of the middle layer to prevent the points from being blocked. -I've also used the `coord_cartesian()` function to remove the default axis -expansion. This way the background color reaches both axes and doesn't have a -white gap. +When combining layers, the layers are added to the plot in order, so in this case the points are the bottom layer and the contour lines are the top layer. We changed the alpha of the middle layer to prevent the points from being blocked. I've also used the `coord_cartesian()` function to remove the default axis expansion. This way the background color reaches both axes and doesn't have a white gap. ::: ## Facetting @@ -459,11 +370,7 @@ ggplot(penguins) + theme(axis.text = element_text(color = "black")) ``` -Now, what if we wanted to also split the data by the species of the penguins? -We're already using color and shape, so what other aesthetic could we use? We -could possible use some shapes that have both a fill and outline color, but that -sounds messy. Instead of using another aesthetic, we could also use a `facet`. -This splits the chart into multiple panels: +Now, what if we wanted to also split the data by the species of the penguins? We're already using color and shape, so what other aesthetic could we use? We could possible use some shapes that have both a fill and outline color, but that sounds messy. Instead of using another aesthetic, we could also use a `facet`. This splits the chart into multiple panels: ```{r} #| label: scatter-facet @@ -503,13 +410,7 @@ OK, maybe we've gone a little too far here, but you get the picture! # Combining plots -When publishing results, often we need to combine multiple figures into a single -visualization. There are lots of packages for accomplishing this (even my own -[deeptime](https://williamgearty.com/deeptime) package has some functionality -for it), but today we'll check out the -[`patchwork`](https://patchwork.data-imaginist.com/) package which extends the -"grammar of graphics" to combining plots (you may need to install it if you -haven't done so already). +When publishing results, often we need to combine multiple figures into a single visualization. There are lots of packages for accomplishing this (even my own [deeptime](https://williamgearty.com/deeptime) package has some functionality for it), but today we'll check out the [`patchwork`](https://patchwork.data-imaginist.com/) package which extends the "grammar of graphics" to combining plots (you may need to install it if you haven't done so already). ```{r} #| label: patchwork @@ -517,9 +418,7 @@ haven't done so already). library(patchwork) ``` -First, let's go ahead and make some plots. Instead of plotting them, though, -we'll save them as objects in our environment. Note that when you save a plot to -an object it isn't displayed in the "Plots" tab. +First, let's go ahead and make some plots. Instead of plotting them, though, we'll save them as objects in our environment. Note that when you save a plot to an object it isn't displayed in the "Plots" tab. ```{r} #| label: plot-objects @@ -542,8 +441,7 @@ g2 <- ggplot(penguins) + theme(axis.text = element_text(color = "black")) ``` -Now, in order to combine these, all we need to do is combine them using the `+` -operator, like we did with the individual elements of the plots. +Now, in order to combine these, all we need to do is combine them using the `+` operator, like we did with the individual elements of the plots. ```{r} #| label: add-plots @@ -551,10 +449,7 @@ operator, like we did with the individual elements of the plots. g1 + g2 ``` -You can see that `patchwork` does all of the work for us, lining up the -different components of the plots. If we have more plots to combine, we can then -use the `|` (side-by-side) and `/` (above-and-below) operators to make more -complex arrangements of plots. +You can see that `patchwork` does all of the work for us, lining up the different components of the plots. If we have more plots to combine, we can then use the `|` (side-by-side) and `/` (above-and-below) operators to make more complex arrangements of plots. ```{r} #| label: patchwork-complex @@ -568,24 +463,11 @@ g3 <- ggplot(penguins) + (g1 | g2) / g3 ``` -There's a lot more you can do with `patchwork`, including adjusting the widths -and heights, but we'll stop here for now. +There's a lot more you can do with `patchwork`, including adjusting the widths and heights, but we'll stop here for now. # Saving plots -The last big thing you'll need to know about plotting is how to save your plots. -`ggplot2` comes with a nifty `ggsave()` function which you can use to save your -plots in a number of different formats. Here we'll save our most recent combined -plot as both a PDF and a JPEG. The former is a vector format, meaning all of the -elements of the figure as geometric shapes, and, because of this, none of the -data is lost (aka "lossless"). The latter is a raster format, meaning the figure -is converted to a 2-D array of colored pixels of a desired size, and because of -this, some of the data is lost in the process (aka "lossy"). This results in the -pixellation that you see when you zoom in on a JPEG. `ggsave()` detects what -format you want based on the file extension, so we just need to specify the file -location and the object to be saved (and optionally the dimensions of the output -file). It's usually a good idea to keep all figures in their own folder (which -was already created for you). +The last big thing you'll need to know about plotting is how to save your plots. `ggplot2` comes with a nifty `ggsave()` function which you can use to save your plots in a number of different formats. Here we'll save our most recent combined plot as both a PDF and a JPEG. The former is a vector format, meaning all of the elements of the figure as geometric shapes, and, because of this, none of the data is lost (aka "lossless"). The latter is a raster format, meaning the figure is converted to a 2-D array of colored pixels of a desired size, and because of this, some of the data is lost in the process (aka "lossy"). This results in the pixellation that you see when you zoom in on a JPEG. `ggsave()` detects what format you want based on the file extension, so we just need to specify the file location and the object to be saved (and optionally the dimensions of the output file). It's usually a good idea to keep all figures in their own folder (which was already created for you). ```{r} #| label: ggsave @@ -596,9 +478,4 @@ ggsave("figures/penguins_1.pdf", gg, height = 10, width = 10) ggsave("figures/penguins_1.jpg", gg, height = 10, width = 10) ``` -Now we'll once again use GitHub Desktop to commit these files and push them to -our GitHub repository. You should see all of the files that have been modified -since we last committed. In this case, you should see your code file (modified -from our last session) and your two new figures. Make sure the checkboxes are -checked for these three files, write a succinct commit summary, then click the -Commit button, then the Push button. And that's that! +Now we'll once again use GitHub Desktop to commit these files and push them to our GitHub repository. You should see all of the files that have been modified since we last committed. In this case, you should see your code file (modified from our last session) and your two new figures. Make sure the checkboxes are checked for these three files, write a succinct commit summary, then click the Commit button, then the Push button. And that's that! diff --git a/panache.toml b/panache.toml new file mode 100644 index 0000000..c9b394a --- /dev/null +++ b/panache.toml @@ -0,0 +1,2 @@ +[format] +wrap = "preserve" \ No newline at end of file