Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Data Science
  3. Question
Data Science

How can we color ggplot by factor and gradient?

Asked by Isha Patil Nov 30, -0001 1.2K views 1 answer
Share

Your answer

1 Answer

Isha Patil Latest answer

Answered on Nov 30, -0001

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




Was this helpful?

More Data Science discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Data Science Blogs

Guides, tips and career advice on Data Science from JanBask experts.