Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

use two different viridis colour palettes in a single plot

I would like to use two different viridis colour palettes with scale_color_viridis_c to show differences between the two different groups (a and b). Is there a way to change my code below in a way that would allow this?

example plot (working):

data.frame(site  = sample(LETTERS[1:4], 10000, replace = TRUE),
           group = sample(letters[1:2], 10000, replace = TRUE),
           x     = rnorm(10000, 20, 5)) %>% 
  mutate(y = x + rnorm(10000, 0, 1),
         z = rnorm(10000, 0, 1)) %>% 
  ggplot(aes(x, y, col = z)) + geom_point() + facet_wrap(~site) +
  scale_color_viridis_c(option = "plasma")

something like:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

scale_color_viridis_c(option = c("a" = "plasma", "b" = "viridis")

>Solution :

I suggest the ggnewscale package. You will need to filter/split the dataset for each group "a" and "b", with a geom_point() call for each, and a new_scale_color() in between.

library(tidyverse)
library(ggnewscale)

data.frame(site  = sample(LETTERS[1:4], 10000, replace = TRUE),
           group = sample(letters[1:2], 10000, replace = TRUE),
           x     = rnorm(10000, 20, 5)) %>% 
  mutate(y = x + rnorm(10000, 0, 1),
         z = rnorm(10000, 0, 1)) %>% 
  ggplot(aes(x, y)) + geom_point(data = . %>%  filter(group =="a"), aes(col = z)) + facet_wrap(~site) +
  scale_color_viridis_c(option = "plasma") +
  labs(col= "z group a")+
  new_scale_color() +
  geom_point(data = . %>%  filter(group =="b"), aes(col = z)) + 
  scale_color_viridis_c(option = "viridis")+
    labs(col= "z group b")

Created on 2024-05-28 with reprex v2.1.0

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading