如何在 aes() 和 ggplot 中的本地 geom_xxx() 中*有条件地*手动设置颜色?
How to manually set colours *conditionally* in aes() AND local geom_xxx() in ggplot?
我正在尝试创建一个函数,该函数可以采用单个组或多个组,同时使用单个 colour
参数。但是,在全局(aes()
)和局部(geom_smooth()
)中指定 colour
似乎存在问题。看起来 aes()
不接受 colour=NULL
或只是留空 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
时有效)
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
时不起作用)
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()
语句以前对我有用,但在这里不行:
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
,都会出现这个错误。那么当没有明确调用时它的默认值是什么?
我宁愿避免再次重复整个调用,因为我对 geom_points()
、geom_shape()
等也有这个问题,我需要对每个元素组合重复它。 .
问题:如何解决这个问题?
给这只猫换皮的方法有很多种,但一个简单的方法就是用if {} else {}
预先定义geom_smooth
。例如:
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))
两者都很好。
我正在尝试创建一个函数,该函数可以采用单个组或多个组,同时使用单个 colour
参数。但是,在全局(aes()
)和局部(geom_smooth()
)中指定 colour
似乎存在问题。看起来 aes()
不接受 colour=NULL
或只是留空 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
时有效)
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
时不起作用)
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()
语句以前对我有用,但在这里不行:
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
,都会出现这个错误。那么当没有明确调用时它的默认值是什么?
我宁愿避免再次重复整个调用,因为我对 geom_points()
、geom_shape()
等也有这个问题,我需要对每个元素组合重复它。 .
问题:如何解决这个问题?
给这只猫换皮的方法有很多种,但一个简单的方法就是用if {} else {}
预先定义geom_smooth
。例如:
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))
两者都很好。