How can we color ggplot by factor and gradient?

670    Asked by IshaPatil in Data Science , Asked on Nov 30, -0001
Answered by Isha Patil

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)))





Your Answer

Interviews

Parent Categories