2017-08-24 94 views
2

我正在尝试使用scale_color_brewer(direction = -1)来颠倒图的颜色映射。但是,这样做也会更改调色板。如何颠倒ggplot2的默认调色板

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 = ?) 
+0

已更新标题 – Ben

+0

没有答案,但默认比例是“scale_color_discrete”,而不是颜色啤酒比例。它调用'discrete_scale'指定调色板为'scales :: hue_pal',并且* does *采用'direction'参数,但是添加'scale_color_discrete(direction = -1)'不仅仅是改变调色板。现在没有时间挖掘更多... – Gregor

回答

5

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

顺便说一句,为了让分类变量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') 
0

我会用scale_color_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'))) 
1

我们可以使用hue_pal功能从scales包来获得颜色的名称。之后,使用scale_color_manual指定颜色与rev颠倒颜色的顺序从hue_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))