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,