{"id":13712843,"url":"https://github.com/olihawkins/pilot","last_synced_at":"2026-01-16T16:57:05.306Z","repository":{"id":48142485,"uuid":"179835351","full_name":"olihawkins/pilot","owner":"olihawkins","description":"A minimal ggplot2 theme with an accessible discrete color palette.","archived":false,"fork":false,"pushed_at":"2023-03-08T09:51:23.000Z","size":15961,"stargazers_count":83,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-13T23:32:39.630Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/olihawkins.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-04-06T13:14:25.000Z","updated_at":"2024-05-24T04:19:21.000Z","dependencies_parsed_at":"2024-01-07T01:43:42.135Z","dependency_job_id":"de126ec6-641d-4537-a9a1-4a99364ff7fb","html_url":"https://github.com/olihawkins/pilot","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/olihawkins%2Fpilot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/olihawkins%2Fpilot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/olihawkins%2Fpilot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/olihawkins%2Fpilot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/olihawkins","download_url":"https://codeload.github.com/olihawkins/pilot/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252779106,"owners_count":21802884,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T23:01:23.447Z","updated_at":"2026-01-16T16:57:05.291Z","avatar_url":"https://github.com/olihawkins.png","language":"R","funding_links":[],"categories":["Themes and aesthetics"],"sub_categories":[],"readme":"# pilot\n\n`pilot` is an attractive, minimal, general purpose ggplot2 theme with an accessible discrete color palette.\n\n## Changes in version 4.0\n\nThe custom functions for saving plots `save_png` and `save_svg` have been removed in favour of using `ggsave`. This removes an unnecessary dependency on `rsvg`, making the package easier to maintain and to use in different computing environments. The code examples have all been updated to reflect this change. \n\nIf you have existing code that uses the functions that have been removed, replacing them with `ggsave` is as easy as reversing the order of the first two arguments, which are for the `plot` and the `filename`. If you are using named arguments to set these values, it requires no change to the arguments at all. So for example, this call to `save_png`:\n\n```r\nsave_png(\n    plot,\n    \"plotfile.png\",\n    width = 8,\n    height = 6)\n```\n\nCan be replaced with this call to `ggsave`:\n\n```r\nggsave(\n    \"plotfile.png\",\n    plot,\n    width = 8,\n    height = 6)\n```\n\nOptionally, you may also wish to set the `dpi` argument in `ggsave` to `400`, as this was the default value for saving plots with `save_png`.\n\n## Installation\n\nInstall from GitHub using remotes.\n\n``` r\ninstall.packages(\"remotes\")\nremotes::install_github(\"olihawkins/pilot\")\n```\n\n## Basic usage\n\nImport the package and add `theme_pilot` to a plot made with `ggplot` in the normal way.\n\n```r\nlibrary(ggplot2)\nlibrary(pilot)\n\nplot \u003c- ggplot(\n        data = mpg,\n        mapping = aes(\n            x = displ, \n            y = hwy, \n            color = class)) + \n    geom_point() +\n    labs(\n        title = \"Cars with smaller engines are more efficient\",\n        subtitle = \"Engine size by fuel efficiency and class\",\n        x = \"Engine size in litres\",\n        y = \"Miles per gallon\",\n        color = \"Class\",\n        caption = \"Reproduced from Chapter 3 of R for Data Science\") + \n    theme_pilot() +\n    scale_color_pilot()\n```\n\n\u003cimg src=\"gallery/examples/scatter-chart-basic/scatter-chart-basic-1.png\" width=\"800\" /\u003e\n\nBy default, ggplot2 horizontally aligns the title and subtitle with the left-hand edge of the plotting area. A helper function called `add_pilot_titles` is available that will align the title and subtitle with the left-hand edge of the whole plot instead. To use it, first create the plot in the normal way, but without specifying the title and subtitle, and then use `add_pilot_titles` to add the titles to the plot. \n\n```r\nlibrary(ggplot2)\nlibrary(pilot)\n\nplot \u003c- ggplot(\n        data = mpg,\n        mapping = aes(\n            x = displ, \n            y = hwy, \n            color = class)) + \n    geom_point() +\n    labs(\n        x = \"Engine size in litres\",\n        y = \"Miles per gallon\",\n        color = \"Class\",\n        caption = \"Reproduced from Chapter 3 of R for Data Science\") + \n    theme_pilot() +\n    scale_color_pilot()\n\nplot \u003c- add_pilot_titles(\n    plot,\n    title = \"Cars with smaller engines are more efficient\",\n    subtitle = \"Engine size by fuel efficiency and class\")\n```\n\n\u003cimg src=\"gallery/examples/scatter-chart-basic/scatter-chart-basic-2.png\" width=\"800\" /\u003e\n\nNote that `add_pilot_titles` uses `patchwork` behind the scenes to compose a new plot from the existing plot and the titles. So if you want to adjust the plot margins using a `theme` customisation, do it *after* you have applied the titles, otherwise your customisation will be overwritten by `add_pilot_titles`.\n\n## Colors\n\nThe package includes an accessible discrete color palette, comprising seven colors that aim to be visually distinct to people with the most common forms of color blindness. You can see what the colors look like under different conditions of color blindness using the [Viz Palette](https://projects.susielu.com/viz-palette?colors=[%22#204466%22,%22#b84818%22,%22#9956db%22,%22#249db5%22,%22#f28100%22,%22#30c788%22,%22#ffc517%22]) tool. \n\nThese colors are available in a named vector called `pilot_colors`. The base color names are:\n\n* __navy__\n* __blue__\n* __brown__\n* __green__\n* __yellow__\n* __purple__\n* __orange__\n\nYou can use the `pilot_color` function to return the unnamed hex code value for a given name. This makes it easy to map specific colors to categorical variables using the `scale_color_manual()` and `scale_fill_manual()` functions.\n\n```r\nscale_color_manual(values = c(\n    \"a\" = pilot_color(\"navy\"),\n    \"b\" = pilot_color(\"blue\")))\n```\n\nThese colors are also avaialable as ggplot2 scales with a range of palettes representing different subsets of the colors. See `scale_color_pilot` and `scale_fill_pilot` for further details in the function reference below. \n\nHowever, care should be taken in how you use these scales. For convenience, these scales support ggplot2's color interpolation feature. But expanding the seven color palette to represent more than seven categories risks creating new colors that are no longer visually distinct to people with color blindness. To ensure the colors remain distinct, you should only use these scales with discrete data that has the same number of categories as the palette that you choose.\n\n## Fonts\n\nThe theme uses a different default font depending on the opertating system. This is \"Avenir Next\" on MacOS and the default sans-serif font on Windows and Linux. \n\nYou can customise the fonts the theme uses in three ways: \n\n- Universally -- By setting family options for the theme in .Rprofile\n- Per R session -- By setting the options dynamically using `options` or `set_pilot_family`\n- Per plot -- By using the family arguments in `theme_pilot`\n\nThe full list of font family options that can be set in .Rprofile or using `options` are:\n\n- `pilot.title_family`\n- `pilot.subtitle_family`\n- `pilot.axis_title_family`\n- `pilot.axis_text_family`\n- `pilot.legend_title_family`\n- `pilot.legend_text_family`\n- `pilot.facet_title_family`\n- `pilot.caption_family`\n- `pilot.geom_text_family`\n- `pilot.annotate_family`\n\nThe `set_pilot_family` function sets all the font family options to the same family with a single function call.\n\n```r\nset_pilot_family(\"Helvetica Neue\")\n```\n\nYou can optionally set a different family for the main title.\n\n```r\nset_pilot_family(\"Avenir Next\", title_family = \"Avenir Next Demi Bold\")\n```\n\nUse `?theme_pilot` to see the documentation showing the full list of arguments that can be used to customise the fonts for an individual plot using the arguments in `theme_pilot`.\n\n## Function reference\n\n### Themes\n\nApply the theme to a plot with `theme_pilot()`. There are a large number of arguments you can use to configure the components of the theme. Use `?theme_pilot` to see the full list of arguments. The principal arguments are documented below.\n\n---\n\n_pilot_::__theme_pilot__(_axes = \"\"_, _grid = \"hv\"_, _legend_position = \"right\"_, _caption_position = \"right\"_, _..._)\n\nSet the theme with the following arguments:\n\n* __axes__ A string indicating which axes should have lines and ticks. Specify which axes to show by including the matching characters in the string: \"t\" for top, \"r\" for right, \"b\" for bottom, \"l\" for left. You will need to ensure this argument is consistent with the axes settings in your plot for the lines and ticks to be displayed. The default is an empty string, meaning no ticks and lines are shown on any axes by default.\n* __grid__ A string indicating which gridlines should be shown. Specify the gridlines to show by including the matching characters in the string: \"h\" for horizontal, \"v\" for vertical. The default is \"hv\", meaning both gridlines are shown by default.\n* __legend_position__ A string indicating the position of the legend. Valid positions are \"top\", \"right\", \"bottom\", \"left\", \"top-right\", \"top-left\", \"bottom-right\", \"bottom-left\", and \"none\". The default is \"right\".\n* __caption_position__ A string indicating the horizontal position of the caption. Valid positions are \"left\" or \"right\". The default is \"right\".\n\n---\n\n### Scales\n\nUse `scale_color_pilot()` or `scale_fill_pilot()` as approriate. Both functions have the same signature. Please see the note on colors above for appropiate use of these scales.\n\n---\n\n_pilot_::__scale_color_pilot__(_palette = \"seven\"_, _discrete = TRUE_, _reverse = FALSE_, _..._)\n_pilot_::__scale_fill_pilot__(_palette = \"seven\"_, _discrete = TRUE_, _reverse = FALSE_, _..._)\n\nSets the scales with the following arguments. The default palette is \"seven\".\n\n* __palette__ The name of a palette. Valid names are:\n    * _two_ - navy, blue\n    * _three_ - navy, blue, brown\n    * _four_ - navy, blue, brown, green\n    * _five_ - navy, blue, brown, green, yellow\n    * _six_ - navy, blue, brown, green, yellow, purple\n    * _seven_ - navy, blue, brown, green, yellow, purple, orange\n* __discrete__ Boolean to indicate if color aesthetic is discrete.\n* __reverse__ Boolean to indicate whether palette should be reversed.\n* __...__ Additional arguments passed to `discrete_scale` or `scale_color_gradient`, depending on the value of `discrete`.\n\n---\n\n### Wrapper functions\n\nThe package provides some variants of standard ggplot2 functions to make it easier to use the theme. These are:\n\n- `geom_text_pilot`\n- `annotate_pilot`\n\nThese are simple wrappers around the standard versions of these functions, which set the font family, style and color to those of the theme. All other arguments are passed on to the underlying ggplot2 functions transparently.\n\n---\n\n## Gallery\n\nThe following gallery shows examples of how to create different types of charts with `pilot`. The datasets for each of these can be found in the [gallery/examples](gallery/examples) folder.\n\n### Bar chart\n\n\u003cimg src=\"gallery/examples/bar-chart-labels/bar-chart-labels.png\" width=\"800\" /\u003e\n\n```r\n# Imports ---------------------------------------------------------------------\n\nlibrary(tidyverse)\nlibrary(pilot)\n\n# Read in and prepare the data ------------------------------------------------\n\n# Load the data from the csv as a dataframe\ndf \u003c- read_csv(\"bar-chart-labels.csv\")\n\n# Turn the region column into a factor and order it by the population in each\n# region: this sorts the bars in the chart from largest to smallest\ndf$region \u003c- factor(df$region)\ndf$region \u003c- fct_reorder(df$region, df$population, max)\n\n# Create the plot -------------------------------------------------------------\n\n# Use ggplot to create a plot with data\nplot \u003c- ggplot(data = df) +\n    # Add a column geometry for the bars\n    geom_col(\n        mapping = aes(\n            x = population,\n            y = region),\n        fill = pilot_color(\"navy\")) +\n    # Add a text geometry for the labels: geom_text_pilot uses the theme fonts\n    geom_text_pilot(\n        mapping = aes(\n            x = population,\n            y = region,\n            label = format(population, digits = 2)),\n        hjust = \"center\",\n        nudge_x = -0.4) +\n    # Set labels for the axes, but don't set titles here\n    labs(\n        x = \"Millions of people\",\n        y = NULL) +\n    # Configure the the x and y axes, removing the expansion for the x axis\n    scale_x_continuous(\n        limits = c(0, 10),\n        breaks = seq(0, 10, 2),\n        expand = c(0,0)) +\n    scale_y_discrete(\n        expand = expansion(add = c(0.6, 0.6))) +\n    # Add the pilot theme, setting a bottom axis with no gridlines\n    theme_pilot(\n        axes = \"b\",\n        grid = \"\")\n\n# After creating the plot, add a title and subtitle with add_pilot_titles\nplot \u003c- add_pilot_titles(\n    plot,\n    title = \"Countries and regions vary in population\",\n    subtitle = \"Population of countries and regions in mid-2020, United Kingdom\")\n\n# Save the plot in different formats ------------------------------------------\n\n# Save a high resolution export of the plot as a png\nggsave(\n    filename = \"bar-chart-labels.png\",\n    plot = plot,\n    width = 7.7,\n    height = 6.2,\n    dpi = 400)\n\n# Save an editable verson of the plot as an svg\nggsave(\n    filename = \"bar-chart-labels.svg\",\n    plot = plot,\n    width = 7.7,\n    height = 6.2,\n    dpi = 400)\n```\n\n### Line chart\n\n\u003cimg src=\"gallery/examples/line-chart/line-chart.png\" width=\"800\" /\u003e\n\n```r\n# Imports ---------------------------------------------------------------------\n\nlibrary(tidyverse)\nlibrary(pilot)\n\n# Read in and prepare the data ------------------------------------------------\n\n# Load the data from the csv as a dataframe\ndf \u003c- read_csv(\"line-chart.csv\")\n\n# Create the plot -------------------------------------------------------------\n\n# Use ggplot to create a plot with data and mappings\nplot \u003c- ggplot(\n        data = df,\n        mapping = aes(\n            x = quarter,\n            y = estimate,\n            color = flow)) +\n    # Add a line geometry to draw lines\n    geom_line(size = 1.1) +\n    # Set labels for the axes, legend, and caption, but don't set titles here\n    labs(\n        color = NULL,\n        x = NULL,\n        y = \"Thousands of people\",\n        caption = \"Source: ONS, Provisional LTIM estimates\") +\n    # Configure the the x and y axes: we set the y axis breaks and limits\n    scale_x_date(\n        expand = c(0, 0)) +\n    scale_y_continuous(\n        breaks = seq(0, 800, 200),\n        limits = c(0, 800),\n        expand = c(0, 0)) +\n    # Add the pilot theme, setting a bottom axis and horizontal gridlines\n    theme_pilot(\n        axes = \"b\",\n        grid = \"h\") +\n    # Use scale_color_manual and pilot_color to set colors for each lines\n    scale_color_manual(values = c(\n        \"Immigration\" = pilot_color(\"navy\"),\n        \"Net migration\" = pilot_color(\"blue\"))) +\n    # Here we use a theme customisation to overlay the legend on the plot area:\n    # We could have used legend_position = \"top-right\" in theme_pilot\n    # to put the legend at the top-right above the plot area\n    theme(\n        legend.position = c(1.03, 0.99),\n        legend.justification = c(1, 1),\n        legend.direction = \"horizontal\",\n        legend.text = element_text(margin = margin(r = 10)))\n\n# After creating the plot, add a title and subtitle with add_pilot_titles\nplot \u003c- add_pilot_titles(\n    plot,\n    title = \"Net migration fell after the EU referendum\",\n    subtitle = \"International migration in the year ending each quarter\")\n\n# Save the plot in different formats ------------------------------------------\n\n# Save a high resolution export of the plot as a png\nggsave(\n    filename = \"line-chart.png\",\n    plot = plot,\n    width = 7.7,\n    height = 5.8,\n    dpi = 400)\n\n# Save an editable verson of the plot as an svg\nggsave(\n    filename = \"line-chart.svg\",\n    plot = plot,\n    width = 7.7,\n    height = 5.8,\n    dpi = 400)\n```\n\n### Area chart\n\n\u003cimg src=\"gallery/examples/area-chart-annotations/area-chart-annotations.png\" width=\"800\" /\u003e\n\n```r\n# Imports ---------------------------------------------------------------------\n\nlibrary(tidyverse)\nlibrary(scales)\nlibrary(pilot)\n\n# Read in and prepare the data ------------------------------------------------\n\n# Load the data from the csv as a dataframe and pivot it into a tidy format\ndf \u003c- read_csv(\"area-chart-annotations.csv\") %\u003e%\n    pivot_longer(\n        cols = -date,\n        names_to = \"energy_source\",\n        values_to = \"gwh\")\n\n# Turn the energy_source column into a factor: setting the order of the levels\n# controls the order of the categories from top to bottom\ndf$energy_source \u003c- factor(\n    df$energy_source,\n    levels = c(\"other\", \"renewables\"))\n\n# Create the plot -------------------------------------------------------------\n\n# Use ggplot to create a plot with data and mappings\nplot \u003c- ggplot(\n        data = df,\n        mapping = aes(x = date, y = gwh, fill = energy_source)) +\n    # Add an area geometry to fill areas based on the data\n    geom_area() +\n    # Set labels for the axes, but don't set titles here\n    labs(\n        x = NULL,\n        y = NULL,\n        caption = \"Source: BEIS, Digest of UK Energy Statistics, Table 5.3\") +\n    # Configure the the x and y axes: we set the y axis breaks and limits, and\n    # we turn off the expansion on both axes\n    scale_x_date(\n        expand = c(0, 0)) +\n    scale_y_continuous(\n        label = comma,\n        limits = c(0, 402000),\n        breaks = seq(0, 400000, 100000),\n        expand = c(0, 0)) +\n    # Use annotate_pilot to add annotations to a plot: this function does\n    # the same thing as annotate but it automatically sets the fonts to match\n    # the theme style; position each annotation using values on the axis scales\n    annotate_pilot(\n        x = as.Date(\"2013-10-07\"),\n        y = 200000,\n        label = \"Non-renewable\",\n        color = \"#ffffff\",\n        hjust = 0) +\n    annotate_pilot(\n        x = as.Date(\"2015-04-01\"),\n        y = 40000,\n        label = \"Renewable\",\n        color = \"#202020\",\n        hjust = 0) +\n    # Add the pilot theme, turning on the bottom and left axes, and turning off\n    # the gridlines and legend\n    theme_pilot(\n        axes = \"bl\",\n        grid = \"\",\n        legend_position = \"none\") +\n    # Use scale_fill_manual and pilot_color to set category colors\n    scale_fill_manual(values = c(\n        \"renewables\" = pilot_color(\"green\"),\n        \"other\" = pilot_color(\"navy\")))\n\n# After creating the plot, add a title and subtitle with add_pilot_titles\n    plot \u003c- add_pilot_titles(\n        plot,\n        title = \"Renewables are growing as a share of electricity generation\",\n        subtitle = \"Electricity generation by fuel type in the United Kingdom from 1996 to 2020, GWh\")\n\n# Save the plot in different formats ------------------------------------------\n\n# Save a high resolution export of the plot as a png\nggsave(\n    filename = \"area-chart-annotations.png\",\n    plot = plot,\n    width = 7.7,\n    height = 5.8,\n    dpi = 400)\n\n# Save an editable verson of the plot as an svg\nggsave(\n    filename = \"area-chart-annotations.svg\",\n    plot = plot,\n    width = 7.7,\n    height = 5.8,\n    dpi = 400)\n```\n\n### Stacked column chart\n\n\u003cimg src=\"gallery/examples/stacked-column-chart/stacked-column-chart.png\" width=\"800\" /\u003e\n\n```r\n# Imports ---------------------------------------------------------------------\n\nlibrary(tidyverse)\nlibrary(pilot)\n\n# Read in and prepare the data ------------------------------------------------\n\n# Load the data from the csv as a dataframe\ndf \u003c- read_csv(\"stacked-column-chart.csv\")\n\n# Convert the year to character data: we don't want to treat this as a date or\n# a number in this case, it is just a label for each bar\ndf$year \u003c- as.character(df$year)\n\n# Turn the nationality column into a factor: setting the order of the levels\n# controls the order of the categories in each bar from top to bottom\ndf$nationality \u003c- factor(df$nationality, levels = c(\"Non-EU\", \"EU\", \"British\"))\n\n# Create the plot -------------------------------------------------------------\n\n# Use ggplot to create a plot with data and mappings\nplot \u003c- ggplot(\n        data = df,\n        mapping = aes(\n            x = year,\n            y = estimate,\n            fill = nationality)) +\n    # Add a col geometry for columns\n    geom_col(width = 0.8) +\n    # Set labels for the axes, legend, and caption, but don't set titles here\n    labs(\n        x = NULL,\n        y = NULL,\n        fill = NULL,\n        caption = \"Source: ONS, Provisional LTIM estimates\") +\n    # Configure the the x and y axes: set the y axis breaks and limits, and\n    # turn off the y-axis expansion\n    scale_x_discrete() +\n    scale_y_continuous(\n        limits = c(0, 700),\n        breaks = seq(0, 700, 100),\n        expand = c(0,0)) +\n    # Add the pilot theme: set the grid to horizontal, the legend to top-left,\n    # and the caption to left\n    theme_pilot(\n        grid = \"h\",\n        legend_position = \"top-left\",\n        caption_position = \"left\") +\n    # Use scale_fill_manual and pilot_color to set category colors\n    scale_fill_manual(values = c(\n        \"British\" = pilot_color(\"yellow\"),\n        \"EU\" = pilot_color(\"navy\"),\n        \"Non-EU\" = pilot_color(\"blue\")))\n\n# After creating the plot, add a title and subtitle with add_pilot_titles\nplot \u003c- add_pilot_titles(\n    plot,\n    title = \"Immigration is stable but the composition has changed\",\n    subtitle = \"Immigration by nationality in each year ending September (000s)\")\n\n# Save the plot in different formats ------------------------------------------\n\n# Save a high resolution export of the plot as a png\nggsave(\n    filename = \"stacked-column-chart.png\",\n    plot = plot,\n    width = 7.7,\n    height = 5.8,\n    dpi = 400)\n\n# Save an editable verson of the plot as an svg\nggsave(\n    filename = \"stacked-column-chart.svg\",\n    plot = plot,\n    width = 7.7,\n    height = 5.8,\n    dpi = 400)\n```\n\n### Small multiple scatterplot\n\n\u003cimg src=\"gallery/examples/scatter-chart-facets/scatter-chart-facets.png\" width=\"800\" /\u003e\n\n```r\n# Imports ---------------------------------------------------------------------\n\nlibrary(tidyverse)\nlibrary(scales)\nlibrary(pilot)\n\n# Read in and prepare the data ------------------------------------------------\n\n# Load the data from the csv as a dataframe and filter for GB constituencies\ndf \u003c- read_csv(\"scatter-chart-facets.csv\") %\u003e%\n    filter(! is.na(classification))\n\n# Turn the classification column into a factor: setting the order of the levels\n# controls the order of the categories in the legend from top to bottom\nsettlement_classes \u003c- c(\n    \"London\",\n    \"Other city\",\n    \"Large town\",\n    \"Medium town\",\n    \"Small town\",\n    \"Village\")\n\ndf$classification \u003c- factor(df$classification, levels = settlement_classes)\n\n# Create the plot -------------------------------------------------------------\n\n# Use ggplot to create a plot with data and mappings\nplot \u003c- ggplot(\n        data = df,\n        mapping = aes(\n            x = median_age,\n            y = turnout,\n            color = classification)) +\n    # Add a point geometry to add points: set shape = 16 to match house style\n    geom_point(\n        shape = 16,\n        size = 2,\n        alpha = 0.6) +\n    # Use facet_wrap to set the variable to facet with\n    facet_wrap(~ classification) +\n    # Set labels for the axes, colors and caption: DON'T set titles here\n    labs(\n        x = \"Median age\",\n        y = \"Turnout\",\n        color = \"Settlement class\") +\n    # Configure the the x and y axes: set the x axis limits; set the y axis\n    # limits and the y axis labels to show percentages to the nearest percent,\n    # turn off the expansion on both axes\n    scale_x_continuous(\n        expand = c(0, 0),\n        limits = c(25, 55),\n        breaks = seq(25, 55, 10)) +\n    scale_y_continuous(\n        expand = c(0, 0),\n        limits = c(0.5, 0.8),\n        label = percent_format(accuracy = 1)) +\n    # Add the pilot theme: turn off the axes, set the gridlines to\n    # both horizontal and vertical, and turn off the legend\n    theme_pilot(\n        axes = \"\",\n        grid = \"hv\",\n        legend_position = \"none\") +\n    # Use scale_color_manual and pilot_color to set category colors\n    scale_color_manual(values = c(\n        \"London\" = pilot_color(\"navy\"),\n        \"Other city\" = pilot_color(\"blue\"),\n        \"Large town\" = pilot_color(\"brown\"),\n        \"Medium town\" = pilot_color(\"green\"),\n        \"Small town\" = pilot_color(\"orange\"),\n        \"Village\" = pilot_color(\"purple\")))\n\n# After creating the plot, add a title and subtitle with add_pilot_titles\nplot \u003c- add_pilot_titles(\n    plot,\n    title = \"Turnout was higher in older, less urban constituencies\",\n    subtitle = \"Constituencies by age, turnout and settlement class, 2017\")\n\n# Save the plot in different formats ------------------------------------------\n\n# Save a high resolution export of the plot as a png\nggsave(\n    filename = \"scatter-chart-facets.png\",\n    plot = plot,\n    width = 7.7,\n    height = 6.3,\n    dpi = 400)\n\n# Save an editable verson of the plot as an svg\nggsave(\n    filename = \"scatter-chart-facets.svg\",\n    plot = plot,\n    width = 7.7,\n    height = 6.3,\n    dpi = 400)\n```\n\n### Regression scatterplot\n\n\u003cimg src=\"gallery/examples/scatter-chart-regression/scatter-chart-regression.png\" width=\"800\" /\u003e\n\n```r\n# Imports ---------------------------------------------------------------------\n\nlibrary(tidyverse)\nlibrary(pilot)\n\n# Read in and prepare the data ------------------------------------------------\n\n# Load the data from the csvs as dataframes\ndf_data \u003c- read_csv(\"scatter-chart-regression-data.csv\")\ndf_posterior \u003c- read_csv(\"scatter-chart-regression-posterior.csv\")\n\n# Create the plot -------------------------------------------------------------\n\n# Use ggplot to create a plot with data and mappings for the posterior\nplot \u003c- ggplot(\n        data = df_posterior,\n        mapping = aes(x = weight)) +\n    # Add a ribbon geometry for the posterior prediction intervals\n    geom_ribbon(\n        mapping = aes(\n            ymin = lower_prediction,\n            ymax = upper_prediction),\n        fill = pilot_color(\"orange\"),\n        alpha = 0.5)  +\n    # Add a ribbon geometry for the posterior slope parameter intervals\n    geom_ribbon(\n        mapping = aes(\n            ymin = lower_parameter,\n            ymax = upper_parameter),\n        fill = pilot_color(\"brown\"),\n        alpha = 0.5)  +\n    # Add a line geometry for the posterior slope parameter central estimate\n    geom_line(\n        mapping = aes(y = height),\n        color = pilot_color(\"brown\")) +\n    # Add a point geometry for the regression data\n    geom_point(\n        data = df_data,\n        mapping = aes(\n            x = weight,\n            y = height),\n        shape = 16,\n        size = 2,\n        color = \"#404040\",\n        alpha = 0.6) +\n    # Set labels for the axes and caption, but don't set titles here\n    labs(\n        x = \"Weight\",\n        y = \"Height\",\n        caption = \"Source: Richard McElreath, Statistical Rethinking, Figure 4.10\") +\n    # Configure the the axes: set the axis limits and turn off the expansion\n    scale_x_continuous(\n        expand = c(0, 0)) +\n    scale_y_continuous(\n        expand = c(0, 0),\n        limits = c(120, 190)) +\n    # Add the pilot theme: set the axes to bottom and left, the gridlines to\n    # horizontal and vertical, and the caption to left\n    theme_pilot(\n        axes = \"bl\",\n        grid = \"hv\",\n        caption_position = \"left\")\n\n# After creating the plot, add a title and subtitle with add_pilot_titles\nplot \u003c- add_pilot_titles(\n    plot,\n    title = \"Height increases as a function of weight\",\n    subtitle = \"Fitted regression line, slope interval, and 89% prediction interval\")\n\n# Save the plot in different formats ------------------------------------------\n\n# Save a high resolution export of the plot as a png\nggsave(\n    filename = \"scatter-chart-regression.png\",\n    plot = plot,\n    width = 7.7,\n    height = 6.4,\n    dpi = 400)\n\n# Save an editable verson of the plot as an svg\nggsave(\n    filename = \"scatter-chart-regression.svg\",\n    plot = plot,\n    width = 6.4,\n    height = 6.4,\n    dpi = 400)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Folihawkins%2Fpilot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Folihawkins%2Fpilot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Folihawkins%2Fpilot/lists"}