Hot to change/set global scale aesthetics / colours for ggplot?

孤街醉人 提交于 2019-12-11 11:37:17

问题


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 defaults can be changed e.g. with

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

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?

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

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?


回答1:


you can simply redefine the relevant scale,

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


来源:https://stackoverflow.com/questions/26019984/hot-to-change-set-global-scale-aesthetics-colours-for-ggplot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!