本文介绍了无法将用户定义函数中的参数传递给ggplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想创建一个用户定义函数,它包含了我发现的一些流行的ggplot代码。我得到以下错误: $ $ p $ 错误`[.data.frame`(DS,,xvar):object' xcol'not found 下面是一个小的伪数据集来说明问题。 n = 25 dataTest 用户定义的代码如下: TRIPLOT #localenv< -environment() gg< - data.frame(x = DS [,xvar],y = DS [,yvar],fill = DS [,zvar]) empty< - (), plot.background = panel.grid.minor = element_blank(), panel.border = element_blank(), panel.background = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank(),轴。 text.x = element_blank(), axis.text.y = element_blank(), axis.ticks = element_blank()) scatter< -ggplot(gg,aes_string(x = xvar,y = yvar),environment = environment())+ geom_point(aes_string(color = zvar))+ scale_color_manual(values = c(orange,purple))+ 主题(legend.position = c(1,1),legend.justification = c(1,1)) plot_top plot_right< - ggplot gg,aes_string(yvar,fill = zvar),environment = environment())+ geom_density(alpha = .5)+ coord_flip()+ scale_fill_manual(values = c(orange,purple))+ 主题(legend.position =none,axis.title.x = element_blank()) PLT print(PLT)} 我试图运行的函数是以下: $ $ p $ TRIPLOT(dataTest,xcol,ycol,Cat) 我按照帖子的建议添加了环境参数和data.frame()参数。我也通过引号将参数传递给函数。 感谢您的帮助。 解决方案将变量名作为字符串传递给函数, gg 数据框;然后用 aes 来代替 aes_string ;并在调用 ggplot 时使用 gg 数据框及其变量名称。 或者将变量名作为字符串传递给函数;然后使用 aes_string (并跳过 gg 数据框)。 另外, scale_fill_manual 中的颜色数量有问题。我已经评论了这些线。 library(gridExtra) library(ggplot2) n = 25 dataTest = data.frame( xcol = sample(1:3,n,replace = TRUE), ycol = rnorm(n,5,2), Cat = letters [1: 5]) 其中: #localenv< -environment() gg< - data.frame(x = DS [ ,xvar],y = DS [,yvar],fill = DS [,zvar])空 panel.background = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), panel.background = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_blank (), axis.text.y = element_blank(), axis.ticks = element_blank()) scatter< -ggplot(gg,aes(x = x ,y = y))+ geo m_point(aes(color = fill))+ #scale_color_manual(values = c(orange,purple))+ theme(legend.position = c(1,1),legend。 justification = c(1,1)) plot_top< -ggplot(gg,aes(x = x,fill = fill))+ geom_density(alpha = .5)+ #scale_fill_manual(values = c(orange,purple))+ theme(legend.position =none) plot_right< -ggplot(gg, aes(x = y,fill = fill))+ geom_density(alpha = .5)+ coord_flip()+ #scale_fill_manual(values = c(orange,purple))+ 主题(legend.position =none,axis.title.x = element_blank()) PLT print(PLT)} TRIPLOT ,ycol,Cat) OR: TRIPLOT< - 函数(DS,xvar,yvar,zvar){#localenv< -environment() #gg< - 数据.frame(x = DS [,xvar],y = DS [,yvar],fill = DS [,zvar]) 空< -ggplot()+ geom_point(aes(1,1),color =white)+ theme( plot.background = element_blank(), panel.background = element_blank (), panel.grid.minor = element_blank(), panel.border = element_blank(), panel.background = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank(),)axis.ticks = element_blank()) scatter< -ggplot(DS,aes_string(x = xvar,y = yvar))+ geom_point(aes_string(color = zvar)) + #scale_color_manual(values = c(orange,purple))+ theme(legend.position = c(1,1),legend.justification = c(1,1)) plot_top< -ggplot(DS,aes_string(x = xvar,fill = zvar))+ geom_density(alpha = .5)+ #scale_fill_manual(values = c (orange,purple))+ theme(legend.position =none) plot_right< -ggplot(DS,aes_string(x = yvar,f ()= + geom_density(alpha = .5)+ coord_flip()+ #scale_fill_manual(values = c(orange,purple))+ theme .position =none,axis.title.x = element_blank()) PLT print(PLT)} TRIPLOT(dataTest,xcol,ycol, Cat) I want to create a user define function that wraps around some popular ggplot code I found. I am getting the following error: "Error in `[.data.frame`(DS, , xvar) : object 'xcol' not found"The following is a small pseudo dataset to illustrate the issue.n=25dataTest <- data.frame(xcol=sample(1:3, n, replace=TRUE), ycol = rnorm(n, 5, 2), Cat=letters[1:5])The user defined code is as here: TRIPLOT <- function (DS,xvar,yvar,zvar) { #localenv<-environment() gg <- data.frame(x=DS[,xvar],y=DS[,yvar],fill=DS[,zvar]) empty<-ggplot()+geom_point(aes(1,1),colour="white") + theme( plot.background = element_blank(), panel.background = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), panel.background = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank(), axis.ticks = element_blank()) scatter <-ggplot(gg, aes_string(x=xvar,y=yvar), environment=environment())+ geom_point(aes_string(color=zvar))+ scale_color_manual(values=c("orange","purple"))+ theme(legend.position=c(1,1), legend.justification=c(1,1)) plot_top <- ggplot(gg,aes_string(x=xvar, fill=zvar), environment=environment())+geom_density(alpha=.5)+scale_fill_manual(values=c("orange","purple"))+theme(legend.position="none") plot_right <- ggplot(gg, aes_string(yvar, fill=zvar),environment=environment())+geom_density(alpha=.5)+coord_flip()+ scale_fill_manual(values=c("orange","purple"))+ theme(legend.position="none", axis.title.x=element_blank()) PLT <- grid.arrange(plot_top,empty,scatter,plot_right, ncol=2,nrow=2,widths=c(4,1), heights=c(1,4)) print(PLT)}The function I am trying to run is the following:TRIPLOT(dataTest,"xcol","ycol","Cat")I added environment argument and the data.frame() argument as recommend by the post. I have also passed the arguments to the function in quotes.Thank you for any help. 解决方案 Either pass the variable names as strings to the function, rename the variables in gg data frame; then use aes in place of aes_string; and use the gg data frame and its variable names in the calls to ggplot.Or pass the variable names as strings to the function; then use aes_string (and skip the gg data frame).In addition there is a problem with the number of colours in scale_fill_manual. I've commented those lines out. library(gridExtra)library(ggplot2)n=25dataTest = data.frame( xcol=sample(1:3, n, replace=TRUE), ycol = rnorm(n, 5, 2), Cat=letters[1:5])EITHER: TRIPLOT <- function (DS,xvar,yvar,zvar) { #localenv<-environment() gg <- data.frame(x = DS[,xvar], y = DS[, yvar],fill = DS[,zvar]) empty<-ggplot()+geom_point(aes(1,1),colour="white") + theme( plot.background = element_blank(), panel.background = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), panel.background = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank(), axis.ticks = element_blank()) scatter <-ggplot(gg, aes(x = x,y = y))+ geom_point(aes(color = fill))+ #scale_color_manual(values=c("orange","purple"))+ theme(legend.position=c(1,1), legend.justification=c(1,1)) plot_top <-ggplot(gg,aes(x = x, fill = fill))+ geom_density(alpha=.5)+ #scale_fill_manual(values=c("orange","purple"))+ theme(legend.position="none") plot_right <-ggplot(gg, aes(x = y, fill = fill))+ geom_density(alpha=.5)+coord_flip()+ #scale_fill_manual(values=c("orange","purple"))+ theme(legend.position="none", axis.title.x=element_blank()) PLT<-grid.arrange(plot_top,empty,scatter,plot_right, ncol=2,nrow=2,widths=c(4,1), heights=c(1,4)) print(PLT)}TRIPLOT(dataTest,"xcol","ycol","Cat")OR: TRIPLOT <- function (DS,xvar,yvar,zvar) { #localenv<-environment() #gg <- data.frame(x = DS[,xvar], y = DS[, yvar],fill = DS[,zvar]) empty<-ggplot()+geom_point(aes(1,1),colour="white") + theme( plot.background = element_blank(), panel.background = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), panel.background = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank(), axis.ticks = element_blank()) scatter <-ggplot(DS, aes_string(x = xvar,y = yvar))+ geom_point(aes_string(color = zvar))+ #scale_color_manual(values=c("orange","purple"))+ theme(legend.position=c(1,1), legend.justification=c(1,1)) plot_top <-ggplot(DS,aes_string(x = xvar, fill = zvar))+ geom_density(alpha=.5)+ #scale_fill_manual(values=c("orange","purple"))+ theme(legend.position="none") plot_right <-ggplot(DS, aes_string(x = yvar, fill = zvar))+ geom_density(alpha=.5)+coord_flip()+ #scale_fill_manual(values=c("orange","purple"))+ theme(legend.position="none", axis.title.x=element_blank()) PLT<-grid.arrange(plot_top,empty,scatter,plot_right, ncol=2,nrow=2,widths=c(4,1), heights=c(1,4)) print(PLT)}TRIPLOT(dataTest,"xcol","ycol","Cat") 这篇关于无法将用户定义函数中的参数传递给ggplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 17:31