只会积因子吗?

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 函数这一事实。您可以通过键入以下内容查看可用的 plot.-methods:

methods(plot)  

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

您有几个选择:强制绘图方法,然后需要使用 ::: 中缀函数进行缩放,因为 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