Skip to content

Commit

Permalink
Minor update to cookbook code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivia-Box-Power committed Jan 6, 2025
1 parent 48c841f commit 85a9d94
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 81 deletions.
69 changes: 45 additions & 24 deletions vignettes/cookbook/_annotations.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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") +

Check warning on line 110 in vignettes/cookbook/_annotations.Rmd

View workflow job for this annotation

GitHub Actions / lint

file=vignettes/cookbook/_annotations.Rmd,line=110,col=34,[infix_spaces_linter] Put spaces around all infix operators.

Check warning on line 110 in vignettes/cookbook/_annotations.Rmd

View workflow job for this annotation

GitHub Actions / lint

file=vignettes/cookbook/_annotations.Rmd,line=110,col=43,[commas_linter] Commas should always have a space after.
theme_af() +
scale_y_continuous(
expand = c(0, 0),

Check warning on line 113 in vignettes/cookbook/_annotations.Rmd

View workflow job for this annotation

GitHub Actions / lint

file=vignettes/cookbook/_annotations.Rmd,line=113,col=4,[indentation_linter] Hanging indent should be 21 spaces but is 4 spaces.
Expand All @@ -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"
)
Expand Down
74 changes: 46 additions & 28 deletions vignettes/cookbook/_chart-types.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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),
Expand Down Expand Up @@ -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) +
Expand All @@ -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),
Expand Down Expand Up @@ -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"
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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"
)
Expand All @@ -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") +
Expand Down
24 changes: 15 additions & 9 deletions vignettes/cookbook/_colour-palettes.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
55 changes: 35 additions & 20 deletions vignettes/cookbook/_customisations.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
)
```

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 85a9d94

Please sign in to comment.