简体   繁体   English

带R的分类变量的图

[英]plot of categorical variables with R

I'm a newcomer both to R and statistics in general. 我是R和统计领域的新手。 I'm trying to plot the relationship of two categorical variables which are vectors of length 147. The tables for each are: 我正在尝试绘制两个类别变量的关系,它们是长度为147的向量。每个类别的表是:

    AgeGroup
26-49   50-64   65 and over  Under 26
101     16      5            25

and

    Role
A/C     C/H                     Dietician
6       8                       2
Doctor  Healthcare Assistant    Lab
56      7                       2
Nurse   Pharmacist              Phlebotomist
54      5                       1
Radiographer                    Therapist
1                               5

After reading an answer to a question here from 2012 I expected to get a spineplot by using this command: 从2012年开始在这里阅读问题的答案后,我期望通过使用以下命令来获得Spineplot:

    plot(mydata$Role,mydata$AgeGroup,xlab="Role",ylab="Age Group")

but it returns: 但它返回:

    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 xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf
5: In min(x) : no non-missing arguments to min; returning Inf
6: In max(x) : no non-missing arguments to max; returning -Inf

I'm guessing that Warning message 1 is telling me that xycoords() is attempting to coerce the categorial elements into numeric data and when it fails to do so is returning NAs, which gives plot() nothing to work with. 我猜测警告消息1告诉我xycoords()试图将类别元素强制转换为数字数据,而如果这样做失败,则会返回NA,这将使plot()无法使用。
In short, what am I failing to understand and how do I fix it? 简而言之,我无法理解什么以及如何解决?

I guess I might as well put my comments in an answer: 我想我也可以将我的评论放在答案中:

There is a plot.table method so why not try:with( mydata, plot( table(Role, AgeGroup), xlab="Role",ylab="Age Group") – BondedDust 3 hours ago 有一个plot.table方法,所以为什么不尝试:with(mydata,plot(table(Role,AgeGroup),xlab =“ Role”,ylab =“ Age Group”)– BondedDust 3小时前

with creates a local environment where the column names are interpreted as objects, and within that environment, a 'table'-object is passed to plot . with创建一个将列名解释为对象的本地环境,并在该环境内将“表”对象传递给plot The plot function is generic (try typing methods(plot) ) so it looks up the proper method for the 'table'-classed object and does its work. plot函数是通用的(尝试键入methods(plot) ),因此它为“ table”类对象查找适当的方法并执行其工作。 To see the code for plot.table which is not exported, you need to use getAnywhere(plot.table) 要查看未导出的plot.table的代码,您需要使用getAnywhere(plot.table)

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

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