如何在ggplot中的aes()和local geom_xxx()中*有条件*手动设置颜色? [英] How to manually set colours *conditionally* in aes() AND local geom_xxx() in ggplot?

查看:60
本文介绍了如何在ggplot中的aes()和local geom_xxx()中*有条件*手动设置颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个可以使用单个 colour 自变量的函数,该函数可以采用一个或多个组.但是,在全局(在 aes()中)和本地(在 geom_smooth()中)中指定 colour 似乎存在问题.看起来 aes()不接受 colour = NULL 或仅留空 colour = .

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

没有组的图(作品)

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

包含组(作品)的图

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

不带条件的组图(当 has.Groups = F 时有效)

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

具有条件的组图(在 has.Groups = T 时不起作用)

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

使用替代的 switch()语句之前对我有用,但在这里无效:

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

似乎只要我在 aesthetics()中添加" colour = "语句,无论我将其保留为空白还是 = NULL ,则出现此错误.如果未显式调用则默认为什么?

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?

我宁愿避免再次重复整个通话,因为我在 geom_points() geom_shape()等上也遇到了这个问题,我需要重复它适用于元素的每个组合...

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?

推荐答案

有很多方法可以给这只猫换皮,但是一种简单的方法是使用预先定义 geom_smooth 如果{} else {} .例如:

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

两个都很好.

这篇关于如何在ggplot中的aes()和local geom_xxx()中*有条件*手动设置颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆