【发布时间】: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