gpt4 book ai didi

r - 如何反转 ggplot2 的默认调色板

转载 作者:行者123 更新时间:2023-12-02 07:03:33 26 4
gpt4 key购买 nike

我正在尝试使用 scale_color_brewer(direction = -1) 反转绘图的颜色图。然而,这样做也会改变调色板。

library(ggplot2)
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species))+geom_point()

# reverse colors
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species))+geom_point()+
scale_color_brewer(direction = -1)

潜在的解决方案

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species))+geom_point()+
scale_color_brewer(direction = -1, palette = ?)

最佳答案

ggplot 使用的默认调色板是 scale_color_hue

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species))+geom_point()

相当于

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
geom_point() + scale_color_hue(direction = 1)

direction = -1 确实反转颜色。但随后您需要调整色相轮中的起点,以便以相反的顺序获得相同的三种颜色。

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species))+geom_point()+
scale_color_hue(direction = -1, h.start=90)

每种颜色将色相指针移动 30 度。所以我们将起点设置为 90。

hue wheel

顺便说一句,为了让 scale_colour_brewer 适用于分类变量,您需要设置 type = 'qual':

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species))+geom_point()+
scale_color_brewer(type = 'qual', palette = 'Dark2')

关于r - 如何反转 ggplot2 的默认调色板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45868625/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com