5

我有一个有序因子变量,我想使用ggplot2. 有什么方法可以使用scale_color_viridis()连续色标,使用这个有序因子而不将因子转换为数字?直截了当

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

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

不起作用。

4

2 回答 2

12

Viridis 有一个discrete = TRUE选择。

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)
于 2017-03-10T21:46:40.817 回答
3

{ggplot2} 的最新版本(开发:2.2.1.9000)现在包含了一个 viridis 规模。
您可以scale_colour_viridis_d()用于离散值或scale_fill_viridis_c()连续值。

在你的情况下:

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

ggplot(iris, aes(Sepal.Length, Petal.Length, color=Sepal.Width)) + 
  geom_point() + 
  scale_colour_viridis_d()
于 2018-04-11T15:32:06.683 回答