R 通过另一个函数将数据和列名传递给ggplot
我将直接跳到一个示例,并在后面评论:R 通过另一个函数将数据和列名传递给ggplot,r,function,ggplot2,environment,aesthetics,R,Function,Ggplot2,Environment,Aesthetics,我将直接跳到一个示例,并在后面评论: cont <- data.frame(value = c(1:20),variable = c(1:20,(1:20)^1.5,(1:20)^2),group=rep(c(1,2,3),each=20)) value variable group 1 1 1.000000 1 2 2 2.000000 1 3 3 3.000000 1 #... etc. #ser is s
cont <- data.frame(value = c(1:20),variable = c(1:20,(1:20)^1.5,(1:20)^2),group=rep(c(1,2,3),each=20))
value variable group
1 1 1.000000 1
2 2 2.000000 1
3 3 3.000000 1
#... etc.
#ser is shorthand for "series".
plot_scat <- function(data,x,y,ser) {
ggplot(data,aes(x=x,y=y,color=factor(ser)))+geom_point()
}
plot_scat(cont,value,variable,group)
#This gives the error:
#Error in eval(expr,envir,enclose) : object 'x' not found
这就是我想要的。因此,我添加了下一层复杂性:
plot_scat <- function(data,x,y,ser) {
#added facet_grid
ggplot(data,aes(x=x,y=y,color=factor(ser)),environment=environment())+geom_point()+
facet_grid(.~ser)
}
plot_scat(cont,cont$value,cont$variable,cont$group)
#This gives the error:
#Error in layout_base(data, cols, drop = drop):
# At least one layer must contain all variables used for facetting
plot_scat您可以使用aes_string()代替aes(),并将列名作为字符串传递
plot_scat <- function(data,x,y,ser) {
ser_col = paste("factor(",ser,")")
ggplot(data,aes_string(x=x,y=y,col=ser_col))+geom_point()+facet_grid(as.formula(sprintf('~%s',ser)))
}
plot_scat(cont,"value","variable","group")
plot\u scat如果您不想将变量(列名)作为字符串/引号传递,那么我尝试的一种方法(另请参见)是使用match.call()
和eval
。它也适用于镶嵌面(如您的情况):
由(v0.2.1)于2019-01-10创建
或者,另一种解决方法是使用get()
plot\u scat
plot_scat <- function(data,x,y,ser) {
#added facet_grid
ggplot(data,aes(x=x,y=y,color=factor(ser)),environment=environment())+geom_point()+
facet_grid(.~ser)
}
plot_scat(cont,cont$value,cont$variable,cont$group)
#This gives the error:
#Error in layout_base(data, cols, drop = drop):
# At least one layer must contain all variables used for facetting
plot_scat <- function(data,x,y,ser) {
ser_col = paste("factor(",ser,")")
ggplot(data,aes_string(x=x,y=y,col=ser_col))+geom_point()+facet_grid(as.formula(sprintf('~%s',ser)))
}
plot_scat(cont,"value","variable","group")
library(ggplot2)
cont <- data.frame( value = c(1:20),
variable = c(1:20, (1:20) ^ 1.5, (1:20) ^ 2),
group = rep(c(1, 2, 3), each = 20))
plot_scat <- function(data, x, y, ser) {
arg <- match.call()
ggplot(data, aes(x = eval(arg$x),
y = eval(arg$y),
color = factor(eval(arg$ser)))) +
geom_point() +
facet_grid(. ~ eval(arg$ser))
}
# Call your custom function without quoting the variables
plot_scat(data = cont, x = value, y = variable, ser = group)
plot_scat <- function(data, x, y, ser) {
str(as.list(match.call()))
}
plot_scat(cont, value, variable, group)
#> List of 5
#> $ : symbol plot_scat
#> $ data: symbol cont
#> $ x : symbol value
#> $ y : symbol variable
#> $ ser : symbol group
plot_scat <- function(data, x, y, ser) {
ggplot(data, aes(x = get(x),
y = get(y),
color = factor(get(ser)))) +
geom_point() +
facet_grid(. ~ get(ser))
}
plot_scat(data = cont, x = "value", y = "variable", ser = "group")