How can I use a custome colour fill (viridis) when also using the aes(colour= variable)

1.6k views Asked by At

I am trying to implement the viridis package using viridis:: + scale_fill_viridis().

This is my code:

ggplot(combi_plot, aes(x = day_of_symptoms, y = ct_value)) + geom_smooth(aes(colour=gender)) + 
xlim(-5,20)  + scale_fill_viridis()

However, this probably interferes with my use of aes(colour=gender), which draws a seperate line for the two genders in my dataset, using 2 different colours of the ggplot2 basic palet.

My question is: How do you use a custom color palette such as viridis, in combination with the aes(colour="variable") argument.

Cheers

1

There are 1 answers

3
J. W. Rozelle On

So, it's a little difficult to do this without your data. I'm also not entirely sure what, specifically, you're trying to color. I'm assuming it's the gender.

I set a color palette (I like wespalette, so here's a wespalette example, but you could do the same with virdis, getting a vector of specific colors you want to use).

    # load color palette
palette_c <- wes_palette("FantasticFox1", 5)

Then, I pull out the colors I want to use. Rather than the scale_..._virdis, you might want to try the built in functions.

#Add colors
      + scale_color_manual(values = c("Female" = palette_c[5], "Male" = palette_c[4]))

Also, might have to move the color outside of the aes, but I'm not sure what the structure of your data is, so your mileage may vary. So, potentially start tinkering with something like:

ggplot(combi_plot, aes(x = day_of_symptoms, y = ct_value)) + geom_smooth(color=gender) + 
xlim(-5,20) + scale_color_manual(values = c("Female" = palette_c[5], "Male" = palette_c[4]))