How to manually set colours *conditionally* in aes() AND local geom_xxx() in ggplot?

我的梦境 提交于 2020-01-25 07:52:45

问题


I'm trying to make a function that can take either a single group, or several groups, while using a single colour argument. However, there seems to be a problem in specifying colour both globally (in aes()) and locally (in geom_smooth()). Looks like aes() doesn't accept colour=NULL or just left empty colour=.

Plot without groups (works)

scatterfunction <- function (Data=mtcars,Predictor=mtcars$wt,Response=mtcars$mpg,what.Colour="purple") {
  library(ggplot2)
  ggplot(Data,aes(x=Predictor,y=Response)) +
    geom_smooth(method="lm",colour=what.Colour)
}
scatterfunction()

Plot with groups (works)

groupscatterfunction <- function (Data=mtcars,Predictor=mtcars$wt,Response=mtcars$mpg,Group.variable=factor(mtcars$cyl),what.Colour=c("purple", "yellow", "brown")) {
  library(ggplot2)
  ggplot(Data,aes(x=Predictor,y=Response,colour=Group.variable)) +
    geom_smooth(method="lm") +
    scale_color_manual(values=what.Colour)
}
groupscatterfunction()

Plot without groups, conditional (works when has.Groups=F)

conditionalscatterfunction <- function (Data=mtcars,Predictor=mtcars$wt,Response=mtcars$mpg,Group.variable=factor(mtcars$cyl),has.Groups=F,what.Colour="purple") {
  library(ggplot2)
  ggplot(Data,aes(x=Predictor,y=Response,colour= if(has.Groups==T) {Group.variable})) +
    geom_smooth(method="lm",colour= if(has.Groups==F){what.Colour}) +
    if (has.Groups==T) {scale_color_manual(values=what.Colour)}
}
conditionalscatterfunction()

Plot with groups, conditional (doesn't work when has.Groups=T)

conditionalscatterfunction(Data = mtcars,
                           Predictor = mtcars$wt,
                           Response = mtcars$mpg,
                           has.Groups = TRUE,
                           Group.variable = factor(mtcars$cyl),
                           what.Colour = c("purple", "yellow", "brown"))

Error: Aesthetics must be either length 1 or the same as the data (80): colour

Using the alternative switch() statement has worked for me before but not here:

conditionalscatterfunction <- function (Data=mtcars,Predictor=mtcars$wt,Response=mtcars$mpg,Group.variable=factor(mtcars$cyl),has.Groups=T,what.Colour=c("purple", "yellow", "brown")) {
  library(ggplot2)
  ggplot(Data,aes(x=Predictor,y=Response,colour= switch(has.Groups, Group.variable))) +
    geom_smooth(method="lm",colour= if(has.Groups==F){what.Colour}) +
    if (has.Groups==T) {scale_color_manual(values=what.Colour)}
}
conditionalscatterfunction()

Error: Aesthetics must be either length 1 or the same as the data (80): colour

It seems like as long as I add the "colour=" statement in aesthetics(), no matter if I leave it blank or = NULL, I get this error. What's its default when not explicitly called then?

I would prefer avoiding repeating the whole call again because I also have this issue with geom_points(), geom_shape(), etc., and I would need to repeat it for each combination of elements...

Question: How can I solve this problem?


回答1:


There are many ways to skin this cat, but a simple method is to just pre-define the geom_smooth with an if {} else {}. E.g.:

conditionalscatterfunction <- function (Data, Predictor, Response, Group.variable, col = c("purple", "yellow", "brown")) {
  require(ggplot2)

  if (missing(Group.variable)) {
    smooth <- geom_smooth(method = "lm", color = col[1])
  } else {
    smooth <- geom_smooth(aes(color = {{Group.variable}}), method = "lm")
  }

  ggplot(Data, aes(x = {{Predictor}},y = {{Response}})) +
    smooth +
    scale_color_manual(values = col)
}

conditionalscatterfunction(mtcars, wt, mpg)
conditionalscatterfunction(mtcars, wt, mpg, factor(cyl))

Both work fine.



来源:https://stackoverflow.com/questions/57875193/how-to-manually-set-colours-conditionally-in-aes-and-local-geom-xxx-in-ggp

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