热衷于更改/设置ggplot的全局比例美学/颜色吗? [英] Hot to change/set global scale aesthetics / colours for ggplot?

查看:62
本文介绍了热衷于更改/设置ggplot的全局比例美学/颜色吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢编写一个函数,该函数可以全局设置ggplot对象的特定主题选项并缩放外观,与ggthemr-package .

I like to write a function, which globally sets specific theme options and scale aesthetics for ggplot objects, comparable to the ggthemr-package.

我可以使用以下代码段修改主题选项:

I can modify theme options with following code-snippet:

sjtheme <- theme_grey() +
  theme(axis.text.x = element_text(angle=axis.angle.x, size=rel(axis.size.x), colour=axis.color.x), 
        axis.text.y = element_text(angle=axis.angle.y, size=rel(axis.size.y), colour=axis.color.y), 
        axis.title = element_text(size=rel(axis.title.size), colour=axis.title.color),
        axis.ticks = element_line(colour=axis.ticks.color),
        axis.ticks.length = unit(axis.ticks.length, "cm"),
        plot.title = element_text(size=rel(title.size), colour=title.color),
        plot.background = element_rect(colour=plot.bordercol, fill=plot.backgroundcol),
        panel.background = element_rect(colour=panel.bordercol, fill=panel.backcol),
        panel.grid.minor = element_line(colour=panel.minor.gridcol),
        panel.grid.major = element_line(colour=panel.major.gridcol))
theme_set(sjtheme)

geom默认值可以更改,例如与

geom defaults can be changed e.g. with

update_geom_defaults('boxplot', list(fill = geom.colors, alpha = geom.alpha, outlier.colour = geom.colors))

现在这两种方法都很好用,所以每当我创建一个ggplot对象时,都会应用我的主题和geom选项.

now this both works well, so whenever I create a ggplot-object, my theme and geom options are applied.

但是我该如何使用比例尺,所以条/点/线的颜色也将获得新的默认值?

But how do I do this with scales, so bar/dot/line colors get a new default as well?

ggthemr软件包中有一个解决方案: https://github.com/cttobin/ggthemr/blob/master/R/theme_scales.R

There is a solution in the ggthemr-package: https://github.com/cttobin/ggthemr/blob/master/R/theme_scales.R

但是,这种方法修改了全球环境,这将违反CRAN提交政策.我在网络上通过搜索引擎发现的所有建议都无效,例如"set_scale_defaults"(已在较新的ggplot版本中删除)或此帖子.

However, this approach modifies the global environment, which would violate CRAN submission policies. All suggestions I found via search eninge on the web don't work, like "set_scale_defaults" (which has been removed in newer ggplot versions) or this posting or this posting.

那么,有可能更改不更改用户全局环境的比例默认值吗?

So, is there a possibility to change scale defaults that don't change the user's global environment?

推荐答案

您可以简单地重新定义相关比例,

you can simply redefine the relevant scale,

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

这篇关于热衷于更改/设置ggplot的全局比例美学/颜色吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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