Skip to content

Commit

Permalink
Merge update to barchart examples in cookbook
Browse files Browse the repository at this point in the history
Merge branch 'update-bar-chart-examples' into development

# Conflicts:
#	vignettes/cookbook/_customisations.Rmd
  • Loading branch information
Olivia-Box-Power committed Jan 7, 2025
2 parents d1f8160 + 0cbb404 commit 95d5bca
Show file tree
Hide file tree
Showing 11 changed files with 1,797 additions and 1,127 deletions.
76 changes: 56 additions & 20 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ knitr::opts_chunk$set(
ex1_alt <- paste("A bar chart with grey background, white grid lines",
"and dark grey bars.")
ex2_alt <- paste("A bar chart with white background,",
"light grey horizontal grid lines dark blue bars.")
"light grey horizontal grid lines, and dark blue bars.")
ex3_alt <- paste("A line chart with white background,
light grey horizontal grid lines, an orange line, and a dark blue
line.")
```

# afcharts <img src="man/figures/logo.svg" alt="afcharts logo" align="right" height="150"/>
Expand Down Expand Up @@ -79,49 +81,83 @@ Help files for each function in the package can be found on the [References](htt

The easiest way to use afcharts is by adding `use_afcharts()` to the beginning of your R script, Rmarkdown document or Shiny app code. This function will set a number of defaults to ggplot2 geoms, use afcharts colour palettes and use `theme_af()`.

#### Example 1: Plot with one colour using ggplot2 defaults
#### Example 1: Bar chart with one colour using ggplot2 defaults

```{r ex1, fig.alt = ex1_alt}
```{r ex1, fig.alt = ex1_alt, fig.width = 10}
library(ggplot2)
library(dplyr)
library(gapminder)
library(afcharts)
gapminder |>
filter(year == 2007 & continent == "Europe") |>
slice_max(order_by = lifeExp, n = 5) |>
filter(year == 2007 & continent == "Americas") |>
slice_max(order_by = pop, n = 5) |>
ggplot() +
geom_col(aes(x = reorder(country, -lifeExp), y = lifeExp)) +
scale_y_continuous(expand = c(0, 0)) +
geom_col(aes(x = reorder(country, -pop), y = pop)) +
scale_y_continuous(
labels = scales::label_number(scale = 1E-6),
limits = c(0, 350E6),
expand = expansion(mult = c(0, 0.1))
) +
scale_fill_discrete_af("focus", reverse = TRUE) +
labs(
x = NULL,
y = NULL,
title = "Iceland has the highest life expectancy in Europe",
subtitle = "Life expectancy in European countries, 2007",
title = "The U.S.A. is the most populous country in\nthe Americas",
subtitle = "Population of countries in the Americas (millions), 2007",
caption = "Source: Gapminder"
)
)
```

#### Example 2: Plot with one colour using afcharts defaults
#### Example 2: Bar chart of one colour using afcharts defaults

```{r ex2, fig.alt = ex2_alt}
```{r ex2, fig.alt = ex2_alt, fig.width = 10}
afcharts::use_afcharts()
gapminder |>
filter(year == 2007 & continent == "Europe") |>
slice_max(order_by = lifeExp, n = 5) |>
ggplot() +
geom_col(aes(x = reorder(country, -lifeExp), y = lifeExp)) +
scale_y_continuous(expand = c(0, 0)) +
filter(year == 2007 & continent == "Americas") |>
slice_max(order_by = pop, n = 5) |>
ggplot(aes(x = reorder(country, -pop), y = pop)) +
geom_col(fill = af_colour_values["dark-blue"]) +
scale_y_continuous(
labels = scales::label_number(scale = 1E-6),
limits = c(0, 350E6),
expand = c(0, 0),expansion(mult = c(0, 0.1))
) +
labs(
x = NULL,
y = NULL,
title = "Iceland has the highest life expectancy in Europe",
subtitle = "Life expectancy in European countries, 2007",
title = "The U.S.A. is the most populous country in\nthe Americas",
subtitle = "Population of countries in the Americas (millions), 2007",
caption = "Source: Gapminder"
)
```

#### Example 3: Multiple colour line chart with afcharts formatting

```{r ex3, fig.alt = ex3_alt, fig.width = 10}
afcharts::use_afcharts()
gapminder |>
filter(country %in% c("United Kingdom", "China")) |>
ggplot(aes(x = year, y = lifeExp, colour = country)) +
geom_line(linewidth = 1) +
scale_y_continuous(
breaks = seq(0, 80, 20),
limits = c(0, 82),
expand = expansion(mult = c(0, 0.1))
) +
scale_x_continuous(breaks = seq(1952, 2007, 5)) +
labs(
x = "Year",
y = NULL,
title = "Living Longer",
subtitle = "Life Expectancy in the United Kingdom and China 1952-2007",
caption = "Source: Gapminder",
colour = NULL
)
```

**Note on use of titles, subtitles and captions** <br> Titles, subtitles and captions have been embedded in these example charts for demonstration purposes. However, for accessibility reasons, it is usually preferable to provide titles in the body of the page rather than embedded within the image of the plot. More information is available in the [accessibility article](https://best-practice-and-impact.github.io/afcharts/articles/accessibility.html#other-accessibility-considerations).

## Acknowledgments
Expand Down
73 changes: 56 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ beginning of your R script, Rmarkdown document or Shiny app code. This
function will set a number of defaults to ggplot2 geoms, use afcharts
colour palettes and use `theme_af()`.

#### Example 1: Plot with one colour using ggplot2 defaults
#### Example 1: Bar chart with one colour using ggplot2 defaults

``` r
library(ggplot2)
Expand All @@ -82,43 +82,82 @@ library(gapminder)
library(afcharts)

gapminder |>
filter(year == 2007 & continent == "Europe") |>
slice_max(order_by = lifeExp, n = 5) |>
filter(year == 2007 & continent == "Americas") |>
slice_max(order_by = pop, n = 5) |>
ggplot() +
geom_col(aes(x = reorder(country, -lifeExp), y = lifeExp)) +
scale_y_continuous(expand = c(0, 0)) +
geom_col(aes(x = reorder(country, -pop), y = pop)) +
scale_y_continuous(
labels = scales::label_number(scale = 1E-6),
limits = c(0, 350E6),
expand = expansion(mult = c(0, 0.1))
) +
scale_fill_discrete_af("focus", reverse = TRUE) +
labs(
x = NULL,
y = NULL,
title = "Iceland has the highest life expectancy in Europe",
subtitle = "Life expectancy in European countries, 2007",
title = "The U.S.A. is the most populous country in\nthe Americas",
subtitle = "Population of countries in the Americas (millions), 2007",
caption = "Source: Gapminder"
)
)
```

<img src="man/figures/README-ex1-1.svg" alt="A bar chart with grey background, white grid lines and dark grey bars." />

#### Example 2: Plot with one colour using afcharts defaults
#### Example 2: Bar chart of one colour using afcharts defaults

``` r
afcharts::use_afcharts()

gapminder |>
filter(year == 2007 & continent == "Europe") |>
slice_max(order_by = lifeExp, n = 5) |>
ggplot() +
geom_col(aes(x = reorder(country, -lifeExp), y = lifeExp)) +
scale_y_continuous(expand = c(0, 0)) +
filter(year == 2007 & continent == "Americas") |>
slice_max(order_by = pop, n = 5) |>
ggplot(aes(x = reorder(country, -pop), y = pop)) +
geom_col(fill = af_colour_values["dark-blue"]) +
scale_y_continuous(
labels = scales::label_number(scale = 1E-6),
limits = c(0, 350E6),
expand = c(0, 0),expansion(mult = c(0, 0.1))
) +
labs(
x = NULL,
y = NULL,
title = "Iceland has the highest life expectancy in Europe",
subtitle = "Life expectancy in European countries, 2007",
title = "The U.S.A. is the most populous country in\nthe Americas",
subtitle = "Population of countries in the Americas (millions), 2007",
caption = "Source: Gapminder"
)
```

<img src="man/figures/README-ex2-1.svg" alt="A bar chart with white background, light grey horizontal grid lines dark blue bars." />
<img src="man/figures/README-ex2-1.svg" alt="A bar chart with white background, light grey horizontal grid lines, and dark blue bars." />

#### Example 3: Multiple colour line chart with afcharts formatting

``` r
afcharts::use_afcharts()
#> NULL

gapminder |>
filter(country %in% c("United Kingdom", "China")) |>
ggplot(aes(x = year, y = lifeExp, colour = country)) +
geom_line(linewidth = 1) +
scale_y_continuous(
breaks = seq(0, 80, 20),
limits = c(0, 82),
expand = expansion(mult = c(0, 0.1))
) +
scale_x_continuous(breaks = seq(1952, 2007, 5)) +
labs(
x = "Year",
y = NULL,
title = "Living Longer",
subtitle = "Life Expectancy in the United Kingdom and China 1952-2007",
caption = "Source: Gapminder",
colour = NULL
)
```

<img src="man/figures/README-ex3-1.svg" alt="A line chart with white background,
light grey horizontal grid lines, an orange line, and a dark blue
line." />

**Note on use of titles, subtitles and captions** <br> Titles, subtitles
and captions have been embedded in these example charts for
Expand Down
Loading

0 comments on commit 95d5bca

Please sign in to comment.