From e0cd7e53df3fc83549182052ce35d3a61dd794da Mon Sep 17 00:00:00 2001 From: Rosetta <135234996+gisellerosetta@users.noreply.github.com> Date: Tue, 26 Nov 2024 15:58:28 +0000 Subject: [PATCH 01/17] updated bar chart examples --- README.md | 61 ++- man/figures/README-ex1-1.svg | 598 +++---------------------- man/figures/README-ex2-1.svg | 490 +++----------------- man/figures/README-ex3-1.svg | 67 +++ vignettes/cookbook/_annotations.Rmd | 2 +- vignettes/cookbook/_chart-types.Rmd | 45 +- vignettes/cookbook/_customisations.Rmd | 6 +- 7 files changed, 272 insertions(+), 997 deletions(-) create mode 100644 man/figures/README-ex3-1.svg diff --git a/README.md b/README.md index 94130f6..53ea035 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ +--- +editor_options: + markdown: + wrap: 72 +--- @@ -10,6 +15,7 @@ date)](https://img.shields.io/github/v/release/best-practice-and-impact/afcharts [![R build status](https://github.com/best-practice-and-impact/afcharts/workflows/R-CMD-check/badge.svg)](https://github.com/best-practice-and-impact/afcharts/actions) [![R-CMD-check](https://github.com/best-practice-and-impact/afcharts/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/best-practice-and-impact/afcharts/actions/workflows/R-CMD-check.yaml) + afcharts is an R package for creating accessible plots by the Government @@ -84,44 +90,67 @@ library(gapminder) library(afcharts) gapminder |> - filter(year == 2007 & continent == "Europe") |> - slice_max(order_by = lifeExp, n = 5) |> - ggplot() + - geom_col(aes(x = reorder(country, -lifeExp), y = lifeExp)) + + filter(year == 2007 & continent == "Americas") |> + slice_max(order_by = pop, n = 5) |> + ggplot(aes(x = reorder(country, -pop), y = pop/(10^6))) + + geom_col() + scale_y_continuous(expand = c(0, 0)) + 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 the Americas", + subtitle = "Population of countries in the Americas (millions), 2007", caption = "Source: Gapminder" - ) + ) ``` -A bar chart with grey background, white grid lines and dark grey bars. +A bar chart with grey background, white grid lines and dark grey bars. #### Example 2: Plot with one colour using afcharts defaults ``` r afcharts::use_afcharts() #> NULL - gapminder |> - filter(year == 2007 & continent == "Europe") |> - slice_max(order_by = lifeExp, n = 5) |> - ggplot() + - geom_col(aes(x = reorder(country, -lifeExp), y = lifeExp)) + + filter(year == 2007 & continent == "Americas") |> + slice_max(order_by = pop, n = 5) |> + ggplot(aes(x = reorder(country, -pop), y = pop/(10^6))) + + geom_col(fill = af_colour_values["dark-blue"]) + + scale_y_continuous(expand = c(0, 0)) + + labs( + x = NULL, + y = NULL, + 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" + ) +``` + +A bar chart with white background, light grey horizontal grid lines dark blue bars. + +#### Example 3: Plot with focus colours using afcharts + +``` r +pop_bar_data |> + ggplot( + aes(x = reorder(country, -pop), y = pop/(10^6), + fill = country == "Brazil") + ) + + geom_col() + + theme_af(legend = "none") + scale_y_continuous(expand = c(0, 0)) + + 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 = "Brazil has the second highest population in\nthe Americas", + subtitle = "Population of countries in the Americas (millions), 2007", caption = "Source: Gapminder" ) + ) ``` -A bar chart with white background, light grey horizontal grid lines dark blue bars. +A bar chart with the bar for Brazil highlighted in dark blue and other bars in grey. **Note on use of titles, subtitles and captions**
Titles, subtitles and captions have been embedded in these example charts for diff --git a/man/figures/README-ex1-1.svg b/man/figures/README-ex1-1.svg index c0cc16a..1a7a840 100644 --- a/man/figures/README-ex1-1.svg +++ b/man/figures/README-ex1-1.svg @@ -1,534 +1,74 @@ - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0 +100 +200 +300 + + + + + + + + + +United States +Brazil +Mexico +Colombia +Argentina +Population of countries in the Americas (millions), 2007 +The U.S.A. is the most populous country in the Americas +Source: Gapminder diff --git a/man/figures/README-ex2-1.svg b/man/figures/README-ex2-1.svg index 8a98779..3f2392c 100644 --- a/man/figures/README-ex2-1.svg +++ b/man/figures/README-ex2-1.svg @@ -1,433 +1,67 @@ - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + +0 +100 +200 +300 + + + + + + + + + + +United States +Brazil +Mexico +Colombia +Argentina +Population of countries in the Americas (millions), 2007 +The U.S.A. is the most populous country in +the Americas +Source: Gapminder diff --git a/man/figures/README-ex3-1.svg b/man/figures/README-ex3-1.svg new file mode 100644 index 0000000..2a09f0e --- /dev/null +++ b/man/figures/README-ex3-1.svg @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0 +100 +200 +300 + + + + + + + + + + +United States +Brazil +Mexico +Colombia +Argentina +Population of countries in the Americas (millions), 2007 +Brazil has the second highest population in +the Americas +Source: Gapminder + + diff --git a/vignettes/cookbook/_annotations.Rmd b/vignettes/cookbook/_annotations.Rmd index c8a0474..1804096 100644 --- a/vignettes/cookbook/_annotations.Rmd +++ b/vignettes/cookbook/_annotations.Rmd @@ -85,7 +85,7 @@ Annotations may also be used to add value labels to a bar chart. Note that `geom ```{r annotations-3} #| fig.alt = "A bar chart with white text labels at the end of each bar." -ggplot(bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) + +ggplot(life_exp_bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) + geom_col(fill = af_colour_values["dark-blue"]) + geom_text(aes(label = round(lifeExp, 1)), nudge_y = -5, colour = "white") + diff --git a/vignettes/cookbook/_chart-types.Rmd b/vignettes/cookbook/_chart-types.Rmd index af07453..8635b8f 100644 --- a/vignettes/cookbook/_chart-types.Rmd +++ b/vignettes/cookbook/_chart-types.Rmd @@ -7,7 +7,6 @@ gapminder |> filter(country == "United Kingdom") |> - ggplot(aes(x = year, y = lifeExp)) + geom_line(linewidth = 1, colour = af_colour_values["dark-blue"]) + theme_af() + scale_y_continuous(limits = c(0, 82), @@ -54,26 +53,25 @@ An example with line labels and no legend can be found in the [Adding annotation ## Bar charts ```{r bar-data} -bar_data <- - gapminder |> - filter(year == 2007 & continent == "Europe") |> - slice_max(order_by = lifeExp, n = 5) +pop_bar_data <- gapminder |> + filter(year == 2007 & continent == "Americas") |> + slice_max(order_by = pop, n = 5) ``` ```{r bar-chart-1} #| fig.alt = "A bar chart using afcharts theme and dark blue colour." -ggplot(bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) + +ggplot(pop_bar_data, aes(x = reorder(country, -pop), y = pop/(10^6))) + geom_col(fill = af_colour_values["dark-blue"]) + theme_af() + scale_y_continuous(expand = c(0, 0)) + 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" - ) + ) ``` A bar chart can sometimes look better with horizontal bars. This can also be a good option if your bar labels are long and difficult to display horizontally on the x axis. To produce a horizontal bar chart, swap the variables defined for x and y in `aes()` and make a few tweaks to `theme_af()`; draw grid lines for the x axis only by setting the `grid` argument, and draw an axis line for the y axis only by setting the `axis` argument. @@ -81,17 +79,17 @@ A bar chart can sometimes look better with horizontal bars. This can also be a g ```{r bar-chart-2} #| fig.alt = "A horizontal bar chart using afcharts theme and dark blue colour." -ggplot(bar_data, aes(x = lifeExp, y = reorder(country, lifeExp))) + +ggplot(pop_bar_data, aes(x = pop/(10^6), y = reorder(country, -pop))) + geom_col(fill = af_colour_values["dark-blue"]) + theme_af(grid = "x", axis = "y") + scale_x_continuous(expand = c(0, 0)) + 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" - ) + ) ``` @@ -265,12 +263,12 @@ stacked_bar_data |> ## Focus charts ```{r focus-chart} -#| fig.alt = "A bar chart with the bar for Sweden highlighted in dark blue and other bars in grey." +#| fig.alt = "A bar chart with the bar for Brazil highlighted in dark blue and other bars in grey." -bar_data |> +pop_bar_data |> ggplot( - aes(x = reorder(country, -lifeExp), y = lifeExp, - fill = country == "Sweden") + aes(x = reorder(country, -pop), y = pop/(10^6), + fill = country == "Brazil") ) + geom_col() + theme_af(legend = "none") + @@ -279,8 +277,8 @@ bar_data |> labs( x = NULL, y = NULL, - title = "Sweden has the fourth highest life expectancy in Europe", - subtitle = "Life expectancy in European countries, 2007", + title = "Brazil has the second highest population in\nthe Americas", + subtitle = "Population of countries in the Americas (millions), 2007", caption = "Source: Gapminder" ) ``` @@ -294,11 +292,18 @@ To make a `ggplot2` chart interactive, use `ggplotly()` from the `plotly` packag * Subtitles and captions are not supported in `ggplotly()`. As stated elsewhere in this guidance, titles and subtitles should ideally be included in the body of text surrounding a chart rather than embedded in the chart itself, and so this is hopefully not a big issue. This example therefore has no title, subtitle or caption. +```{r life-exp-bar-data} +life_exp_bar_data <- + gapminder |> + filter(year == 2007 & continent == "Europe") |> + slice_max(order_by = lifeExp, n = 5) +``` + ```{r interactive-charts} #| fig.alt = "An interactive bar chart using afcharts theme and dark blue colour. A tooltip appears when hovering over each bar." p <- - bar_data |> + life_exp_bar_data |> # Format text for tooltips mutate(tooltip = paste0( "Country: ", country, "\n", diff --git a/vignettes/cookbook/_customisations.Rmd b/vignettes/cookbook/_customisations.Rmd index 88feda1..3d7dd9e 100644 --- a/vignettes/cookbook/_customisations.Rmd +++ b/vignettes/cookbook/_customisations.Rmd @@ -9,7 +9,7 @@ To control the order of bars in a chart, wrap the variable you want to arrange w ```{r sorting} #| fig.alt = "A bar chart using afcharts theme and dark blue colour with bars sorted in decreasing order by life expectancy." -bar_data |> +life_exp_bar_data |> ggplot(aes(x = lifeExp, y = reorder(country, lifeExp))) + geom_col(fill = af_colour_values["dark-blue"]) + theme_af(axis = "y", grid = "x") @@ -156,7 +156,7 @@ If text is too long, it may be cut off or distort the dimensions of the chart. #| fig.alt = "A bar chart with end of main title text not visible and long y axis title skewing the dimensions of the chart." plot <- - ggplot(bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) + + ggplot(life_exp_bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) + geom_col(fill = af_colour_values["dark-blue"]) + theme_af() + scale_y_continuous(expand = c(0, 0)) + @@ -198,7 +198,7 @@ If you find you need to adjust theme elements for your chart, this can be done u ```{r adjust-theme} #| fig.alt = "A bar chart using afcharts theme and dark blue colour, with axis lines and ticks coloured black." -ggplot(bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) + +ggplot(life_exp_bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) + geom_col(fill = af_colour_values["dark-blue"]) + theme_af(axis = "xy") + theme(axis.line = element_line(colour = "black"), From 8d0d90c12bdc05690d27f89cb037304e677400f8 Mon Sep 17 00:00:00 2001 From: gisellerosetta Date: Tue, 26 Nov 2024 16:16:29 +0000 Subject: [PATCH 02/17] Re-build README.md --- README.md | 61 +--- man/figures/README-ex1-1.svg | 598 +++++++++++++++++++++++++++++++---- man/figures/README-ex2-1.svg | 490 ++++++++++++++++++++++++---- 3 files changed, 973 insertions(+), 176 deletions(-) diff --git a/README.md b/README.md index 53ea035..94130f6 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,3 @@ ---- -editor_options: - markdown: - wrap: 72 ---- @@ -15,7 +10,6 @@ date)](https://img.shields.io/github/v/release/best-practice-and-impact/afcharts [![R build status](https://github.com/best-practice-and-impact/afcharts/workflows/R-CMD-check/badge.svg)](https://github.com/best-practice-and-impact/afcharts/actions) [![R-CMD-check](https://github.com/best-practice-and-impact/afcharts/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/best-practice-and-impact/afcharts/actions/workflows/R-CMD-check.yaml) - afcharts is an R package for creating accessible plots by the Government @@ -90,67 +84,44 @@ library(gapminder) library(afcharts) gapminder |> - filter(year == 2007 & continent == "Americas") |> - slice_max(order_by = pop, n = 5) |> - ggplot(aes(x = reorder(country, -pop), y = pop/(10^6))) + - geom_col() + + 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)) + labs( x = NULL, y = NULL, - title = "The U.S.A. is the most populous country in the Americas", - subtitle = "Population of countries in the Americas (millions), 2007", + title = "Iceland has the highest life expectancy in Europe", + subtitle = "Life expectancy in European countries, 2007", caption = "Source: Gapminder" - ) + ) ``` -A bar chart with grey background, white grid lines and dark grey bars. +A bar chart with grey background, white grid lines and dark grey bars. #### Example 2: Plot with one colour using afcharts defaults ``` r afcharts::use_afcharts() #> NULL -gapminder |> - filter(year == 2007 & continent == "Americas") |> - slice_max(order_by = pop, n = 5) |> - ggplot(aes(x = reorder(country, -pop), y = pop/(10^6))) + - geom_col(fill = af_colour_values["dark-blue"]) + - scale_y_continuous(expand = c(0, 0)) + - labs( - x = NULL, - y = NULL, - 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" - ) -``` - -A bar chart with white background, light grey horizontal grid lines dark blue bars. - -#### Example 3: Plot with focus colours using afcharts -``` r -pop_bar_data |> - ggplot( - aes(x = reorder(country, -pop), y = pop/(10^6), - fill = country == "Brazil") - ) + - geom_col() + - theme_af(legend = "none") + +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)) + - scale_fill_discrete_af("focus", reverse = TRUE) + labs( x = NULL, y = NULL, - title = "Brazil has the second highest population in\nthe Americas", - subtitle = "Population of countries in the Americas (millions), 2007", + title = "Iceland has the highest life expectancy in Europe", + subtitle = "Life expectancy in European countries, 2007", caption = "Source: Gapminder" ) - ) ``` -A bar chart with the bar for Brazil highlighted in dark blue and other bars in grey. +A bar chart with white background, light grey horizontal grid lines dark blue bars. **Note on use of titles, subtitles and captions**
Titles, subtitles and captions have been embedded in these example charts for diff --git a/man/figures/README-ex1-1.svg b/man/figures/README-ex1-1.svg index 1a7a840..c0cc16a 100644 --- a/man/figures/README-ex1-1.svg +++ b/man/figures/README-ex1-1.svg @@ -1,74 +1,534 @@ - - + + - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - -0 -100 -200 -300 - - - - - - - - - -United States -Brazil -Mexico -Colombia -Argentina -Population of countries in the Americas (millions), 2007 -The U.S.A. is the most populous country in the Americas -Source: Gapminder + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/man/figures/README-ex2-1.svg b/man/figures/README-ex2-1.svg index 3f2392c..8a98779 100644 --- a/man/figures/README-ex2-1.svg +++ b/man/figures/README-ex2-1.svg @@ -1,67 +1,433 @@ - - + + - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - -0 -100 -200 -300 - - - - - - - - - - -United States -Brazil -Mexico -Colombia -Argentina -Population of countries in the Americas (millions), 2007 -The U.S.A. is the most populous country in -the Americas -Source: Gapminder + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From de4a804bd1cdfbe37e1bae99066baaeceb5ee6be Mon Sep 17 00:00:00 2001 From: Rosetta <135234996+gisellerosetta@users.noreply.github.com> Date: Wed, 27 Nov 2024 09:20:29 +0000 Subject: [PATCH 03/17] fix aes in vignette --- vignettes/cookbook/_chart-types.Rmd | 1 + 1 file changed, 1 insertion(+) diff --git a/vignettes/cookbook/_chart-types.Rmd b/vignettes/cookbook/_chart-types.Rmd index 8635b8f..4e6a22d 100644 --- a/vignettes/cookbook/_chart-types.Rmd +++ b/vignettes/cookbook/_chart-types.Rmd @@ -7,6 +7,7 @@ gapminder |> filter(country == "United Kingdom") |> + ggplot(aes(x = year, y = lifeExp)) + geom_line(linewidth = 1, colour = af_colour_values["dark-blue"]) + theme_af() + scale_y_continuous(limits = c(0, 82), From dc59a535d6a2e3ed7e9ac5cfa9647cd87076b479 Mon Sep 17 00:00:00 2001 From: Rosetta <135234996+gisellerosetta@users.noreply.github.com> Date: Wed, 27 Nov 2024 11:29:21 +0000 Subject: [PATCH 04/17] update readme graphs --- README.Rmd | 60 +- README.md | 61 +- man/figures/README-ex1-1.svg | 1043 +++++++++++++++++----------------- man/figures/README-ex2-1.svg | 855 ++++++++++++++-------------- man/figures/README-ex3-1.svg | 469 +++++++++++++-- 5 files changed, 1468 insertions(+), 1020 deletions(-) diff --git a/README.Rmd b/README.Rmd index dbbfa90..c46dc7f 100644 --- a/README.Rmd +++ b/README.Rmd @@ -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 afcharts logo @@ -80,7 +82,7 @@ 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} library(ggplot2) @@ -89,40 +91,66 @@ 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)) + + geom_col(aes(x = reorder(country, -pop), y = pop/(10^6))) + scale_y_continuous(expand = c(0, 0)) + + 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} 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)) + + filter(year == 2007 & continent == "Americas") |> + slice_max(order_by = pop, n = 5) |> + ggplot(aes(x = reorder(country, -pop), y = pop/(10^6))) + + geom_col(fill = af_colour_values["dark-blue"]) + scale_y_continuous(expand = c(0, 0)) + 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} +afcharts::use_afcharts() + +gapminder |> + filter(country %in% c("United Kingdom", "China")) |> + ggplot(aes(x = year, y = lifeExp, colour = country)) + + geom_line(linewidth = 1) + + theme_af(legend = "bottom") + + scale_colour_discrete_af() + + scale_y_continuous(limits = c(0, 82), + breaks = seq(0, 80, 20), + expand = c(0, 0)) + + 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**
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 diff --git a/README.md b/README.md index 94130f6..d001510 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,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) @@ -84,44 +84,75 @@ 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)) + + geom_col(aes(x = reorder(country, -pop), y = pop/(10^6))) + scale_y_continuous(expand = c(0, 0)) + + 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" - ) + ) ``` 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() #> NULL gapminder |> - filter(year == 2007 & continent == "Europe") |> - slice_max(order_by = lifeExp, n = 5) |> - ggplot() + - geom_col(aes(x = reorder(country, -lifeExp), y = lifeExp)) + + filter(year == 2007 & continent == "Americas") |> + slice_max(order_by = pop, n = 5) |> + ggplot(aes(x = reorder(country, -pop), y = pop/(10^6))) + + geom_col(fill = af_colour_values["dark-blue"]) + scale_y_continuous(expand = c(0, 0)) + 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" ) ``` -A bar chart with white background, light grey horizontal grid lines dark blue bars. +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) + + theme_af(legend = "bottom") + + scale_colour_discrete_af() + + scale_y_continuous(limits = c(0, 82), + breaks = seq(0, 80, 20), + expand = c(0, 0)) + + 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 + ) +``` + +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**
Titles, subtitles and captions have been embedded in these example charts for diff --git a/man/figures/README-ex1-1.svg b/man/figures/README-ex1-1.svg index c0cc16a..634496b 100644 --- a/man/figures/README-ex1-1.svg +++ b/man/figures/README-ex1-1.svg @@ -1,534 +1,557 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/man/figures/README-ex2-1.svg b/man/figures/README-ex2-1.svg index 8a98779..f115375 100644 --- a/man/figures/README-ex2-1.svg +++ b/man/figures/README-ex2-1.svg @@ -1,433 +1,454 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/man/figures/README-ex3-1.svg b/man/figures/README-ex3-1.svg index 2a09f0e..7149067 100644 --- a/man/figures/README-ex3-1.svg +++ b/man/figures/README-ex3-1.svg @@ -1,67 +1,412 @@ - - + + - - - - - - - - - - + + + - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - -0 -100 -200 -300 - - - - - - - - - - -United States -Brazil -Mexico -Colombia -Argentina -Population of countries in the Americas (millions), 2007 -Brazil has the second highest population in -the Americas -Source: Gapminder + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 9a2491cf0733abff13ff0215c30ee3299e30e4fe Mon Sep 17 00:00:00 2001 From: gisellerosetta Date: Wed, 27 Nov 2024 11:30:58 +0000 Subject: [PATCH 05/17] Re-build README.md --- man/figures/README-ex1-1.svg | 1080 +++++++++++++++++----------------- man/figures/README-ex2-1.svg | 886 ++++++++++++++-------------- man/figures/README-ex3-1.svg | 810 ++++++++++++------------- 3 files changed, 1405 insertions(+), 1371 deletions(-) diff --git a/man/figures/README-ex1-1.svg b/man/figures/README-ex1-1.svg index 634496b..6e038f3 100644 --- a/man/figures/README-ex1-1.svg +++ b/man/figures/README-ex1-1.svg @@ -1,557 +1,571 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/man/figures/README-ex2-1.svg b/man/figures/README-ex2-1.svg index f115375..cd3f062 100644 --- a/man/figures/README-ex2-1.svg +++ b/man/figures/README-ex2-1.svg @@ -1,454 +1,464 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - diff --git a/man/figures/README-ex3-1.svg b/man/figures/README-ex3-1.svg index 7149067..9db4ed4 100644 --- a/man/figures/README-ex3-1.svg +++ b/man/figures/README-ex3-1.svg @@ -1,412 +1,422 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + - - + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - From 1ad6d94fbdecc3077d4ae0699378288c84535ebe Mon Sep 17 00:00:00 2001 From: Rosetta <135234996+gisellerosetta@users.noreply.github.com> Date: Wed, 27 Nov 2024 13:56:14 +0000 Subject: [PATCH 06/17] fix cut off text --- vignettes/cookbook/_annotations.Rmd | 3 +-- vignettes/cookbook/_customisations.Rmd | 6 ++++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/vignettes/cookbook/_annotations.Rmd b/vignettes/cookbook/_annotations.Rmd index 1804096..27adecd 100644 --- a/vignettes/cookbook/_annotations.Rmd +++ b/vignettes/cookbook/_annotations.Rmd @@ -84,8 +84,7 @@ Annotations may also be used to add value labels to a bar chart. Note that `geom ```{r annotations-3} #| fig.alt = "A bar chart with white text labels at the end of each bar." - -ggplot(life_exp_bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) + +ggplot(pop_bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) + geom_col(fill = af_colour_values["dark-blue"]) + geom_text(aes(label = round(lifeExp, 1)), nudge_y = -5, colour = "white") + diff --git a/vignettes/cookbook/_customisations.Rmd b/vignettes/cookbook/_customisations.Rmd index 3d7dd9e..3531233 100644 --- a/vignettes/cookbook/_customisations.Rmd +++ b/vignettes/cookbook/_customisations.Rmd @@ -29,7 +29,9 @@ last_plot() + labs( x = NULL, y = NULL, - title = "Iceland has the highest life expectancy in Europe", + title = stringr::str_wrap( + paste("Iceland has the highest life expectancy in Europe"), + width = 40), subtitle = "Life expectancy in European countries, 2007", caption = "Source: Gapminder" ) @@ -185,7 +187,7 @@ plot + title = stringr::str_wrap( paste("Iceland has the highest life expectancy in Europe", "followed closely by Switzerland"), - width = 50 + width = 40 ) ) ``` From 225441a6785b422fb7b480199c14fcdaa62d250a Mon Sep 17 00:00:00 2001 From: Rosetta <135234996+gisellerosetta@users.noreply.github.com> Date: Wed, 27 Nov 2024 13:58:58 +0000 Subject: [PATCH 07/17] update annotations example 3 --- vignettes/cookbook/_annotations.Rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vignettes/cookbook/_annotations.Rmd b/vignettes/cookbook/_annotations.Rmd index 27adecd..f195a2b 100644 --- a/vignettes/cookbook/_annotations.Rmd +++ b/vignettes/cookbook/_annotations.Rmd @@ -84,7 +84,7 @@ Annotations may also be used to add value labels to a bar chart. Note that `geom ```{r annotations-3} #| fig.alt = "A bar chart with white text labels at the end of each bar." -ggplot(pop_bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) + +ggplot(life_exp_bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) + geom_col(fill = af_colour_values["dark-blue"]) + geom_text(aes(label = round(lifeExp, 1)), nudge_y = -5, colour = "white") + From 88e16e0282627cfae3cc30ded8a178c6b38ec51a Mon Sep 17 00:00:00 2001 From: Rosetta <135234996+gisellerosetta@users.noreply.github.com> Date: Wed, 27 Nov 2024 15:51:37 +0000 Subject: [PATCH 08/17] update readme graph axes --- README.Rmd | 14 +- README.md | 14 +- man/figures/README-ex1-1.svg | 1143 ++++++++++++++++++---------------- man/figures/README-ex2-1.svg | 927 ++++++++++++++------------- man/figures/README-ex3-1.svg | 810 ++++++++++++------------ 5 files changed, 1496 insertions(+), 1412 deletions(-) diff --git a/README.Rmd b/README.Rmd index c46dc7f..366b99d 100644 --- a/README.Rmd +++ b/README.Rmd @@ -95,13 +95,16 @@ gapminder |> slice_max(order_by = pop, n = 5) |> ggplot() + geom_col(aes(x = reorder(country, -pop), y = pop/(10^6))) + - scale_y_continuous(expand = c(0, 0)) + + scale_y_continuous(expand = c(0, 0), + limits = c(0, 350), + breaks = c(seq(0, 350, 50)), + labels = c(seq(0, 300, 50), "350 million")) + scale_fill_discrete_af("focus", reverse = TRUE) + labs( x = NULL, y = NULL, title = "The U.S.A. is the most populous country in\nthe Americas", - subtitle = "Population of countries in the Americas (millions), 2007", + subtitle = "Population of countries in the Americas, 2007", caption = "Source: Gapminder" ) ``` @@ -116,12 +119,15 @@ gapminder |> slice_max(order_by = pop, n = 5) |> ggplot(aes(x = reorder(country, -pop), y = pop/(10^6))) + geom_col(fill = af_colour_values["dark-blue"]) + - scale_y_continuous(expand = c(0, 0)) + + scale_y_continuous(expand = c(0, 0), + limits = c(0, 350), + breaks = c(seq(0, 350, 50)), + labels = c(seq(0, 300, 50), "350 million")) + labs( x = NULL, y = NULL, title = "The U.S.A. is the most populous country in\nthe Americas", - subtitle = "Population of countries in the Americas (millions), 2007", + subtitle = "Population of countries in the Americas, 2007", caption = "Source: Gapminder" ) ``` diff --git a/README.md b/README.md index d001510..e5ffa6b 100644 --- a/README.md +++ b/README.md @@ -88,13 +88,16 @@ gapminder |> slice_max(order_by = pop, n = 5) |> ggplot() + geom_col(aes(x = reorder(country, -pop), y = pop/(10^6))) + - scale_y_continuous(expand = c(0, 0)) + + scale_y_continuous(expand = c(0, 0), + limits = c(0, 350), + breaks = c(seq(0, 350, 50)), + labels = c(seq(0, 300, 50), "350 million")) + scale_fill_discrete_af("focus", reverse = TRUE) + labs( x = NULL, y = NULL, title = "The U.S.A. is the most populous country in\nthe Americas", - subtitle = "Population of countries in the Americas (millions), 2007", + subtitle = "Population of countries in the Americas, 2007", caption = "Source: Gapminder" ) ``` @@ -112,12 +115,15 @@ gapminder |> slice_max(order_by = pop, n = 5) |> ggplot(aes(x = reorder(country, -pop), y = pop/(10^6))) + geom_col(fill = af_colour_values["dark-blue"]) + - scale_y_continuous(expand = c(0, 0)) + + scale_y_continuous(expand = c(0, 0), + limits = c(0, 350), + breaks = c(seq(0, 350, 50)), + labels = c(seq(0, 300, 50), "350 million")) + labs( x = NULL, y = NULL, title = "The U.S.A. is the most populous country in\nthe Americas", - subtitle = "Population of countries in the Americas (millions), 2007", + subtitle = "Population of countries in the Americas, 2007", caption = "Source: Gapminder" ) ``` diff --git a/man/figures/README-ex1-1.svg b/man/figures/README-ex1-1.svg index 6e038f3..f99a8ff 100644 --- a/man/figures/README-ex1-1.svg +++ b/man/figures/README-ex1-1.svg @@ -1,571 +1,622 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/man/figures/README-ex2-1.svg b/man/figures/README-ex2-1.svg index cd3f062..f55ff00 100644 --- a/man/figures/README-ex2-1.svg +++ b/man/figures/README-ex2-1.svg @@ -1,464 +1,495 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/man/figures/README-ex3-1.svg b/man/figures/README-ex3-1.svg index 9db4ed4..7149067 100644 --- a/man/figures/README-ex3-1.svg +++ b/man/figures/README-ex3-1.svg @@ -1,422 +1,412 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + - - + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 40de1a783c7c257b838791a8a1ddca8c0a8a2eeb Mon Sep 17 00:00:00 2001 From: gisellerosetta Date: Wed, 27 Nov 2024 15:54:02 +0000 Subject: [PATCH 09/17] Re-build README.md --- man/figures/README-ex1-1.svg | 1170 +++++++++++++++++----------------- man/figures/README-ex2-1.svg | 962 ++++++++++++++-------------- man/figures/README-ex3-1.svg | 810 +++++++++++------------ 3 files changed, 1488 insertions(+), 1454 deletions(-) diff --git a/man/figures/README-ex1-1.svg b/man/figures/README-ex1-1.svg index f99a8ff..cfcf6f4 100644 --- a/man/figures/README-ex1-1.svg +++ b/man/figures/README-ex1-1.svg @@ -1,622 +1,636 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - - - + + + + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/man/figures/README-ex2-1.svg b/man/figures/README-ex2-1.svg index f55ff00..3838735 100644 --- a/man/figures/README-ex2-1.svg +++ b/man/figures/README-ex2-1.svg @@ -1,495 +1,505 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/man/figures/README-ex3-1.svg b/man/figures/README-ex3-1.svg index 7149067..9db4ed4 100644 --- a/man/figures/README-ex3-1.svg +++ b/man/figures/README-ex3-1.svg @@ -1,412 +1,422 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + - - + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - From 0fa18e9a8de65644ffc2e436d8d163253c03fe7b Mon Sep 17 00:00:00 2001 From: Rosetta <135234996+gisellerosetta@users.noreply.github.com> Date: Wed, 27 Nov 2024 16:25:25 +0000 Subject: [PATCH 10/17] updated cookbook graph axes --- vignettes/cookbook/_annotations.Rmd | 5 ++- vignettes/cookbook/_chart-types.Rmd | 42 ++++++++++++++++++-------- vignettes/cookbook/_customisations.Rmd | 5 ++- 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/vignettes/cookbook/_annotations.Rmd b/vignettes/cookbook/_annotations.Rmd index f195a2b..1a0aac7 100644 --- a/vignettes/cookbook/_annotations.Rmd +++ b/vignettes/cookbook/_annotations.Rmd @@ -89,7 +89,10 @@ ggplot(life_exp_bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) + geom_text(aes(label = round(lifeExp, 1)), nudge_y = -5, colour = "white") + theme_af() + - scale_y_continuous(expand = c(0, 0)) + + scale_y_continuous(expand = c(0, 0), + limits = c(0, 100), + breaks = c(seq(0, 100, 20)), + labels = c(seq(0, 100, 20))) + labs( x = NULL, y = NULL, diff --git a/vignettes/cookbook/_chart-types.Rmd b/vignettes/cookbook/_chart-types.Rmd index 4e6a22d..6c30569 100644 --- a/vignettes/cookbook/_chart-types.Rmd +++ b/vignettes/cookbook/_chart-types.Rmd @@ -65,14 +65,17 @@ pop_bar_data <- gapminder |> ggplot(pop_bar_data, aes(x = reorder(country, -pop), y = pop/(10^6))) + geom_col(fill = af_colour_values["dark-blue"]) + theme_af() + - scale_y_continuous(expand = c(0, 0)) + + scale_y_continuous(expand = c(0, 0), + limits = c(0, 350), + breaks = c(seq(0, 350, 50)), + labels = c(seq(0, 300, 50), "350 million")) + labs( x = NULL, y = NULL, - title = "The U.S.A. is the most populous country in\nthe Americas", - subtitle = "Population of countries in the Americas (millions), 2007", + title = stringr::str_wrap("The U.S.A. is the most populous country in the Americas", 40), + subtitle = "Population of countries in the Americas, 2007", caption = "Source: Gapminder" - ) + ) ``` A bar chart can sometimes look better with horizontal bars. This can also be a good option if your bar labels are long and difficult to display horizontally on the x axis. To produce a horizontal bar chart, swap the variables defined for x and y in `aes()` and make a few tweaks to `theme_af()`; draw grid lines for the x axis only by setting the `grid` argument, and draw an axis line for the y axis only by setting the `axis` argument. @@ -83,12 +86,15 @@ A bar chart can sometimes look better with horizontal bars. This can also be a g ggplot(pop_bar_data, aes(x = pop/(10^6), y = reorder(country, -pop))) + geom_col(fill = af_colour_values["dark-blue"]) + theme_af(grid = "x", axis = "y") + - scale_x_continuous(expand = c(0, 0)) + + scale_x_continuous(expand = c(0, 0), + limits = c(0, 350), + breaks = c(seq(0, 350, 50)), + labels = c(seq(0, 300, 50), "350 million")) + labs( x = NULL, y = NULL, - title = "The U.S.A. is the most populous country in\nthe Americas", - subtitle = "Population of countries in the Americas (millions), 2007", + title = stringr::str_wrap("The U.S.A. is the most populous country in the Americas", 40), + subtitle = "Population of countries in the Americas, 2007", caption = "Source: Gapminder" ) ``` @@ -109,7 +115,10 @@ grouped_bar_data <- ggplot(grouped_bar_data, aes(x = country, y = lifeExp, fill = as.factor(year))) + geom_bar(stat = "identity", position = "dodge") + - scale_y_continuous(expand = c(0, 0)) + + scale_y_continuous(expand = c(0, 0), + limits = c(0, 100), + breaks = c(seq(0, 100, 20)), + labels = c(seq(0, 100, 20))) + theme_af(legend = "bottom") + scale_fill_discrete_af() + labs( @@ -170,7 +179,10 @@ gapminder |> colour = "white", fill = af_colour_values["dark-blue"]) + theme_af() + - scale_y_continuous(expand = c(0, 0)) + + scale_y_continuous(expand = c(0, 0), + limits = c(0, 35), + breaks = c(seq(0, 35, 5)), + labels = c(seq(0, 35, 5))) + labs( x = NULL, y = "Number of \ncountries", @@ -273,12 +285,15 @@ pop_bar_data |> ) + geom_col() + theme_af(legend = "none") + - scale_y_continuous(expand = c(0, 0)) + + scale_y_continuous(expand = c(0, 0), + limits = c(0, 350), + breaks = c(seq(0, 350, 50)), + labels = c(seq(0, 300, 50), "350 million")) + scale_fill_discrete_af("focus", reverse = TRUE) + labs( x = NULL, y = NULL, - title = "Brazil has the second highest population in\nthe Americas", + title = stringr::str_wrap("Brazil has the second highest population in the Americas", 40), subtitle = "Population of countries in the Americas (millions), 2007", caption = "Source: Gapminder" ) @@ -314,7 +329,10 @@ p <- geom_col(fill = af_colour_values["dark-blue"]) + theme_af(ticks = "x") + theme(text = element_text(family = "")) + - scale_y_continuous(expand = c(0, 0)) + + scale_y_continuous(expand = c(0, 0), + limits = c(0, 100), + breaks = c(seq(0, 100, 20)), + labels = c(seq(0, 100, 20))) + labs( x = NULL, y = NULL diff --git a/vignettes/cookbook/_customisations.Rmd b/vignettes/cookbook/_customisations.Rmd index 3531233..cb22235 100644 --- a/vignettes/cookbook/_customisations.Rmd +++ b/vignettes/cookbook/_customisations.Rmd @@ -205,7 +205,10 @@ ggplot(life_exp_bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) + theme_af(axis = "xy") + theme(axis.line = element_line(colour = "black"), axis.ticks = element_line(colour = "black")) + - scale_y_continuous(expand = c(0, 0)) + + scale_y_continuous(expand = c(0, 0), + limits = c(0, 100), + breaks = c(seq(0, 100, 20)), + labels = c(seq(0, 100, 20))) + labs( x = NULL, y = NULL, From 61cb403d9ed924ebe4ca1964a1771bb172d683f6 Mon Sep 17 00:00:00 2001 From: Rosetta <135234996+gisellerosetta@users.noreply.github.com> Date: Thu, 5 Dec 2024 10:53:20 +0000 Subject: [PATCH 11/17] scales first example --- vignettes/cookbook.Rmd | 1 + vignettes/cookbook/_chart-types.Rmd | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/vignettes/cookbook.Rmd b/vignettes/cookbook.Rmd index f89a2ac..1732808 100644 --- a/vignettes/cookbook.Rmd +++ b/vignettes/cookbook.Rmd @@ -35,6 +35,7 @@ library(afcharts) library(ggplot2) library(dplyr) library(ggtext) +library(scales) # Use gapminder data for cookbook charts library(gapminder) diff --git a/vignettes/cookbook/_chart-types.Rmd b/vignettes/cookbook/_chart-types.Rmd index 6c30569..c83fce9 100644 --- a/vignettes/cookbook/_chart-types.Rmd +++ b/vignettes/cookbook/_chart-types.Rmd @@ -68,7 +68,7 @@ ggplot(pop_bar_data, aes(x = reorder(country, -pop), y = pop/(10^6))) + scale_y_continuous(expand = c(0, 0), limits = c(0, 350), breaks = c(seq(0, 350, 50)), - labels = c(seq(0, 300, 50), "350 million")) + + labels = scales::label_number(suffix = " millions")) + labs( x = NULL, y = NULL, From b50349b5904d7b86ad8df2db4285f00c565c362b Mon Sep 17 00:00:00 2001 From: Olivia Box Power Date: Thu, 5 Dec 2024 11:16:20 +0000 Subject: [PATCH 12/17] Update bar chart scale --- vignettes/cookbook/_chart-types.Rmd | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/vignettes/cookbook/_chart-types.Rmd b/vignettes/cookbook/_chart-types.Rmd index c83fce9..d453035 100644 --- a/vignettes/cookbook/_chart-types.Rmd +++ b/vignettes/cookbook/_chart-types.Rmd @@ -62,18 +62,19 @@ pop_bar_data <- gapminder |> ```{r bar-chart-1} #| fig.alt = "A bar chart using afcharts theme and dark blue colour." -ggplot(pop_bar_data, aes(x = reorder(country, -pop), y = pop/(10^6))) + +ggplot(pop_bar_data, aes(x = reorder(country, -pop), y = pop)) + geom_col(fill = af_colour_values["dark-blue"]) + theme_af() + - scale_y_continuous(expand = c(0, 0), - limits = c(0, 350), - breaks = c(seq(0, 350, 50)), - labels = scales::label_number(suffix = " millions")) + + scale_y_continuous( + expand = c(0, 0), + limits = c(0, 450E6), + labels = scales::label_number(scale = 1E-6) + ) + labs( x = NULL, y = NULL, title = stringr::str_wrap("The U.S.A. is the most populous country in the Americas", 40), - subtitle = "Population of countries in the Americas, 2007", + subtitle = "Population of countries in the Americas (millions), 2007", caption = "Source: Gapminder" ) ``` From f0bf8376b29d9d248af3dc8b2118e07684f1986d Mon Sep 17 00:00:00 2001 From: Rosetta <135234996+gisellerosetta@users.noreply.github.com> Date: Fri, 13 Dec 2024 11:51:22 +0000 Subject: [PATCH 13/17] update graph scales --- vignettes/cookbook/_annotations.Rmd | 18 +++++--- vignettes/cookbook/_chart-types.Rmd | 70 +++++++++++++++++------------ 2 files changed, 52 insertions(+), 36 deletions(-) diff --git a/vignettes/cookbook/_annotations.Rmd b/vignettes/cookbook/_annotations.Rmd index 1a0aac7..26c82d8 100644 --- a/vignettes/cookbook/_annotations.Rmd +++ b/vignettes/cookbook/_annotations.Rmd @@ -17,9 +17,11 @@ ann_data |> geom_line(linewidth = 1) + theme_af(legend = "none") + scale_colour_discrete_af() + - scale_y_continuous(limits = c(0, 82), - breaks = seq(0, 80, 20), - expand = c(0, 0)) + + scale_y_continuous( + limits = c(0, 82), + breaks = seq(0, 80, 20), + expand = c(0, 0) + ) + scale_x_continuous(limits = c(1952, 2017), breaks = seq(1952, 2017, 5)) + annotate(geom = "label", x = 2008, y = 73, label = "China", @@ -89,10 +91,12 @@ ggplot(life_exp_bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) + geom_text(aes(label = round(lifeExp, 1)), nudge_y = -5, colour = "white") + theme_af() + - scale_y_continuous(expand = c(0, 0), - limits = c(0, 100), - breaks = c(seq(0, 100, 20)), - labels = c(seq(0, 100, 20))) + + scale_y_continuous( + expand = c(0, 0), + limits = c(0, 100), + breaks = c(seq(0, 100, 20)), + labels = c(seq(0, 100, 20)) + ) + labs( x = NULL, y = NULL, diff --git a/vignettes/cookbook/_chart-types.Rmd b/vignettes/cookbook/_chart-types.Rmd index d453035..b9ec0a9 100644 --- a/vignettes/cookbook/_chart-types.Rmd +++ b/vignettes/cookbook/_chart-types.Rmd @@ -10,9 +10,11 @@ gapminder |> ggplot(aes(x = year, y = lifeExp)) + geom_line(linewidth = 1, colour = af_colour_values["dark-blue"]) + theme_af() + - scale_y_continuous(limits = c(0, 82), - breaks = seq(0, 80, 20), - expand = c(0, 0)) + + scale_y_continuous( + limits = c(0, 82), + breaks = seq(0, 80, 20), + expand = c(0, 0) + ) + scale_x_continuous(breaks = seq(1952, 2007, 5)) + labs( x = "Year", @@ -34,9 +36,11 @@ gapminder |> geom_line(linewidth = 1) + theme_af(legend = "bottom") + scale_colour_discrete_af() + - scale_y_continuous(limits = c(0, 82), - breaks = seq(0, 80, 20), - expand = c(0, 0)) + + scale_y_continuous( + limits = c(0, 82), + breaks = seq(0, 80, 20), + expand = c(0, 0) + ) + scale_x_continuous(breaks = seq(1952, 2007, 5)) + labs( x = "Year", @@ -84,18 +88,19 @@ A bar chart can sometimes look better with horizontal bars. This can also be a g ```{r bar-chart-2} #| fig.alt = "A horizontal bar chart using afcharts theme and dark blue colour." -ggplot(pop_bar_data, aes(x = pop/(10^6), y = reorder(country, -pop))) + +ggplot(pop_bar_data, aes(x = pop, y = reorder(country, pop))) + geom_col(fill = af_colour_values["dark-blue"]) + theme_af(grid = "x", axis = "y") + - scale_x_continuous(expand = c(0, 0), - limits = c(0, 350), - breaks = c(seq(0, 350, 50)), - labels = c(seq(0, 300, 50), "350 million")) + + scale_x_continuous( + expand = c(0, 0), + limits = c(0, 450E6), + labels = scales::label_number(scale = 1E-6) + ) + labs( x = NULL, y = NULL, title = stringr::str_wrap("The U.S.A. is the most populous country in the Americas", 40), - subtitle = "Population of countries in the Americas, 2007", + subtitle = "Population of countries in the Americas (millions), 2007", caption = "Source: Gapminder" ) ``` @@ -116,10 +121,12 @@ grouped_bar_data <- ggplot(grouped_bar_data, aes(x = country, y = lifeExp, fill = as.factor(year))) + geom_bar(stat = "identity", position = "dodge") + - scale_y_continuous(expand = c(0, 0), - limits = c(0, 100), - breaks = c(seq(0, 100, 20)), - labels = c(seq(0, 100, 20))) + + scale_y_continuous( + expand = c(0, 0), + limits = c(0, 100), + breaks = c(seq(0, 100, 20)), + labels = c(seq(0, 100, 20)) + ) + theme_af(legend = "bottom") + scale_fill_discrete_af() + labs( @@ -180,10 +187,12 @@ gapminder |> colour = "white", fill = af_colour_values["dark-blue"]) + theme_af() + - scale_y_continuous(expand = c(0, 0), - limits = c(0, 35), - breaks = c(seq(0, 35, 5)), - labels = c(seq(0, 35, 5))) + + scale_y_continuous( + expand = c(0, 0), + limits = c(0, 35), + breaks = c(seq(0, 35, 5)), + labels = c(seq(0, 35, 5)) + ) + labs( x = NULL, y = "Number of \ncountries", @@ -281,15 +290,16 @@ stacked_bar_data |> pop_bar_data |> ggplot( - aes(x = reorder(country, -pop), y = pop/(10^6), + aes(x = reorder(country, -pop), y = pop, fill = country == "Brazil") ) + geom_col() + theme_af(legend = "none") + - scale_y_continuous(expand = c(0, 0), - limits = c(0, 350), - breaks = c(seq(0, 350, 50)), - labels = c(seq(0, 300, 50), "350 million")) + + scale_y_continuous( + expand = c(0, 0), + limits = c(0, 450E6), + labels = scales::label_number(scale = 1E-6) + ) + scale_fill_discrete_af("focus", reverse = TRUE) + labs( x = NULL, @@ -330,10 +340,12 @@ p <- geom_col(fill = af_colour_values["dark-blue"]) + theme_af(ticks = "x") + theme(text = element_text(family = "")) + - scale_y_continuous(expand = c(0, 0), - limits = c(0, 100), - breaks = c(seq(0, 100, 20)), - labels = c(seq(0, 100, 20))) + + scale_y_continuous( + expand = c(0, 0), + limits = c(0, 100), + breaks = c(seq(0, 100, 20)), + labels = c(seq(0, 100, 20)) + ) + labs( x = NULL, y = NULL From 0bcff9457e2926eeae3ba8639edbc6a99e092c47 Mon Sep 17 00:00:00 2001 From: Rosetta <135234996+gisellerosetta@users.noreply.github.com> Date: Mon, 6 Jan 2025 09:41:52 +0000 Subject: [PATCH 14/17] update bar charts data --- vignettes/cookbook/_annotations.Rmd | 16 ++++----- vignettes/cookbook/_chart-types.Rmd | 15 +++----- vignettes/cookbook/_customisations.Rmd | 47 +++++++++++++------------- 3 files changed, 35 insertions(+), 43 deletions(-) diff --git a/vignettes/cookbook/_annotations.Rmd b/vignettes/cookbook/_annotations.Rmd index 26c82d8..5e3a8df 100644 --- a/vignettes/cookbook/_annotations.Rmd +++ b/vignettes/cookbook/_annotations.Rmd @@ -86,22 +86,20 @@ Annotations may also be used to add value labels to a bar chart. Note that `geom ```{r annotations-3} #| fig.alt = "A bar chart with white text labels at the end of each bar." -ggplot(life_exp_bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) + +ggplot(pop_bar_data, aes(x = reorder(country, -pop), y = pop)) + geom_col(fill = af_colour_values["dark-blue"]) + - geom_text(aes(label = round(lifeExp, 1)), - nudge_y = -5, colour = "white") + + geom_text(aes(label = round(pop/1E6, 1),), + vjust = 1.2, colour = "white") + theme_af() + scale_y_continuous( expand = c(0, 0), - limits = c(0, 100), - breaks = c(seq(0, 100, 20)), - labels = c(seq(0, 100, 20)) - ) + + limits = c(0, 350E6), + labels = scales::label_number(scale = 1E-6)) + labs( x = NULL, y = NULL, - title = "Iceland has the highest life expectancy in Europe", - subtitle = "Life expectancy in European countries, 2007", + title = stringr::str_wrap("The U.S.A. is the most populous country in the Americas", 40), + subtitle = "Population of countries in the Americas (millions), 2007", caption = "Source: Gapminder" ) ``` diff --git a/vignettes/cookbook/_chart-types.Rmd b/vignettes/cookbook/_chart-types.Rmd index b9ec0a9..cd199e9 100644 --- a/vignettes/cookbook/_chart-types.Rmd +++ b/vignettes/cookbook/_chart-types.Rmd @@ -71,7 +71,7 @@ ggplot(pop_bar_data, aes(x = reorder(country, -pop), y = pop)) + theme_af() + scale_y_continuous( expand = c(0, 0), - limits = c(0, 450E6), + limits = c(0, 350E6), labels = scales::label_number(scale = 1E-6) ) + labs( @@ -93,7 +93,7 @@ ggplot(pop_bar_data, aes(x = pop, y = reorder(country, pop))) + theme_af(grid = "x", axis = "y") + scale_x_continuous( expand = c(0, 0), - limits = c(0, 450E6), + limits = c(0, 350E6), labels = scales::label_number(scale = 1E-6) ) + labs( @@ -297,7 +297,7 @@ pop_bar_data |> theme_af(legend = "none") + scale_y_continuous( expand = c(0, 0), - limits = c(0, 450E6), + limits = c(0, 350E6), labels = scales::label_number(scale = 1E-6) ) + scale_fill_discrete_af("focus", reverse = TRUE) + @@ -319,18 +319,11 @@ To make a `ggplot2` chart interactive, use `ggplotly()` from the `plotly` packag * Subtitles and captions are not supported in `ggplotly()`. As stated elsewhere in this guidance, titles and subtitles should ideally be included in the body of text surrounding a chart rather than embedded in the chart itself, and so this is hopefully not a big issue. This example therefore has no title, subtitle or caption. -```{r life-exp-bar-data} -life_exp_bar_data <- - gapminder |> - filter(year == 2007 & continent == "Europe") |> - slice_max(order_by = lifeExp, n = 5) -``` - ```{r interactive-charts} #| fig.alt = "An interactive bar chart using afcharts theme and dark blue colour. A tooltip appears when hovering over each bar." p <- - life_exp_bar_data |> + pop_bar_data |> # Format text for tooltips mutate(tooltip = paste0( "Country: ", country, "\n", diff --git a/vignettes/cookbook/_customisations.Rmd b/vignettes/cookbook/_customisations.Rmd index cb22235..6b8f03f 100644 --- a/vignettes/cookbook/_customisations.Rmd +++ b/vignettes/cookbook/_customisations.Rmd @@ -9,8 +9,8 @@ To control the order of bars in a chart, wrap the variable you want to arrange w ```{r sorting} #| fig.alt = "A bar chart using afcharts theme and dark blue colour with bars sorted in decreasing order by life expectancy." -life_exp_bar_data |> - ggplot(aes(x = lifeExp, y = reorder(country, lifeExp))) + +pop_bar_data |> + ggplot(aes(x = pop, y = reorder(country, pop))) + geom_col(fill = af_colour_values["dark-blue"]) + theme_af(axis = "y", grid = "x") ``` @@ -30,9 +30,9 @@ last_plot() + x = NULL, y = NULL, title = stringr::str_wrap( - paste("Iceland has the highest life expectancy in Europe"), + paste("The U.S.A. has the highest population in the Americas"), width = 40), - subtitle = "Life expectancy in European countries, 2007", + subtitle = "Population of countries of the Americas, 2007", caption = "Source: Gapminder" ) ``` @@ -61,10 +61,10 @@ Limits, breaks and labels can be defined with custom values. #| fig.alt = "A bar chart with fewer x axis breaks and edited labels." last_plot() + - scale_x_continuous(expand = c(0, 0), - limits = c(0, 85), - breaks = seq(0, 80, 20), - labels = c(seq(0, 70, 20), "80 years")) + scale_x_continuous( + expand = c(0, 0), + limits = c(0, 350E6), + labels = scales::label_number(scale = 1E-6)) ``` Note that further calls to `scale_x/y_continuous` will overwrite previous calls, hence why `expand = c(0, 0)` has been included again in this example. @@ -158,21 +158,21 @@ If text is too long, it may be cut off or distort the dimensions of the chart. #| fig.alt = "A bar chart with end of main title text not visible and long y axis title skewing the dimensions of the chart." plot <- - ggplot(life_exp_bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) + + ggplot(pop_bar_data, aes(x = reorder(country, -pop), y = pop)) + geom_col(fill = af_colour_values["dark-blue"]) + theme_af() + scale_y_continuous(expand = c(0, 0)) + labs( x = NULL, - subtitle = "Life expectancy in European countries, 2007", + subtitle = "Population of countries in the Americas (millions), 2007", caption = "Source: Gapminder" ) plot + labs( - y = "Percentage of countries", - title = paste("Iceland has the highest life expectancy in Europe", - "followed closely by Switzerland") + y = "Population of country", + title = paste("The U.S.A. is the most populous country in ", + "the Americas") ) ``` @@ -183,10 +183,10 @@ There are two suggested ways to solve this issue; Insert `\n` within a string to plot + labs( - y = "Percentage\nof countries", + y = "Population\n of country", title = stringr::str_wrap( - paste("Iceland has the highest life expectancy in Europe", - "followed closely by Switzerland"), + paste("The U.S.A. is the most populous country in ", + "the Americas"), width = 40 ) ) @@ -200,20 +200,21 @@ If you find you need to adjust theme elements for your chart, this can be done u ```{r adjust-theme} #| fig.alt = "A bar chart using afcharts theme and dark blue colour, with axis lines and ticks coloured black." -ggplot(life_exp_bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) + +ggplot(pop_bar_data, aes(x = reorder(country, -pop), y = pop)) + geom_col(fill = af_colour_values["dark-blue"]) + theme_af(axis = "xy") + theme(axis.line = element_line(colour = "black"), axis.ticks = element_line(colour = "black")) + - scale_y_continuous(expand = c(0, 0), - limits = c(0, 100), - breaks = c(seq(0, 100, 20)), - labels = c(seq(0, 100, 20))) + + scale_y_continuous( + expand = c(0, 0), + limits = c(0, 350E6), + labels = scales::label_number(scale = 1E-6) + ) + labs( x = NULL, y = NULL, - title = "Iceland has the highest life expectancy in Europe", - subtitle = "Life expectancy in European countries, 2007", + title = stringr::str_wrap("The U.S.A. is the most populous country in the Americas", 40), + subtitle = "Population of countries in the Americas (millions), 2007", caption = "Source: Gapminder" ) ``` From 48c841f96fc174599c4dfdfa28ab779e2f1ac062 Mon Sep 17 00:00:00 2001 From: gisellerosetta Date: Mon, 6 Jan 2025 09:57:10 +0000 Subject: [PATCH 15/17] Re-build README.md --- man/figures/README-ex1-1.svg | 1170 +++++++++++++++++----------------- man/figures/README-ex2-1.svg | 962 ++++++++++++++-------------- man/figures/README-ex3-1.svg | 810 ++++++++++++----------- 3 files changed, 1454 insertions(+), 1488 deletions(-) diff --git a/man/figures/README-ex1-1.svg b/man/figures/README-ex1-1.svg index cfcf6f4..c3f42a9 100644 --- a/man/figures/README-ex1-1.svg +++ b/man/figures/README-ex1-1.svg @@ -1,636 +1,622 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - - - - + + + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/man/figures/README-ex2-1.svg b/man/figures/README-ex2-1.svg index 3838735..16b52a9 100644 --- a/man/figures/README-ex2-1.svg +++ b/man/figures/README-ex2-1.svg @@ -1,505 +1,495 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/man/figures/README-ex3-1.svg b/man/figures/README-ex3-1.svg index 9db4ed4..9e34860 100644 --- a/man/figures/README-ex3-1.svg +++ b/man/figures/README-ex3-1.svg @@ -1,422 +1,412 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + - - + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 85a9d94d265fa6100216ab327c1628dac4e10590 Mon Sep 17 00:00:00 2001 From: Olivia Box Power Date: Mon, 6 Jan 2025 10:39:16 +0000 Subject: [PATCH 16/17] Minor update to cookbook code formatting --- vignettes/cookbook/_annotations.Rmd | 69 +++++++++++++++-------- vignettes/cookbook/_chart-types.Rmd | 74 +++++++++++++++---------- vignettes/cookbook/_colour-palettes.Rmd | 24 +++++--- vignettes/cookbook/_customisations.Rmd | 55 +++++++++++------- 4 files changed, 141 insertions(+), 81 deletions(-) diff --git a/vignettes/cookbook/_annotations.Rmd b/vignettes/cookbook/_annotations.Rmd index 5e3a8df..b0b34a2 100644 --- a/vignettes/cookbook/_annotations.Rmd +++ b/vignettes/cookbook/_annotations.Rmd @@ -22,16 +22,29 @@ ann_data |> breaks = seq(0, 80, 20), expand = c(0, 0) ) + - scale_x_continuous(limits = c(1952, 2017), - breaks = seq(1952, 2017, 5)) + - annotate(geom = "label", x = 2008, y = 73, label = "China", - colour = af_colour_values[1], - label.size = NA, - hjust = 0, vjust = 0.5) + - annotate(geom = "label", x = 2008, y = 79.4, label = "United Kingdom", - colour = af_colour_values[2], - label.size = NA, - hjust = 0, vjust = 0.5) + + scale_x_continuous( + limits = c(1952, 2017), + breaks = seq(1952, 2017, 5) + ) + + annotate( + geom = "label", + x = 2008, y = 73, + label = "China", + colour = af_colour_values[1], + label.size = NA, + hjust = 0, + vjust = 0.5 + ) + + annotate( + geom = "label", + x = 2008, + y = 79.4, + label = "United Kingdom", + colour = af_colour_values[2], + label.size = NA, + hjust = 0, + vjust = 0.5 + ) + labs( x = "Year", y = NULL, @@ -62,17 +75,23 @@ ann_data |> geom_line(linewidth = 1) + theme_af(legend = "none") + scale_colour_discrete_af() + - scale_y_continuous(limits = c(0, 82), - breaks = seq(0, 80, 20), - expand = c(0, 0)) + - scale_x_continuous(limits = c(1952, 2017), - breaks = seq(1952, 2017, 5)) + - geom_label(data = ann_labs, - aes(x = year, y = lifeExp, label = country, colour = country), - hjust = 0, - vjust = 0.5, - nudge_x = 0.5, - label.size = NA) + + scale_y_continuous( + limits = c(0, 82), + breaks = seq(0, 80, 20), + expand = c(0, 0) + ) + + scale_x_continuous( + limits = c(1952, 2017), + breaks = seq(1952, 2017, 5) + ) + + geom_label( + data = ann_labs, + aes(x = year, y = lifeExp, label = country, colour = country), + hjust = 0, + vjust = 0.5, + nudge_x = 0.5, + label.size = NA + ) + labs( x = "Year", y = NULL, @@ -88,8 +107,7 @@ Annotations may also be used to add value labels to a bar chart. Note that `geom #| fig.alt = "A bar chart with white text labels at the end of each bar." ggplot(pop_bar_data, aes(x = reorder(country, -pop), y = pop)) + geom_col(fill = af_colour_values["dark-blue"]) + - geom_text(aes(label = round(pop/1E6, 1),), - vjust = 1.2, colour = "white") + + geom_text(aes(label = round(pop/1E6, 1),), vjust = 1.2, colour = "white") + theme_af() + scale_y_continuous( expand = c(0, 0), @@ -98,7 +116,10 @@ ggplot(pop_bar_data, aes(x = reorder(country, -pop), y = pop)) + labs( x = NULL, y = NULL, - title = stringr::str_wrap("The U.S.A. is the most populous country in the Americas", 40), + title = stringr::str_wrap( + "The U.S.A. is the most populous country in the Americas", + 40 + ), subtitle = "Population of countries in the Americas (millions), 2007", caption = "Source: Gapminder" ) diff --git a/vignettes/cookbook/_chart-types.Rmd b/vignettes/cookbook/_chart-types.Rmd index cd199e9..462a389 100644 --- a/vignettes/cookbook/_chart-types.Rmd +++ b/vignettes/cookbook/_chart-types.Rmd @@ -99,7 +99,10 @@ ggplot(pop_bar_data, aes(x = pop, y = reorder(country, pop))) + labs( x = NULL, y = NULL, - title = stringr::str_wrap("The U.S.A. is the most populous country in the Americas", 40), + title = stringr::str_wrap( + "The U.S.A. is the most populous country in the Americas", + 40 + ), subtitle = "Population of countries in the Americas (millions), 2007", caption = "Source: Gapminder" ) @@ -115,11 +118,15 @@ To create a grouped bar chart, set `stat = "identity"` and `position = "dodge"` grouped_bar_data <- gapminder |> - filter(year %in% c(1967, 2007) & - country %in% c("United Kingdom", "Ireland", "France", "Belgium")) + filter( + year %in% c(1967, 2007) & + country %in% c("United Kingdom", "Ireland", "France", "Belgium") + ) -ggplot(grouped_bar_data, - aes(x = country, y = lifeExp, fill = as.factor(year))) + +ggplot( + grouped_bar_data, + aes(x = country, y = lifeExp, fill = as.factor(year)) +) + geom_bar(stat = "identity", position = "dodge") + scale_y_continuous( expand = c(0, 0), @@ -151,14 +158,20 @@ Caution should be taken when producing stacked bar charts. They can quickly beco stacked_bar_data <- gapminder |> filter(year == 2007) |> - mutate(lifeExpGrouped = cut(lifeExp, - breaks = c(0, 75, Inf), - labels = c("Under 75", "75+"))) |> + mutate( + lifeExpGrouped = cut( + lifeExp, + breaks = c(0, 75, Inf), + labels = c("Under 75", "75+") + ) + ) |> group_by(continent, lifeExpGrouped) |> summarise(n_countries = n(), .groups = "drop") -ggplot(stacked_bar_data, - aes(x = continent, y = n_countries, fill = lifeExpGrouped)) + +ggplot( + stacked_bar_data, + aes(x = continent, y = n_countries, fill = lifeExpGrouped) +) + geom_bar(stat = "identity", position = "fill") + theme_af(legend = "bottom") + scale_y_continuous(expand = c(0, 0), labels = scales::percent) + @@ -183,9 +196,11 @@ ggplot(stacked_bar_data, gapminder |> filter(year == 2007) |> ggplot(aes(x = lifeExp)) + - geom_histogram(binwidth = 5, - colour = "white", - fill = af_colour_values["dark-blue"]) + + geom_histogram( + binwidth = 5, + colour = "white", + fill = af_colour_values["dark-blue"] + ) + theme_af() + scale_y_continuous( expand = c(0, 0), @@ -213,16 +228,15 @@ gapminder |> ggplot(aes(x = gdpPercap, y = lifeExp, size = pop)) + geom_point(colour = af_colour_values["dark-blue"]) + theme_af(axis = "none", grid = "xy") + - scale_x_continuous( - labels = function(x) scales::dollar(x, prefix = "£") - ) + + scale_x_continuous(labels = function(x) scales::dollar(x, prefix = "£")) + scale_size_continuous(labels = scales::comma) + labs( x = "GDP", y = "Life\nExpectancy", size = "Population", title = stringr::str_wrap( - "The relationship between GDP and Life Expectancy is complex", 40 + "The relationship between GDP and Life Expectancy is complex", + 40 ), subtitle = "GDP and Life Expectancy for all countires, 2007", caption = "Source: Gapminder" @@ -244,8 +258,10 @@ gapminder |> theme_af(axis = "none", ticks = "none", legend = "none") + scale_fill_discrete_af() + facet_wrap(~ continent, ncol = 2) + - scale_y_continuous(breaks = c(0, 2e9, 4e9), - labels = c(0, "2bn", "4bn")) + + scale_y_continuous( + breaks = c(0, 2e9, 4e9), + labels = c(0, "2bn", "4bn") + ) + coord_cartesian(clip = "off") + theme(axis.text.x = element_blank()) + labs( @@ -289,10 +305,7 @@ stacked_bar_data |> #| fig.alt = "A bar chart with the bar for Brazil highlighted in dark blue and other bars in grey." pop_bar_data |> - ggplot( - aes(x = reorder(country, -pop), y = pop, - fill = country == "Brazil") - ) + + ggplot(aes(x = reorder(country, -pop), y = pop, fill = country == "Brazil")) + geom_col() + theme_af(legend = "none") + scale_y_continuous( @@ -304,7 +317,10 @@ pop_bar_data |> labs( x = NULL, y = NULL, - title = stringr::str_wrap("Brazil has the second highest population in the Americas", 40), + title = stringr::str_wrap( + "Brazil has the second highest population in the Americas", + 40 + ), subtitle = "Population of countries in the Americas (millions), 2007", caption = "Source: Gapminder" ) @@ -325,10 +341,12 @@ To make a `ggplot2` chart interactive, use `ggplotly()` from the `plotly` packag p <- pop_bar_data |> # Format text for tooltips - mutate(tooltip = paste0( - "Country: ", country, "\n", - "Life Expectancy: ", round(lifeExp, 1) - )) |> + mutate( + tooltip = paste0( + "Country: ", country, "\n", + "Life Expectancy: ", round(lifeExp, 1) + ) + ) |> ggplot(aes(x = reorder(country, -lifeExp), y = lifeExp, text = tooltip)) + geom_col(fill = af_colour_values["dark-blue"]) + theme_af(ticks = "x") + diff --git a/vignettes/cookbook/_colour-palettes.Rmd b/vignettes/cookbook/_colour-palettes.Rmd index 140dbb1..1ac86e8 100644 --- a/vignettes/cookbook/_colour-palettes.Rmd +++ b/vignettes/cookbook/_colour-palettes.Rmd @@ -24,9 +24,11 @@ gapminder |> geom_line(linewidth = 1) + theme_af(legend = "bottom") + scale_colour_discrete_af("main2") + - scale_y_continuous(limits = c(0, 82), - breaks = seq(0, 80, 20), - expand = c(0, 0)) + + scale_y_continuous( + limits = c(0, 82), + breaks = seq(0, 80, 20), + expand = c(0, 0) + ) + scale_x_continuous(breaks = seq(1952, 2007, 5)) + labs( x = "Year", @@ -55,9 +57,11 @@ gapminder |> ggplot(aes(x = year, y = lifeExp)) + geom_line(linewidth = 1, colour = my_palette[1]) + theme_af() + - scale_y_continuous(limits = c(0, 82), - breaks = seq(0, 80, 20), - expand = c(0, 0)) + + scale_y_continuous( + limits = c(0, 82), + breaks = seq(0, 80, 20), + expand = c(0, 0) + ) + scale_x_continuous(breaks = seq(1952, 2007, 5)) + labs( x = "Year", @@ -77,9 +81,11 @@ gapminder |> geom_line(linewidth = 1) + theme_af(legend = "bottom") + scale_colour_manual(values = my_palette) + - scale_y_continuous(limits = c(0, 82), - breaks = seq(0, 80, 20), - expand = c(0, 0)) + + scale_y_continuous( + limits = c(0, 82), + breaks = seq(0, 80, 20), + expand = c(0, 0) + ) + scale_x_continuous(breaks = seq(1952, 2007, 5)) + labs( x = "Year", diff --git a/vignettes/cookbook/_customisations.Rmd b/vignettes/cookbook/_customisations.Rmd index 6b8f03f..2cfb665 100644 --- a/vignettes/cookbook/_customisations.Rmd +++ b/vignettes/cookbook/_customisations.Rmd @@ -31,7 +31,8 @@ last_plot() + y = NULL, title = stringr::str_wrap( paste("The U.S.A. has the highest population in the Americas"), - width = 40), + width = 40 + ), subtitle = "Population of countries of the Americas, 2007", caption = "Source: Gapminder" ) @@ -77,9 +78,11 @@ Adaptive axis limits and break for `scale_x/y_continuous()` can be defined using limits_pretty <- function(x, ...) range(pretty(x, ...)) last_plot() + - scale_x_continuous(expand = expansion(mult = c(0, .1)), - breaks = pretty, - limits = limits_pretty) + scale_x_continuous( + expand = expansion(mult = c(0, .1)), + breaks = pretty, + limits = limits_pretty + ) ``` @@ -132,13 +135,19 @@ gapminder |> filter(country == "United Kingdom") |> ggplot(aes(x = year, y = lifeExp)) + geom_line(linewidth = 1, colour = af_colour_values[1]) + - geom_hline(yintercept = 75, colour = af_colour_values[2], - linewidth = 1, linetype = "dashed") + + geom_hline( + yintercept = 75, + colour = af_colour_values[2], + linewidth = 1, + linetype = "dashed" + ) + annotate(geom = "text", x = 2007, y = 70, label = "Age 70") + theme_af() + - scale_y_continuous(limits = c(0, 82), - breaks = seq(0, 80, 20), - expand = c(0, 0)) + + scale_y_continuous( + limits = c(0, 82), + breaks = seq(0, 80, 20), + expand = c(0, 0) + ) + scale_x_continuous(breaks = seq(1952, 2007, 5)) + labs( x = "Year", @@ -241,17 +250,23 @@ ann_data |> geom_line(linewidth = 1) + theme_af(legend = "none") + scale_colour_discrete_af() + - scale_y_continuous(limits = c(0, 82), - breaks = seq(0, 80, 20), - expand = c(0, 0)) + - scale_x_continuous(limits = c(1952, 2017), - breaks = seq(1952, 2017, 5)) + - geom_label(data = ann_labs, - aes(x = year, y = lifeExp, label = country, colour = country), - hjust = 0, - vjust = 0.5, - nudge_x = 0.5, - label.size = NA) + + scale_y_continuous( + limits = c(0, 82), + breaks = seq(0, 80, 20), + expand = c(0, 0) + ) + + scale_x_continuous( + limits = c(1952, 2017), + breaks = seq(1952, 2017, 5) + ) + + geom_label( + data = ann_labs, + aes(x = year, y = lifeExp, label = country, colour = country), + hjust = 0, + vjust = 0.5, + nudge_x = 0.5, + label.size = NA + ) + labs( x = "Year", y = NULL, From 0cbb404de2cd33bcf888947952d82513890a8e3c Mon Sep 17 00:00:00 2001 From: Olivia Box Power Date: Mon, 6 Jan 2025 17:37:18 +0000 Subject: [PATCH 17/17] Updates to cookbook --- README.Rmd | 42 +- README.md | 36 +- man/figures/README-ex1-1.svg | 675 +++++++++++-------------- man/figures/README-ex2-1.svg | 582 ++++++++++----------- man/figures/README-ex3-1.svg | 500 +++++++++--------- vignettes/cookbook/_annotations.Rmd | 39 +- vignettes/cookbook/_chart-types.Rmd | 53 +- vignettes/cookbook/_customisations.Rmd | 63 ++- vignettes/saving.Rmd | 14 +- 9 files changed, 970 insertions(+), 1034 deletions(-) diff --git a/README.Rmd b/README.Rmd index 366b99d..1f9d632 100644 --- a/README.Rmd +++ b/README.Rmd @@ -84,7 +84,7 @@ The easiest way to use afcharts is by adding `use_afcharts()` to the beginning o #### 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) @@ -94,58 +94,60 @@ gapminder |> filter(year == 2007 & continent == "Americas") |> slice_max(order_by = pop, n = 5) |> ggplot() + - geom_col(aes(x = reorder(country, -pop), y = pop/(10^6))) + - scale_y_continuous(expand = c(0, 0), - limits = c(0, 350), - breaks = c(seq(0, 350, 50)), - labels = c(seq(0, 300, 50), "350 million")) + + 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 = "The U.S.A. is the most populous country in\nthe Americas", - subtitle = "Population of countries in the Americas, 2007", + subtitle = "Population of countries in the Americas (millions), 2007", caption = "Source: Gapminder" ) ``` #### 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 == "Americas") |> slice_max(order_by = pop, n = 5) |> - ggplot(aes(x = reorder(country, -pop), y = pop/(10^6))) + + ggplot(aes(x = reorder(country, -pop), y = pop)) + geom_col(fill = af_colour_values["dark-blue"]) + - scale_y_continuous(expand = c(0, 0), - limits = c(0, 350), - breaks = c(seq(0, 350, 50)), - labels = c(seq(0, 300, 50), "350 million")) + + 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 = "The U.S.A. is the most populous country in\nthe Americas", - subtitle = "Population of countries in the Americas, 2007", + 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} +```{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) + - theme_af(legend = "bottom") + - scale_colour_discrete_af() + - scale_y_continuous(limits = c(0, 82), - breaks = seq(0, 80, 20), - expand = c(0, 0)) + + 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", diff --git a/README.md b/README.md index e5ffa6b..abb8908 100644 --- a/README.md +++ b/README.md @@ -87,17 +87,18 @@ gapminder |> filter(year == 2007 & continent == "Americas") |> slice_max(order_by = pop, n = 5) |> ggplot() + - geom_col(aes(x = reorder(country, -pop), y = pop/(10^6))) + - scale_y_continuous(expand = c(0, 0), - limits = c(0, 350), - breaks = c(seq(0, 350, 50)), - labels = c(seq(0, 300, 50), "350 million")) + + 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 = "The U.S.A. is the most populous country in\nthe Americas", - subtitle = "Population of countries in the Americas, 2007", + subtitle = "Population of countries in the Americas (millions), 2007", caption = "Source: Gapminder" ) ``` @@ -113,17 +114,18 @@ afcharts::use_afcharts() gapminder |> filter(year == 2007 & continent == "Americas") |> slice_max(order_by = pop, n = 5) |> - ggplot(aes(x = reorder(country, -pop), y = pop/(10^6))) + + ggplot(aes(x = reorder(country, -pop), y = pop)) + geom_col(fill = af_colour_values["dark-blue"]) + - scale_y_continuous(expand = c(0, 0), - limits = c(0, 350), - breaks = c(seq(0, 350, 50)), - labels = c(seq(0, 300, 50), "350 million")) + + 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 = "The U.S.A. is the most populous country in\nthe Americas", - subtitle = "Population of countries in the Americas, 2007", + subtitle = "Population of countries in the Americas (millions), 2007", caption = "Source: Gapminder" ) ``` @@ -140,11 +142,11 @@ gapminder |> filter(country %in% c("United Kingdom", "China")) |> ggplot(aes(x = year, y = lifeExp, colour = country)) + geom_line(linewidth = 1) + - theme_af(legend = "bottom") + - scale_colour_discrete_af() + - scale_y_continuous(limits = c(0, 82), - breaks = seq(0, 80, 20), - expand = c(0, 0)) + + 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", diff --git a/man/figures/README-ex1-1.svg b/man/figures/README-ex1-1.svg index c3f42a9..b88c09e 100644 --- a/man/figures/README-ex1-1.svg +++ b/man/figures/README-ex1-1.svg @@ -1,622 +1,563 @@ - + - + - + - + - + - + + - + - + - + - + - - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + + + + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - + - - - + + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - + - - - + + + - - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + - - - - - - - - - - - - - + + + + + + + + + + + + + - - - - - - + + + + + + - - - - - - + + + + + + - - - - - - - - + + + + + + + + - - - - - - - - - + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - + + + + + + + + + + + + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + diff --git a/man/figures/README-ex2-1.svg b/man/figures/README-ex2-1.svg index 16b52a9..58a11ef 100644 --- a/man/figures/README-ex2-1.svg +++ b/man/figures/README-ex2-1.svg @@ -1,495 +1,457 @@ - + - + - + - + - + - + + - + - + - + - + - - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - + - - - + + + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - + - - + - - - + + + - - - + + + - - - - + + + + + + + + + + + + + + - - - + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + - - - - - - + + + + + + - - - - - - + + + + + + + + - - - - - - - - + + + + + + + + + - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - + + + + + + + + + + + + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + diff --git a/man/figures/README-ex3-1.svg b/man/figures/README-ex3-1.svg index 9e34860..42a61a1 100644 --- a/man/figures/README-ex3-1.svg +++ b/man/figures/README-ex3-1.svg @@ -1,412 +1,412 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - + + + - + - + - + - + - + - - + + - + - - + + - - + + - - + + - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + - - - - + + + + - - - - + + + + - - - - + + + + - - - - + + + + - - - - + + + + - - - - + + + + - - - - + + + + - - - - + + + + - - - - + + + + - - - - + + + + - - - - + + + + - - - - + + + + - - - - - - - - + + + + + + + + - - - - - + + + + + - - - - - - - - - - - - - - + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + diff --git a/vignettes/cookbook/_annotations.Rmd b/vignettes/cookbook/_annotations.Rmd index b0b34a2..a92efd4 100644 --- a/vignettes/cookbook/_annotations.Rmd +++ b/vignettes/cookbook/_annotations.Rmd @@ -2,7 +2,7 @@ Labelling your chart is often preferable to using a legend, as often this relies on a user matching the legend to the data using colour alone. The legend can be removed from a chart by setting `legend = "none"` in `theme_af()`. -The easiest way to add an annotation is to manually define the co-ordinates of the required position. +The easiest way to add an annotation is to manually define the co-ordinates of the required position. Note that black text has been used for the labels, as this ensures sufficient contrast against the white background. ```{r annotations-data} ann_data <- gapminder |> @@ -10,17 +10,20 @@ ann_data <- gapminder |> ``` ```{r annotations-1} -#| fig.alt = "A multiple line chart with colour co-ordinated line labels using afcharts theme and main palette." +#| fig.alt = "A multiple line chart with line labels." ann_data |> - ggplot(aes(x = year, y = lifeExp, colour = country)) + - geom_line(linewidth = 1) + + ggplot(aes(x = year, y = lifeExp)) + + geom_line( + aes(colour = country), + linewidth = 1 + ) + theme_af(legend = "none") + scale_colour_discrete_af() + scale_y_continuous( limits = c(0, 82), breaks = seq(0, 80, 20), - expand = c(0, 0) + expand = expansion(mult = c(0, 0.1)) ) + scale_x_continuous( limits = c(1952, 2017), @@ -30,7 +33,6 @@ ann_data |> geom = "label", x = 2008, y = 73, label = "China", - colour = af_colour_values[1], label.size = NA, hjust = 0, vjust = 0.5 @@ -40,7 +42,6 @@ ann_data |> x = 2008, y = 79.4, label = "United Kingdom", - colour = af_colour_values[2], label.size = NA, hjust = 0, vjust = 0.5 @@ -62,7 +63,7 @@ Note that in the previous examples, `annotate()` also requires a geom (`label` o ```{r annotations-2} -#| fig.alt = "A multiple line chart with colour co-ordinated line labels using afcharts theme and main2 palette." +#| fig.alt = "A multiple line chart with line labels." ann_labs <- ann_data |> group_by(country) |> @@ -71,14 +72,17 @@ ann_labs <- ann_data |> ungroup() ann_data |> - ggplot(aes(x = year, y = lifeExp, colour = country)) + - geom_line(linewidth = 1) + + ggplot(aes(x = year, y = lifeExp)) + + geom_line( + aes(colour = country), + linewidth = 1 + ) + theme_af(legend = "none") + scale_colour_discrete_af() + scale_y_continuous( limits = c(0, 82), breaks = seq(0, 80, 20), - expand = c(0, 0) + expand = expansion(mult = c(0, 0.1)) ) + scale_x_continuous( limits = c(1952, 2017), @@ -86,7 +90,7 @@ ann_data |> ) + geom_label( data = ann_labs, - aes(x = year, y = lifeExp, label = country, colour = country), + aes(x = year, y = lifeExp, label = country), hjust = 0, vjust = 0.5, nudge_x = 0.5, @@ -107,12 +111,17 @@ Annotations may also be used to add value labels to a bar chart. Note that `geom #| fig.alt = "A bar chart with white text labels at the end of each bar." ggplot(pop_bar_data, aes(x = reorder(country, -pop), y = pop)) + geom_col(fill = af_colour_values["dark-blue"]) + - geom_text(aes(label = round(pop/1E6, 1),), vjust = 1.2, colour = "white") + + geom_text( + aes(label = round(pop / 1E6, 1),), + vjust = 1.2, + colour = "white" + ) + theme_af() + scale_y_continuous( - expand = c(0, 0), limits = c(0, 350E6), - labels = scales::label_number(scale = 1E-6)) + + labels = scales::label_number(scale = 1E-6), + expand = expansion(mult = c(0, 0.1)) + ) + labs( x = NULL, y = NULL, diff --git a/vignettes/cookbook/_chart-types.Rmd b/vignettes/cookbook/_chart-types.Rmd index 462a389..3a4c6e9 100644 --- a/vignettes/cookbook/_chart-types.Rmd +++ b/vignettes/cookbook/_chart-types.Rmd @@ -13,7 +13,7 @@ gapminder |> scale_y_continuous( limits = c(0, 82), breaks = seq(0, 80, 20), - expand = c(0, 0) + expand = expansion(mult = c(0, 0.1)) ) + scale_x_continuous(breaks = seq(1952, 2007, 5)) + labs( @@ -39,7 +39,7 @@ gapminder |> scale_y_continuous( limits = c(0, 82), breaks = seq(0, 80, 20), - expand = c(0, 0) + expand = expansion(mult = c(0, 0.1)) ) + scale_x_continuous(breaks = seq(1952, 2007, 5)) + labs( @@ -59,8 +59,8 @@ An example with line labels and no legend can be found in the [Adding annotation ```{r bar-data} pop_bar_data <- gapminder |> - filter(year == 2007 & continent == "Americas") |> - slice_max(order_by = pop, n = 5) + filter(year == 2007 & continent == "Americas") |> + slice_max(order_by = pop, n = 5) ``` ```{r bar-chart-1} @@ -70,9 +70,9 @@ ggplot(pop_bar_data, aes(x = reorder(country, -pop), y = pop)) + geom_col(fill = af_colour_values["dark-blue"]) + theme_af() + scale_y_continuous( - expand = c(0, 0), limits = c(0, 350E6), - labels = scales::label_number(scale = 1E-6) + labels = scales::label_number(scale = 1E-6), + expand = expansion(mult = c(0, 0.1)), ) + labs( x = NULL, @@ -92,9 +92,9 @@ ggplot(pop_bar_data, aes(x = pop, y = reorder(country, pop))) + geom_col(fill = af_colour_values["dark-blue"]) + theme_af(grid = "x", axis = "y") + scale_x_continuous( - expand = c(0, 0), limits = c(0, 350E6), - labels = scales::label_number(scale = 1E-6) + labels = scales::label_number(scale = 1E-6), + expand = expansion(mult = c(0, 0.1)) ) + labs( x = NULL, @@ -129,10 +129,10 @@ ggplot( ) + geom_bar(stat = "identity", position = "dodge") + scale_y_continuous( - expand = c(0, 0), limits = c(0, 100), breaks = c(seq(0, 100, 20)), - labels = c(seq(0, 100, 20)) + labels = c(seq(0, 100, 20)), + expand = expansion(mult = c(0, 0.1)) ) + theme_af(legend = "bottom") + scale_fill_discrete_af() + @@ -174,7 +174,10 @@ ggplot( ) + geom_bar(stat = "identity", position = "fill") + theme_af(legend = "bottom") + - scale_y_continuous(expand = c(0, 0), labels = scales::percent) + + scale_y_continuous( + labels = scales::percent, + expand = expansion(mult = c(0, 0.1)) + ) + coord_cartesian(clip = "off") + scale_fill_discrete_af() + labs( @@ -203,10 +206,9 @@ gapminder |> ) + theme_af() + scale_y_continuous( - expand = c(0, 0), limits = c(0, 35), breaks = c(seq(0, 35, 5)), - labels = c(seq(0, 35, 5)) + expand = expansion(mult = c(0, 0.1)) ) + labs( x = NULL, @@ -225,15 +227,13 @@ gapminder |> gapminder |> filter(year == 2007) |> - ggplot(aes(x = gdpPercap, y = lifeExp, size = pop)) + + ggplot(aes(x = gdpPercap, y = lifeExp)) + geom_point(colour = af_colour_values["dark-blue"]) + theme_af(axis = "none", grid = "xy") + - scale_x_continuous(labels = function(x) scales::dollar(x, prefix = "£")) + - scale_size_continuous(labels = scales::comma) + + scale_x_continuous(labels = scales::label_comma()) + labs( - x = "GDP", + x = "GDP (US$, inflation-adjusted)", y = "Life\nExpectancy", - size = "Population", title = stringr::str_wrap( "The relationship between GDP and Life Expectancy is complex", 40 @@ -309,9 +309,9 @@ pop_bar_data |> geom_col() + theme_af(legend = "none") + scale_y_continuous( - expand = c(0, 0), limits = c(0, 350E6), - labels = scales::label_number(scale = 1E-6) + labels = scales::label_number(scale = 1E-6), + expand = expansion(mult = c(0, 0.1)) ) + scale_fill_discrete_af("focus", reverse = TRUE) + labs( @@ -344,22 +344,21 @@ p <- mutate( tooltip = paste0( "Country: ", country, "\n", - "Life Expectancy: ", round(lifeExp, 1) + "Population (millions): ", round(pop / 10 ^ 6 , 1) ) ) |> - ggplot(aes(x = reorder(country, -lifeExp), y = lifeExp, text = tooltip)) + + ggplot(aes(x = reorder(country, -pop), y = pop, text = tooltip)) + geom_col(fill = af_colour_values["dark-blue"]) + theme_af(ticks = "x") + theme(text = element_text(family = "")) + scale_y_continuous( - expand = c(0, 0), - limits = c(0, 100), - breaks = c(seq(0, 100, 20)), - labels = c(seq(0, 100, 20)) + limits = c(0, 350E6), + labels = scales::label_number(scale = 1E-6), + expand = expansion(mult = c(0, 0.1)) ) + labs( x = NULL, - y = NULL + y = "Population (millions)" ) plotly::ggplotly(p, tooltip = "text") |> diff --git a/vignettes/cookbook/_customisations.Rmd b/vignettes/cookbook/_customisations.Rmd index 2cfb665..b436845 100644 --- a/vignettes/cookbook/_customisations.Rmd +++ b/vignettes/cookbook/_customisations.Rmd @@ -4,12 +4,12 @@ ### Sorting a bar chart -To control the order of bars in a chart, wrap the variable you want to arrange with `reorder()` and specify what variable you want to sort by. The following example sorts bars in ascending order of life expectancy. To sort in descending order, you would change this to `reorder(country, desc(lifeExp))`. +To control the order of bars in a chart, wrap the variable you want to arrange with `reorder()` and specify what variable you want to sort by. The following example sorts bars in ascending order of population. To sort in descending order, you would change this to `reorder(country, desc(pop))`. ```{r sorting} #| fig.alt = "A bar chart using afcharts theme and dark blue colour with bars sorted in decreasing order by life expectancy." -pop_bar_data |> +population_chart <- pop_bar_data |> ggplot(aes(x = pop, y = reorder(country, pop))) + geom_col(fill = af_colour_values["dark-blue"]) + theme_af(axis = "y", grid = "x") @@ -25,7 +25,7 @@ Chart titles such as the main title, subtitle, caption, axis titles and legend t ```{r chart-titles} #| fig.alt = "A bar chart with title, subtitle and caption. Axis titles have been removed." -last_plot() + +population_chart + labs( x = NULL, y = NULL, @@ -33,7 +33,7 @@ last_plot() + paste("The U.S.A. has the highest population in the Americas"), width = 40 ), - subtitle = "Population of countries of the Americas, 2007", + subtitle = "Population of countries of the Americas (millions), 2007", caption = "Source: Gapminder" ) ``` @@ -46,7 +46,8 @@ By default, a bar chart will have a gap between the bottom of the bars and the a ```{r expand} #| fig.alt = "A bar chart with no space between bottom of bars and x axis." -last_plot() + scale_x_continuous(expand = c(0, 0)) +population_chart + + scale_x_continuous(expand = expansion(mult = c(0, 0.1))) ``` The equivalent adjustment can be made for the y axis using `scale_y_continuous`. @@ -61,27 +62,34 @@ Limits, breaks and labels can be defined with custom values. ```{r axis-limits-breaks-labels-custom} #| fig.alt = "A bar chart with fewer x axis breaks and edited labels." -last_plot() + +population_chart + scale_x_continuous( - expand = c(0, 0), - limits = c(0, 350E6), - labels = scales::label_number(scale = 1E-6)) + breaks = seq(0, 400E6, 100E6), + labels = seq(0, 400, 100), + limits = c(0, 420E6), + expand = expansion(mult = c(0, 0.1)) + ) + + labs( + x = "Population (millions)" + ) ``` -Note that further calls to `scale_x/y_continuous` will overwrite previous calls, hence why `expand = c(0, 0)` has been included again in this example. - -Adaptive axis limits and break for `scale_x/y_continuous()` can be defined using the `pretty` function. This defines breaks that are equally spaced ‘round’ values which cover the range of the data and limits that are the next 'round' value just exceeding the range of the data. +Adaptive axis limits and break for `scale_x/y_continuous()` can be defined using the `pretty` function. This defines breaks that are equally spaced ‘round’ values which cover the range of the data and limits that are the next 'round' value just exceeding the range of the data. Setting the limits with a custom `limits_pretty` function ensures the highest gridline value is above the maximum value of the data. ```{r axis-limits-breaks-labels-fct} #| fig.alt = "A bar chart with 'round' breaks and limits using the pretty() function." -limits_pretty <- function(x, ...) range(pretty(x, ...)) +limits_pretty <- function(x) range(pretty(x)) -last_plot() + +population_chart + scale_x_continuous( - expand = expansion(mult = c(0, .1)), - breaks = pretty, - limits = limits_pretty + breaks = \(x) pretty(x), + labels = label_number(scale = 1E-6), + limits = limits_pretty, + expand = expansion(mult = c(0, 0.2)) + ) + + labs( + x = "Population (millions)" ) ``` @@ -100,7 +108,10 @@ stacked_bar_data |> ggplot(aes(x = continent, y = n_countries, fill = lifeExpGrouped)) + geom_bar(stat = "identity", position = "fill") + theme_af(legend = "bottom") + - scale_y_continuous(expand = c(0, 0), labels = scales::percent) + + scale_y_continuous( + expand = expansion(mult = c(0, 0.1)), + labels = scales::percent + ) + scale_fill_discrete_af() + labs( x = NULL, @@ -146,7 +157,7 @@ gapminder |> scale_y_continuous( limits = c(0, 82), breaks = seq(0, 80, 20), - expand = c(0, 0) + expand = expansion(mult = c(0, 0.1)) ) + scale_x_continuous(breaks = seq(1952, 2007, 5)) + labs( @@ -170,7 +181,7 @@ plot <- ggplot(pop_bar_data, aes(x = reorder(country, -pop), y = pop)) + geom_col(fill = af_colour_values["dark-blue"]) + theme_af() + - scale_y_continuous(expand = c(0, 0)) + + scale_y_continuous(expand = expansion(mult = c(0, 0.1))) + labs( x = NULL, subtitle = "Population of countries in the Americas (millions), 2007", @@ -212,10 +223,12 @@ If you find you need to adjust theme elements for your chart, this can be done u ggplot(pop_bar_data, aes(x = reorder(country, -pop), y = pop)) + geom_col(fill = af_colour_values["dark-blue"]) + theme_af(axis = "xy") + - theme(axis.line = element_line(colour = "black"), - axis.ticks = element_line(colour = "black")) + + theme( + axis.line = element_line(colour = "black"), + axis.ticks = element_line(colour = "black") + ) + scale_y_continuous( - expand = c(0, 0), + expand = expansion(mult = c(0, 0.1)), limits = c(0, 350E6), labels = scales::label_number(scale = 1E-6) ) + @@ -253,7 +266,7 @@ ann_data |> scale_y_continuous( limits = c(0, 82), breaks = seq(0, 80, 20), - expand = c(0, 0) + expand = expansion(mult = c(0, 0.1)) ) + scale_x_continuous( limits = c(1952, 2017), @@ -261,7 +274,7 @@ ann_data |> ) + geom_label( data = ann_labs, - aes(x = year, y = lifeExp, label = country, colour = country), + aes(x = year, y = lifeExp, label = country), hjust = 0, vjust = 0.5, nudge_x = 0.5, diff --git a/vignettes/saving.Rmd b/vignettes/saving.Rmd index 05361fe..e643662 100644 --- a/vignettes/saving.Rmd +++ b/vignettes/saving.Rmd @@ -48,7 +48,7 @@ A chart should follow the flow of the content surrounding it. It should be rough The dimensions of your chart will depend on where you plan to use it. For example, if you're using a page-based document such as Microsoft Word, and would like to include a landscape image on an A4 portrait page, a width of 159 mm and height of 100 mm works well. -When using the SVG file format, actual saved image dimensions will be exactly as specified. However, when using JPG or PNG formats, they may not be. This is because these file formats are also influenced by DPI (dots per inch). DPI is a measure of the resolution of an image; the higher the DPI, the more crisp and good quality your image will be. This is another good reason to use SVG over these formats, however if you are limited to JPG or PNG, it may take some trial and error to find the correct combination of width, height and DPI. +When using JPG or PNG formats, we need to consider the DPI (dots per inch). DPI is a measure of the resolution of an image; the higher the DPI, the more crisp and good quality your image will be. ## Examples @@ -58,18 +58,26 @@ When using the SVG file format, actual saved image dimensions will be exactly as The `ggsave()` function from `ggplot2` can be used to save a standalone image file. To specify the SVG file format, simply ensure your file name ends with `.svg`. The following example uses the recommended dimensions from the previous section. ```{r standalone-svg, eval = FALSE} -ggsave(myplot, filename = "example-plot.svg", +ggsave(filename = "example-plot.svg", plot = myplot, width = 159, height = 100, units = "mm") ``` For a JPG or PNG image, also consider the DPI value. In `ggsave`, this can be supplied as a numerical value, or as a string input. For best quality, use "retina". ```{r standalong-png, eval = FALSE} -ggsave(myplot, filename = "example-plot.png", +ggsave(filename = "example-plot.png", plot = myplot, width = 159, height = 100, units = "mm", dpi = "retina") ``` +To save images at the correct size for publishing on GOVUK, afcharts provides +the function `save_govuk`. This function can be used to save a SVG, JPG or PNG image. + +```{r savegovuk, eval = FALSE} +save_govuk(filename = "example-plot.png" plot = myplot, device = "svg" ) +``` + + ### R Markdown For charts in an R Markdown document, file format, dimensions and other settings can be set in the code chunk options.