簡體   English   中英

帶R的分類變量的圖

[英]plot of categorical variables with R

我是R和統計領域的新手。 我正在嘗試繪制兩個類別變量的關系,它們是長度為147的向量。每個類別的表是:

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

    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

從2012年開始在這里閱讀問題的答案后,我期望通過使用以下命令來獲得Spineplot:

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

但它返回:

    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

我猜測警告消息1告訴我xycoords()試圖將類別元素強制轉換為數字數據,而如果這樣做失敗,則會返回NA,這將使plot()無法使用。
簡而言之,我無法理解什么以及如何解決?

我想我也可以將我的評論放在答案中:

有一個plot.table方法,所以為什么不嘗試:with(mydata,plot(table(Role,AgeGroup),xlab =“ Role”,ylab =“ Age Group”)– BondedDust 3小時前

with創建一個將列名解釋為對象的本地環境,並在該環境內將“表”對象傳遞給plot plot函數是通用的(嘗試鍵入methods(plot) ),因此它為“ table”類對象查找適當的方法並執行其工作。 要查看未導出的plot.table的代碼,您需要使用getAnywhere(plot.table)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM