如何反转ggplot2的默认调色板 [英] How to reverse the default color palette for ggplot2

查看:372
本文介绍了如何反转ggplot2的默认调色板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用scale_color_brewer(direction = -1)反转绘图的颜色图.但是,这样做也会更改调色板.

I am trying to reverse the color map for a plot using scale_color_brewer(direction = -1). However, doing so also changes the palette.

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.

The default color palette ggplot uses is 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确实可以反转颜色.但是然后您需要调整色相轮中的起点,以便以相反的顺序获得相同的三种颜色.

direction = -1 does reverse the colors. But then you need to adjust the starting point in the hue wheel, in order to get the same three colors in reversed order.

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

每种颜色将色相指针移动30度.因此,我们将起点设置为90.

Each color moves the hue pointer 30 degrees. So we set the starting point at 90.

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

BTW, in order to let scale_colour_brewer work for categorical variables, you need to set type = 'qual':

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

这篇关于如何反转ggplot2的默认调色板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆