【问题标题】:ggplot: How to set default color for all geoms?ggplot:如何为所有几何图形设置默认颜色?
【发布时间】:2014-01-16 22:55:34
【问题描述】:

我正在尝试将 ggplot 中所有几何图形的默认颜色设置为黑色以外的颜色。请注意,这不是关于设置 scale_color...

简单示例:

# linear model with confidence bands...
set.seed(1)
df <- data.frame(x=1:50, y=5 + 2*(1:50)+rnorm(50,sd=10))
lm <- lm(y~x,df)
se <- summary(lm)$sigma           # standard error of fit
Z  <- qnorm(0.05/2,lower.tail=F)  # 95% confidence bands
df <- cbind(df,predict(lm,se.fit=T)[c("fit","se.fit")])
# plot the result...
library(ggplot2)
ggplot(df, aes(x=x)) + 
  geom_point(aes(y=y), size=3) +
  geom_line(aes(y=fit)) +
  geom_line(aes(y=fit+Z*se.fit), linetype=2)+
  geom_line(aes(y=fit-Z*se.fit), linetype=2)

现在,假设我想让所有东西都变成红色。撇开这样做的可取性不谈,我认为ggplot(df, aes(x=x), colour="red") 会这样做。但是colour= 参数似乎被忽略了:一切仍然是黑色的。我可以将colour="red" 添加到每个geom_ 呼叫中,但我正在努力避免这种情况。

编辑: 使用ggplot(df, aes(x=x, color="red")) 不是一个选项,因为它使用默认的ggplot 调色板(evenly spaced around an HSL color circle)创建了一个色标。使用一种颜色,这是#F8766D,恰好是浅红色。此外,这会创建一个图例,然后必须将其隐藏。

【问题讨论】:

  • 只是好奇,你为什么不想使用 scale_color_?
  • @PirateGrunt 问题末尾的编辑解释了它。我必须通过在 ggplot 调用中在aes(...) 中设置颜色来创建色标,然后使用scale_color_manual(values="red", guide="none") 之类的东西。这只是一个 hack - 应该有一个更简单的方法。另外,假设我想对图中的其他内容使用色标,例如根据某个分组变量为点着色,并将其他所有内容设为红色。
  • @PirateGrunt 另一个原因是黑暗主题的情节。唯一的挑战是设置与几何图形相关的颜色(点、箱线图、线等)

标签: r ggplot2


【解决方案1】:

您可以通过这种方式为每种几何类型设置默认颜色:

update_geom_defaults("point",   list(colour = "red"))
update_geom_defaults("line",   list(colour = "red"))

ggplot(df, aes(x=x)) + 
  geom_point(aes(y=y), size=3) +
  geom_line(aes(y=fit)) +
  geom_line(aes(y=fit+Z*se.fit), linetype=2)+
  geom_line(aes(y=fit-Z*se.fit), linetype=2)

编辑 如果您想对所有内容执行操作,请使用(编辑借自here):

params <- ls(pattern = '^geom_', env = as.environment('package:ggplot2'))
geoms <- gsub("geom_", "", params)

lapply(geoms, update_geom_defaults, list(colour = "red"))
lapply(geoms, update_geom_defaults, list(fill = "red", colour = "red")) ## include fills 

如果您想为只有一个图设置默认颜色,只需执行以下操作:

ggplot(df, aes(x=x, colour="red")) + 
  geom_point(aes(y=y), size=3) +
  geom_line(aes(y=fit)) +
  geom_line(aes(y=fit+Z*se.fit), linetype=2)+
  geom_line(aes(y=fit-Z*se.fit), linetype=2)

【讨论】:

  • 哇,我从来不知道这个功能。太棒了。
  • 谢谢,但这不是我想要的。我试图为这个 ggplot 对象设置默认颜色,就像调用ggplot(df) 使df 成为这个对象的默认数据集一样。根据文档,update_geom_default(...) 设置了本次会话中所有未来绘图的默认值。
  • 已修改以反映这种区别。 @Blue Magister,您之前建议的修正案是什么?如果是这样,请重新提交归属!
  • @metasequoia 惊人的功能!但是,您的代码存在问题,因为它将颜色美学映射到变量“红色”,尝试将其更改为“绿色”,您会看到线条和点仍然是红色的。
  • 有没有办法用多种颜色做到这一点,例如当有 3 个类别时使用 geom_col。
【解决方案2】:

为了将一个geom默认美学替换为另一个(对于所有使用该美学的geom),您可以尝试以下代码。

首先定义一个函数,用于从ggplot2

中获取所有geoms的默认aes设置
library(ggplot2)
library(purrr)

geom_aes_defaults <- function() {
  geom_names <- apropos("^Geom", ignore.case = FALSE)
  geoms <- mget(geom_names, env = asNamespace("ggplot2"))
  map(geoms, ~ .$default_aes)
}

使用geom_aes_defaults(),您可以获得所有几何美学映射的长列表

$Geom
Aesthetic mapping:
<empty>

$GeomAbline
Aesthetic mapping:
* `colour`   -> "black"
* `size`     -> 0.5
* `linetype` -> 1
* `alpha`    -> NA

$GeomAnnotationMap
Aesthetic mapping:
* `colour`   -> "NA"
* `fill`     -> "grey20"
* `size`     -> 0.5
* `linetype` -> 1
* `alpha`    -> NA

$GeomArea
Aesthetic mapping:
* `colour`   -> NA
* `fill`     -> "grey20"
* `size`     -> 0.5
* `linetype` -> 1
* `alpha`    -> NA

...

以下函数迭代所有符合给定美学的几何图形并替换相应的值

replace_geom_aes_defaults <- function(name, old_aes, new_aes) {
  matching_geoms <- 
    map(geom_aes_defaults(), name) %>%
      compact() %>%
      keep(~ !is.na(.) & . == old_aes)
  geoms <- gsub("^Geom(.*)", "\\1", names(matching_geoms))
  walk(geoms, update_geom_defaults, setNames(list(new_aes), name))
}

现在您可以系统地替换颜色,例如变黑变红了

replace_geom_aes_defaults("colour", "black", "red")

甚至将填充颜色(用于条形图)替换为

replace_geom_aes_defaults("fill", "grey35", "red")

【讨论】:

    猜你喜欢
    • 2012-03-12
    • 2016-01-16
    • 2018-04-21
    • 2020-08-01
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多