【问题标题】:How to fix problem with "Error in plot.window(...) : need finite 'xlim' values" in R如何解决 R 中“plot.window(...) 中的错误:需要有限的 'xlim' 值”的问题
【发布时间】:2019-03-04 14:03:03
【问题描述】:

我正在尝试编写一个函数来为我做散点图,

我正在使用的数据结构如下所示:

'data.frame':   129 obs. of  15 variables:
 $ Player      : Factor w/ 129 levels "Abbrederis, Jared",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ College     : Factor w/ 79 levels "Alabama","Arizona",..: 78 20 65 77 27 48 67 31 31 19 ...
 $ Position    : Factor w/ 7 levels "DB","LB","OL",..: 7 7 6 4 4 4 2 2 7 7 ...
 $ OverallGrade: num  5.2 5.96 5.4 5.16 5.45 5.1 6.6 5.37 5.9 6.4 ...
 $ Height      : int  73 73 77 70 68 73 77 73 71 77 ...
 $ ArmLength   : num  31.4 32.6 34 31.2 31 ...
 $ Weight      : int  195 212 265 225 173 218 255 237 198 240 ...
 $ HandLength  : num  9.62 9 9 9.5 8.88 ...
 $ Dash40      : num  4.5 4.56 4.74 4.82 4.26 4.48 4.66 4.64 4.43 4.61 ...
 $ BenchPress  : int  4 14 28 20 20 19 15 22 7 13 ...
 $ VerticalJump: num  30.5 39.5 33 29.5 38 38 34.5 35 38.5 32.5 ...
 $ BroadJump   : int  117 123 118 106 122 121 119 123 122 119 ...
 $ Cone3Drill  : num  6.8 6.82 7.42 7.24 6.86 7.07 6.82 7.24 6.69 7.33 ...
 $ Shuttle20   : num  4.08 4.3 4.3 4.49 4.06 4.46 4.19 4.35 3.94 4.39 ...
 $ Position1   : Factor w/ 7 levels "WO","DB","S",..: 1 1 6 4 4 4 5 5 1 1 ...
  ..- attr(*, "scores")= num [1:7(1d)] 4.54 4.75 5.22 4.59 4.58 ...
  .. ..- attr(*, "dimnames")=List of 1
  .. .. ..$ : chr  "DB" "LB" "OL" "RB" ...

我设法在不编写函数的情况下进行绘图并且代码有效:

with(nfl,plot(nfl$Dash40,nfl$BenchPress,
              pch=c(1,3,4,2,0,8,5),
              col=c("black","red","blue","darkgreen","purple","orange","gray"),
              xlab = "Bench Press weight", 
              ylab="40-year dash time in seconds"),
              panel.first = grid())
legend("bottomright", legend=levels(nfl$Position), 
       pch=c(1,3,4,2,0,8,5),
       cex=0.5,
       col=c("black","red","blue","darkgreen","purple","orange","gray"))
a<-paste(nfl$Player,nfl$BenchPress)
text(nfl$Dash40,nfl$BenchPress,label=as.character(a),cex=0.5)

所以基本上我想看看不同数值变量之间的关系,我想如果上面的代码可以工作,下面的函数应该也可以工作,

myplot<-function(xvar,yvar,xlab,ylab){
  b<-paste("xlab","vs","ylab")
  xvar<-nfl$"xvar"
  yvar<-nfl$"yvar"
  with(nfl,plot(yvar,xvar),
                pch=c(1,3,4,2,0,8,5),
                col=c("black","red","blue","darkgreen","purple","orange","gray"),
                xlab="xlab",ylab="ylab",
                main="b")
}

myplot(Dash40,BenchPress,dash,bench)

我使用 Dash40 和 BenchPress 测试了该功能,但结果发现该功能不起作用:

Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) :
 Show Traceback

 Rerun with Debug
 Error in plot.window(...) : need finite 'xlim' values 

基本上我正在尝试使用两个不同的代码做同样的工作,为什么第二个不起作用?有人可以告诉我如何解决这个问题吗?

【问题讨论】:

    标签: r plot error-handling scatter-plot


    【解决方案1】:

    我对你的功能感到很困惑。为什么它有参数xvar,yvar 被它覆盖而不使用?

    nfl$"xvar"nfl$"yvar" 是什么意思?

    既然你使用with(nfl,...,为什么还要这样做?

    如果 nfl 没有名为 xvaryvar 的列,那么它们将导致错误或被解释为 NULL;如果您绘制NULL,那么您需要指定绘图的限制,因为这无法从数据中找到。

    函数失败,因为您正在绘制NULL

    您还需要将数据从nfl 传递给函数,因为Dash40BenchPress 存在找不到的危险,因为它们只存在于nfl 中。为此,您使用$ 符号。您应该将 nfl$BenchPress 传递给函数,而不是 BenchPress

    以下应该可以工作。

    myplot<-function(xvar,yvar,xlab,ylab){
      b<-paste(xlab,"vs",ylab)
      plot(yvar,xvar,
                    pch=c(1,3,4,2,0,8,5),
                    col=c("black","red","blue","darkgreen","purple","orange","gray"),
                    xlab=xlab,ylab=ylab,
                    main=b)
    }
    
    myplot(nfl$Dash40,nfl$BenchPress,"dash","bench")
    

    【讨论】:

    • VarX 用于填充列的名称...... VarY 也是如此......例如,如果我有 myplot(Dash40,BenchPress...) ,该字符串将替换图中的内容( xvar,yvar)
    • 在尝试使用变量时,您似乎将变量括在引号中,例如 "xlab"。这在 R 中不起作用,你最终会创建一个字符串。
    • 嗨,我运行了你建议的代码,这是我收到的消息:总结时出错:找不到对象'dash'
    • 对不起,dash 和 bench 应该被引用,因为它们是字符串而不是对象。我将编辑答案。
    猜你喜欢
    • 1970-01-01
    • 2021-12-26
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多