首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法将用户定义函数中的参数传递给ggplot

无法将用户定义函数中的参数传递给ggplot
EN

Stack Overflow用户
提问于 2014-04-17 14:45:25
回答 1查看 1.6K关注 0票数 3

我想创建一个用户定义的函数,它包装了我找到的一些流行的ggplot代码。我收到以下错误:

代码语言:javascript
复制
"Error in `[.data.frame`(DS, , xvar) : object 'xcol' not found" 

下面是一个小的伪数据集来说明这个问题。

代码语言:javascript
复制
n=25
dataTest <- data.frame(xcol=sample(1:3, n, replace=TRUE), ycol = rnorm(n, 5, 2), Cat=letters[1:5])

用户定义的代码如下:

代码语言:javascript
复制
  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)
}

我尝试运行的函数如下:

代码语言:javascript
复制
TRIPLOT(dataTest,"xcol","ycol","Cat")

我按照post的建议添加了环境参数和data.frame()参数。我还用引号将参数传递给函数。谢谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2014-04-18 05:03:50

将变量名作为字符串传递给函数,在gg数据框中重命名变量;然后使用aes代替aes_string;并在对ggplot的调用中使用gg数据框及其变量名。

或者将变量名作为字符串传递给函数;然后使用aes_string (并跳过gg数据框)。

此外,scale_fill_manual中的颜色数量也存在问题。我已经把这些行注释掉了。

代码语言:javascript
复制
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])

以下任一项:

代码语言:javascript
复制
 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")

或者:

代码语言:javascript
复制
 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")

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23126328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档