本文介绍了使用ggplot2从函数内绘制data.frame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有此功能来获取IRT软件包sirt返回的对象,并为用户可以指定的一组项目绘制项目响应函数:

I have this function to take an object returned by the IRT package sirt and plot item response functions for a set of items that the user can specify:

plotRaschIRF <- function(x,items=NULL,thl=-5,thu=5,thi=.01,D=1.7) {
  if (!class(x)=="rasch.mml") stop("Object must be of class rasch.mml")
  thetas <- seq(thl,thu,thi)
  N <- length(thetas)
  n <- length(x$item$b)
  tmp <- data.frame(item=rep(1:n,each=N),theta=rep(thetas,times=n),b=rep(x$item$b,each=N))
  probs <- exp(D*(tmp[,2]-tmp[,3]))/(1+exp(D*(tmp[,2]-tmp[,3])))
  dat <- data.frame(item=rep(1:n,each=N),theta=rep(thetas,times=n),b=rep(x$item$b,each=N),p=probs)
  #dat$item <- factor(dat$item,levels=1:n,labels=paste0("Item",1:n))
  if (is.null(items)) {
    m <- min(10,n)
    items <- 1:m
    if (10<n) warning("By default, this function will plot only the first 10 items")
  }
  if (length(items)==1) {
    title="Item Response Function"
  } else {
    title="Item Response Functions"
  }
  dat2 <- subset(dat,eval(quote(eval(item,dat) %in% items)))
  dat2$item <- factor(dat2$item,levels=unique(dat2$item),labels=paste0("Item",unique(dat2$item)))
  out <- ggplot(dat2,aes(x=theta,y=p,group=item)) +
   geom_line(aes(color=dat2$item),lwd=1) + guides(col=guide_legend(title="Items")) +
   theme_bw() + ggtitle(title) + xlab(expression(theta)) +
   ylab("Probability") + scale_x_continuous(breaks=seq(thl,thu,1))
  print(out)
}

但是,似乎在我刚开始使用ggplot2之前(我将dat2的一列转换为一个因子)或者在ggplotting本身上就一直卡在这行上-不能确定是哪个.我收到错误消息"eval(expr,envir,enclos)中的错误:找不到对象'dat2'".

But it seems to be getting stuck at either the line just before I start using ggplot2 (where I convert one column of dat2 to a factor) or at the ggplotting itself -- not really sure which. I get the error message "Error in eval(expr, envir, enclos) : object 'dat2' not found".

我尝试像以前一样通读建议在此处,但这可能是一个不同的问题,或者我只是没有得到它.当我逐行浏览该功能时,该功能运行良好.任何帮助,我们将不胜感激!

I tried reading through this as was suggested here but either this is a different problem or I'm just not getting it. The function works fine when I step through it line by line. Any help is greatly appreciated!

推荐答案

根据您的评论,错误几乎可以肯定在 geom_line(aes(color = dat2 $ item))中.摆脱 dat2 $ ,它应该可以正常工作(即 geom_line(aes(color = item))).在 data 参数(此处为 dat2 )中评估 aes 中的内容,并以全局环境为附件.值得注意的是,这意味着 aes 不能使用功能环境中的内容,除非它是 data (此处为 dat2 )的一部分.由于 dat2 dat2 内部不存在,并且 dat2 在全局环境中不存在,因此会出现该错误.

Based on your comments, the error is almost certainly in geom_line(aes(color=dat2$item)). Get rid of dat2$ and it should work fine (i.e. geom_line(aes(color=item))). Stuff in aes is evaluated in the data argument (dat2 here), with the global environment as the enclosure. Notably this means stuff in the function environment is not available for use by aes unless it is part of the data (dat2 here). Since dat2 doesn't exist inside dat2, and dat2 doesn't exist in the global environment, you get that error.

这篇关于使用ggplot2从函数内绘制data.frame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 09:39