Description Usage Arguments Value Examples
Makes a palette for dataframe where levels within groups defined by the
group
variable share the same hue but different shades, levels within
group based on the shade
variable.
1 2 3 4 5 6 7 8 9 |
df |
dataframe with at least two variables (treated as categories) |
group |
variable name used to assign hues |
shade |
variable name used to assign colour shades of same hue |
maxShades |
maximum allowed number of shades per hue |
hues |
hues available to use for unique levels of |
huePalFun |
function used to create single hue palette for levels of |
manual |
NULL or manual additions or replacements for returned palette
in the style of |
named character vector of colours
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | library(dplyr)
library(ggplot2)
# sort countries, within continents, by average population
sortedSummary <- gapminder::gapminder %>%
group_by(continent, country) %>%
summarise(AvPop = mean(pop, na.rm = TRUE), .groups = "keep") %>%
group_by(continent) %>%
arrange(.by_group = TRUE, desc(AvPop))
# create palettes
countryPal6 <- sortedSummary %>%
hueGroupPal(group = "continent", shade = "country", maxShades = 6)
# plot population per year
gapminder::gapminder %>%
group_by(year) %>%
ggplot(aes(
x = factor(year), y = pop,
# setting as factor with levels in correct order ensures ordering of bars
fill = factor(country, levels = names(countryPal6))
)) +
geom_col() +
guides(fill = "none") +
# setting manual scale of course sets correct colours
scale_fill_manual(values = countryPal6) +
ggfittext::geom_fit_text(
aes(ymin = 0, ymax = pop, label = country),
position = "stack", colour = "white"
) +
theme_classic() +
coord_cartesian(expand = FALSE)
# plot population per year as share of world total that year
gapminder::gapminder %>%
group_by(year) %>%
mutate(popPerc = pop/sum(pop, na.rm = TRUE)) %>%
ggplot(aes(
x = factor(year), y = popPerc,
# setting as factor with levels in correct order ensures ordering of bars
fill = factor(country, levels = names(countryPal6))
)) +
geom_col() +
guides(fill = "none") +
# setting manual scale of course sets correct colours
scale_fill_manual(values = countryPal6) +
ggfittext::geom_fit_text(
aes(ymin = 0, ymax = popPerc, label = country),
position = "stack", colour = "white"
) +
theme_classic() +
coord_cartesian(expand = FALSE)
# plot with modified palette
countryPal6alt <- sortedSummary %>%
hueGroupPal(group = "continent", shade = "country", maxShades = 6,
hues = hueSet(start = 0))
gapminder::gapminder %>%
group_by(year) %>%
mutate(popPerc = pop/sum(pop, na.rm = TRUE)) %>%
# dplyr::filter(year > 1970) %>%
ggplot(aes(
x = factor(year), y = popPerc,
# setting as factor with levels in correct order ensures ordering of bars
fill = factor(country, levels = names(countryPal6alt))
)) +
geom_col() +
guides(fill = "none") +
# setting manual scale of course sets correct colours
scale_fill_manual(values = countryPal6alt) +
ggfittext::geom_fit_text(grow = TRUE,
aes(ymin = 0, ymax = popPerc, label = country),
position = "stack", colour = "white"
) +
theme_classic() +
coord_cartesian(expand = FALSE)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.