How can we color ggplot by factor and gradient?
The approach we can usually use is to manipulate the factor values so we can plug them into the hcl() function.
First, some raw data:
library(tidyverse)
raw_data <-
diamonds %>%
filter(price < 500>%
mutate(
factor = factor(color),
intensity = cut,
interaction = paste(factor, intensity)
)
Next use this kind of wrangling to get hex colors:
Now we can plot it
ggplot(df, aes(x, y, color = interaction)) +
geom_count() +
facet_wrap(~factor) +
scale_color_manual(
values = color_values$hex,
labels = color_values$interaction
) +
guides(color = guide_legend(override.aes = list(size = 5)))