R 如何反转ggplot2的默认调色板

R 如何反转ggplot2的默认调色板,r,ggplot2,R,Ggplot2,我正在尝试使用scale\u color\u 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_po

我正在尝试使用
scale\u color\u 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 = ?)

我会使用
scale\u color\u manual()
进行更多控制。这里有两个版本的反向颜色贴图

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species))+geom_point()+
+     scale_color_manual(values = RColorBrewer::brewer.pal(3,'Blues'))

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species))+geom_point()+
+     scale_color_manual(values = rev(RColorBrewer::brewer.pal(3,'Blues')))

ggplot使用的默认调色板是
缩放\u颜色\u色调

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

顺便说一句,为了让
scale\u color\u brewer
处理分类变量,您需要设置
type='qual'

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

我们可以使用
scales
软件包中的
hue\u pal
功能来获取颜色名称。之后,使用
scale\u color\u manual
使用
rev
指定颜色,从
hue\u pal
反转颜色顺序

library(ggplot2)
library(scales)

# Get the colors with 3 classes
cols <- hue_pal()(3)

# Plot the data and reverse the color
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
  geom_point() +
  scale_color_manual(values = rev(cols))
库(ggplot2)
图书馆(比例尺)
#使用3个类获取颜色

cols更新了标题,但没有答案,但默认比例是
比例\u颜色\u离散
,而不是颜色啤酒比例。它调用
discrete\u scale
将调色板指定为
scales::hue\u pal
,并使用
direction
参数,但添加
scale\u color\u discrete(direction=-1)
会改变调色板,而不仅仅是反转。现在没有时间深入研究…要反转默认的连续颜色比例,可以使用类似于反转连续轴比例的
scale\u color\u continuous(trans='reverse')