简体   繁体   中英

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.

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM