如何在ggplot2的主题对象中添加比例尺功能?

3

我正在尝试为ggplot准备一个自定义主题,以避免一直复制粘贴主题。但我遇到了一个问题,无法将scale函数添加到theme对象中。有人知道应该如何解决吗?

 library(ggthemes)
 theme_RTCGA <- function(base_size = 11, base_family = "", ...){

    theme_pander(gm = TRUE, gM = TRUE, ...) %+replace%
        theme(panel.grid = element_line(), 
                    panel.grid.major = element_line(colour = "grey90", size = 0.2),
                    panel.grid.minor = element_line(colour = "grey98", size = 0.5),
                    legend.position = "top") + 
  scale_colour_pander() +
  scale_fill_pander()
 }

 library(ggplot2)
 # plot
 df <- data.frame(gp = factor(rep(letters[1:3], each = 10)),
                                 y = rnorm(30))
 ds <- plyr::ddply(df, "gp", plyr::summarise, mean = mean(y), sd = sd(y))
 ggplot(df, aes(x = gp, y = y)) +
    geom_point() +
    geom_point(data = ds, aes(y = mean),
                         colour = 'red', size = 3) + theme_RTCGA()
Error: Don't know how to add scale_colour_pander() to a theme object

如果您从自定义主题中删除它们,并手动将它们添加到ggplot调用中,然后跟随..+..theme_RTCGA(),会发生什么? - mtoto
你可以在你的主题函数中将plot作为第一个参数添加,就像这样 `theme_RTCGA <- function(p,base_size = 11, base_family = "", ...){ p + theme_pander(....。 然后使用 (<plot>) %>% theme_RTCGA()` 调用它。 - Pekka
qplot(1,1) + list(theme_bw(), scale_colour_brewer()) - baptiste
@mtoto,我不想在我的100个图中输入超过+ theme_RTCGA()的内容 :) 这就是我提问的原因。 - Marcin
@Pekka 我真的在努力创建一个新主题,所以这种功能不适用于 ggplot2 的 + 约定。 - Marcin
1
@baptiste 这符合问题的要求!您可以将其发布为答案:theme_RTCGA <- function(base_size = 11, base_family = "", ...){list(theme_bw(...), scale_colour_pander())} - Marcin
1个回答

3
根据@baptiste的评论,应该是这样的:
theme_RTCGA <- function(base_size = 11, base_family = "",...){
list(theme_bw(...), scale_colour_pander())
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接