我有一个有序的因子变量,我想使用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()
不起作用。
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)
{ggplot2}的最后一个版本(dev: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()