A discrete colour scale for dividing where there is a natural ordered subgrouping into groups and subgroups
Source:R/zz-api-ggplot-utils.R
scale_colour_subtype.Rd
This is intended to combine with 'scale_fill_subtype' when we want to divide major groupings differently to minor groups
Usage
scale_colour_subtype(
subclasses,
class_colour = "black",
subclass_colour = "grey50",
na.value = "grey50",
aesthetics = "color",
...
)
Arguments
- subclasses
a vector containing the count of the subcategories, e.g. c(2,3,4) defines 3 major categories and a total of 9 sub-categories
- class_colour
the colour for major group divisions
- subclass_colour
the colour for sub group divisions
- na.value
missing value colour
- aesthetics
this only really makes sense for color scales.
- ...
passed on to ggplot2::discrete_scale()
Examples
library(tidyverse)
# prep some data:
data = ggplot2::diamonds %>%
dplyr::mutate(color_cut = sprintf("%s (%s)",color,cut)) %>%
dplyr::group_by(color,cut,color_cut) %>%
dplyr::count() %>%
dplyr::ungroup() %>%
dplyr::mutate(color_cut = ordered(color_cut))
# work out the number of subgroups for each group:
subgroups = data %>%
dplyr::select(color,cut) %>%
dplyr::distinct() %>%
dplyr::group_by(color) %>%
dplyr::count() %>%
dplyr::pull(n)
# plot as a horizontal stacked bar chart using color brewer as the main
# colour axis. N.b. having enough different colours here is important
ggplot2::ggplot(data, ggplot2::aes(y=1,x=n, fill=color_cut, color=color_cut))+
ggplot2::geom_bar(stat="identity",orientation = "y")+
ggrrr::scale_fill_subtype(.palette = scales::brewer_pal,
palette="Accent", subclasses = subgroups)+
ggrrr::scale_colour_subtype(subclasses=subgroups)+
ggrrr::gg_hide_Y_axis()+
ggrrr::gg_narrow()