hueGroupPal: Make HCL palette for groups with multiple levels

Description Usage Arguments Value Examples

View source: R/hueGroupPal.R

Description

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.

Usage

1
2
3
4
5
6
7
8
9
hueGroupPal(
  df,
  group,
  shade,
  maxShades = 5,
  hues = hueSet(),
  huePalFun = huePal(),
  manual = c(Other = "lightgrey")
)

Arguments

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 group variable

huePalFun

function used to create single hue palette for levels of shade variable

manual

NULL or manual additions or replacements for returned palette in the style of c(name = value, ....)

Value

named character vector of colours

Examples

 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)

david-barnett/hueR documentation built on Dec. 19, 2021, 9:03 p.m.