如何更改ggplot2中的默认配色方案? [英] How to change default color scheme in ggplot2?

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

问题描述

我想更改ggplot2中的默认配色方案.也就是说,我想在脚本中的一点定义一种配色方案(例如viridis),以便所有后续ggplot图都将使用此配色方案,而不必每次都调用+ scale_color_viridis().

I would like to change the default color scheme in ggplot2. That is, I would like to define a color scheme (say: viridis) at the one point in the script so that all subsequent ggplot diagrams will use this color scheme without having to call + scale_color_viridis() each time.

我看过这篇SO帖子具有update_geom_defaults(geom, new)的功能,但我找不到一种方法来解释使用诸如viridis之类的方案的功能.

I've seen this SO post featuring update_geom_defaults(geom, new), but I could not find a way to explain this function to use a scheme such as viridis.

我还尝试过更新ggplot颜色,类似于这篇文章,但是,正如@baptise所指出的那样,这种方法实际上是行不通的.

I have also tried to update the ggplot color, similar to this post, but, as @baptise pointed out, this approach does not really work.

简而言之:

  1. 定义新的配色方案,例如viridis

  1. define new color scheme, eg., viridis

随后在不添加+ scale_color_viridis()的情况下调用ggplot,但此ggplot图仍使用viridis配色方案.

call ggplot subsequently without adding + scale_color_viridis() but still this ggplot diagram uses the viridis color scheme.

推荐答案

看起来像

options(ggplot2.continuous.colour="viridis")

将执行您想要的操作(即ggplot将查找名为

will do what you want (i.e. ggplot will look for a colour scale called

scale_colour_whatever

...)

library(ggplot2)
opts <- options(ggplot2.continuous.colour="viridis")
dd <- data.frame(x=1:20,y=1:20,z=1:20)

ggplot(dd,aes(x,y,colour=z))+geom_point(size=5)
options(oldopts) ## reset previous option settings

对于离散刻度,请回答

For discrete scales, the answer to this question (redefine the scale_colour_discrete function with your chosen defaults) seems to work well:

scale_colour_discrete <- function(...) {
  scale_colour_brewer(..., palette="Set1")
}

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

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