Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

discretizing viridis ggplot color scale

I have an ordered factor variable that I would like to plot using ggplot2. Is there any way I can use scale_color_viridis(), a continuous color scale, with this ordered factor without casting the factor to numeric? The straightforward

iris$Sepal.Width <- ordered(iris$Sepal.Width)

ggplot(iris, aes(Sepal.Length, Petal.Length, color=Sepal.Width)) + 
  geom_point() + 
  scale_color_continuous()

doesn't work.

like image 580
RoyalTS Avatar asked Mar 10 '17 21:03

RoyalTS


2 Answers

Viridis has a discrete = TRUE option.

iris$Sepal.Width <- ordered(iris$Sepal.Width)

ggplot(iris, aes(Sepal.Length, Petal.Length, color=Sepal.Width)) + 
geom_point() + 
viridis::scale_color_viridis(discrete = TRUE)
like image 175
detroyejr Avatar answered Oct 25 '22 11:10

detroyejr


Last version of {ggplot2} (dev: 2.2.1.9000) now has a viridis scale included.
You can use scale_colour_viridis_d() for discrete values or scale_fill_viridis_c() for continuous values.

In your case :

iris$Sepal.Width <- ordered(iris$Sepal.Width)

ggplot(iris, aes(Sepal.Length, Petal.Length, color=Sepal.Width)) + 
  geom_point() + 
  scale_colour_viridis_d()
like image 40
Sébastien Rochette Avatar answered Oct 25 '22 13:10

Sébastien Rochette