首页 > 解决方案 > 绘制分类变量

问题描述

我是 R 新手,我按照说明幻灯片进行绘图:

在此处输入图像描述

survey[["Program"]]是数据框中的分类数据列。

> survey[["Program"]]  # returns the Program column as a vector
 [1] "PPM"   "PPM"   "PPM"   "Other" "PPM"   "PPM"   "PPM"   "Other" "PPM"   "Other" "MISM"  "PPM"   "MISM" 
[14] "Other" "PPM"   "PPM"   "PPM"   "PPM"   "PPM"   "Other" "PPM"   "MISM"  "PPM"   "PPM"   "PPM"   "MISM" 
[27] "PPM"   "Other" "Other" "PPM"   "Other"

但是,当我实施时plot(survey[["Program"]]),我得到了错误:

Error in plot.window(...) : need finite 'ylim' 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

我不知道为什么我不能得到与图中所示相同的结果。

标签: rplot

解决方案


该绘图命令仅适用于factor列。(嗯,它适用于很多事情,但这里的混乱是因为变量不是一个因素。)比较例如

plot(c('a', 'a', 'b', 'b', 'c'))
Error in plot.window(...) : need finite 'ylim' 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

plot(factor(c('a', 'a', 'b', 'b', 'c')))

在此处输入图像描述

您可以通过调用来检查列的类str(survey)。您读入数据的方式可能与假设的幻灯片不同。

您可以使用

plot(factor(survey[["Program"]]))

也许

barplot(table(survey[["Program"]]))

得到相同的结果。


推荐阅读