r - 绘制分类变量
问题描述
我是 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
我不知道为什么我不能得到与图中所示相同的结果。
解决方案
该绘图命令仅适用于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"]]))
得到相同的结果。
推荐阅读
- c - SDL 可以在 Atari Mint 上运行吗?
- sql - 列出类别中的项目 (SQL)
- javascript - 根据另一个数组数组过滤对象数组
- html - 如何在导航栏中的链接之间包含短垂直线?
- angular - 上传和验证 - Typescript 和 Angular
- laravel - 使用谷歌邮件在 Laravel 中发送电子邮件时出错
- unity3d - 如何让 Unity 检查器将文件扩展名视为不同的文件扩展名?
- java - 以两次更快的速度连接到数据库还是连接一次并将其存储在本地?
- mysql - 从 reddit json 中提取信息并使用 node.js 将其放入 mysql 数据库中
- php - 在子文件夹中导航主页和页面的最佳做法是什么?