ggplot: How to set default color for all geoms?

后端 未结 2 2072
失恋的感觉
失恋的感觉 2020-12-05 18:40

I\'m trying to set the default color for all geoms in a ggplot to something other than black. Note this is not about setting scale_color...

Simple example:<

相关标签:
2条回答
  • 2020-12-05 18:50

    In order to replace a geom default aesthetic with another one (for all geoms using that aesthetic), you can try the following code.

    First define a function for getting default aes settings of all geoms from ggplot2

    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)
    }
    

    With geom_aes_defaults() you obtain a long list of all geom aesthetic mappings

    $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
    
    ...
    

    The following function iterates over all geoms matching a given aesthetic and substitutes the corresponding values

    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))
    }
    

    Now you can replace colors systematically, e.g. turn black into red by

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

    or even replace fill colors (for bar plots) by

    replace_geom_aes_defaults("fill", "grey35", "red")
    
    0 讨论(0)
  • You can set a default color for each geometry type this way:

    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)
    

    Edit If you want to do to everything then use (Edit borrow from 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 
    

    If you want to set the default colour for the just one plot, simply do:

    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)
    
    0 讨论(0)
提交回复
热议问题