繁体   English   中英

将只绘制因子?

[英]Will only plot Factors?

我正在使用新的数据集,而我始终以

 options(StringsAsFactors = FALSE)

现在的问题是,如果将字符串作为因子选项设置为TRUE,R将仅绘制我设置的数据。

每当我尝试使用Stringsasfactors = FALSE进行绘图时,都会给我下一条错误消息。

plot(Data$Jobs, Data$RXH)
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf

但是当我将Stringsasfactors设置为TRUE时,就可以毫无问题地进行绘制...

这是脚本。

#Setting WD.

getwd()
setwd("C:/Windows/System32/config/systemprofile/Documents/R proj")


options(stringsAsFactors = F)
get <- read.csv("WorkExcelR.csv", header = TRUE, sep = ",")
Data <- na.omit(get)

这是Data $ Jobs和Data $ RXH

> Data$Jobs
[1] "Playstation"  "RWC Heineken" "Jagermeister" "RWC Heineken"
[5] "RWC Heineken" "RWC Heineken"
> Data$RXH
[1]  90  90 100  90  90  90

您要说明的问题是由于存在plot.factor函数但没有plot.character函数。 您可以通过键入以下内容来查看可用的绘图方法:

methods(plot)  

?plot的帮助页面中对此没有特别好的描述,但是对于?plot.factor有一个单独的帮助页面。 R中的函数根据其参数进行分派:S3函数仅基于其第一个参数的类,而S4方法则基于其参数签名。 从某种意义上说, plot.factor函数详细说明了该策略,因为它然后也基于第二个参数的类(假设它与位置匹配或命名为y也基于第二个参数的类调度到不同的绘图例程。

您有两种选择:强制使用plot方法,然后需要使用::: infix函数进行校准,因为不会导出plot.factor,也不会自己强制执行或调用更特定的绘图类型。

graphics:::plot.factor(Data$Jobs, Dat
plot(factor(Data$Jobs), Data$RXH)
boxplot(Data$RXH ~Data$Jobs)    # which is the result if x is factor and y is numeric

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM