如何通过dplyr在R上生成频率表并使用ggplot绘制其值?

时间:2018-03-06 01:48:03

标签: r ggplot2 dplyr

我需要从两个分类变量列中做一个频率表,其中一个是5年龄组,另一个是brfss2013数据集中的健康状态(五个状态),我从中通过以下方式提取感兴趣的列:

    genhlth     X_ageg5yr
1   Fair        Age 60 to 64
2   Good        Age 50 to 54
3   Good        Age 55 to 59
4   Very good   Age 60 to 64
5   Good        Age 65 to 69

因此生成一个两列框架,491775个观察2个变量。

> by(hlthgrpq1$genhlth, hlthgrpq1$X_ageg5yr, summary)
hlthgrpq1$X_ageg5yr: Age 18 to 24
Excellent Very good      Good      Fair      Poor      NA's 
     6896     10266      7795      1873       303        69 
---------------------------------------------------------------------------------------------------------------- 
hlthgrpq1$X_ageg5yr: Age 25 to 29
Excellent Very good      Good      Fair      Poor      NA's 
     5779      8488      6521      1751       325        46 
---------------------------------------------------------------------------------------------------------------- 
hlthgrpq1$X_ageg5yr: Age 30 to 34
Excellent Very good      Good      Fair      Poor      NA's 
     6412      9958      7977      2295       496        75 
---------------------------------------------------------------------------------------------------------------- 
hlthgrpq1$X_ageg5yr: Age 35 to 39
Excellent Very good      Good      Fair      Poor      NA's 
     6366     10169      8236      2637       638        61 
---------------------------------------------------------------------------------------------------------------- 
hlthgrpq1$X_ageg5yr: Age 40 to 44
Excellent Very good      Good      Fair      Poor      NA's 
     6689     11130      9193      3334      1067        95 
---------------------------------------------------------------------------------------------------------------- 
hlthgrpq1$X_ageg5yr: Age 45 to 49
Excellent Very good      Good      Fair      Poor      NA's 
     7051     12278     10611      4343      1815       112 
---------------------------------------------------------------------------------------------------------------- 
hlthgrpq1$X_ageg5yr: Age 50 to 54
Excellent Very good      Good      Fair      Poor      NA's 
     8545     15254     13761      6354      3120       139 
---------------------------------------------------------------------------------------------------------------- 
hlthgrpq1$X_ageg5yr: Age 55 to 59
Excellent Very good      Good      Fair      Poor      NA's 
     8500     16759     15394      7643      3998       197 
---------------------------------------------------------------------------------------------------------------- 
hlthgrpq1$X_ageg5yr: Age 60 to 64
Excellent Very good      Good      Fair      Poor      NA's 
     8283     16825     16266      8101      3955       229 
---------------------------------------------------------------------------------------------------------------- 
hlthgrpq1$X_ageg5yr: Age 65 to 69
Excellent Very good      Good      Fair      Poor      NA's 
     7479     15764     15600      7749      3200       205 
---------------------------------------------------------------------------------------------------------------- 
hlthgrpq1$X_ageg5yr: Age 70 to 74
Excellent Very good      Good      Fair      Poor      NA's 
     5491     11943     13125      6491      2721       196 
---------------------------------------------------------------------------------------------------------------- 
hlthgrpq1$X_ageg5yr: Age 75 to 79
Excellent Very good      Good      Fair      Poor      NA's 
     3320      8501     10128      5545      2426       173 
---------------------------------------------------------------------------------------------------------------- 
hlthgrpq1$X_ageg5yr: Age 80 or older
Excellent Very good      Good      Fair      Poor      NA's 
     3697     10285     14400      8116      3695       322 

我可以使用' by'生成摘要表。功能:

cat /Users/*username*/.npm/_logs/2018-03-05T20_17_52_869Z-debug.log

那就是我被卡住的地方。我已经尝试了几个小时试图来到这里:

Results obtained via spreadsheet.

感谢您的帮助。

(这是针对特定的作业,所以我只能使用dplyr和ggplot2,所以,没有reshape2或者tidyr。)

1 个答案:

答案 0 :(得分:0)

首先:对于将来的帖子,总是包含样本数据是一种好习惯。请参阅here how to provide a minimal reproducible example/attempt,包括示例数据。

基地R的解决方案。

as.data.frame.matrix(t(table(df)));
#             Fair Good Very good
#Age 50 to 54    0    1         0
#Age 55 to 59    0    1         0
#Age 60 to 64    1    0         1
#Age 65 to 69    0    1         0

或者像tidyverse方法这样的东西?

library(tidyverse);
df %>% count(genhlth, X_ageg5yr) %>% spread(genhlth, n);
## A tibble: 4 x 4
#  X_ageg5yr     Fair  Good `Very good`
#  <fct>        <int> <int>       <int>
#1 Age 50 to 54    NA     1          NA
#2 Age 55 to 59    NA     1          NA
#3 Age 60 to 64     1    NA           1
#4 Age 65 to 69    NA     1          NA

或者,如果您坚持只使用dplyr而不是tidyr,则可以执行以下操作:

df2 <- df %>%
    count(genhlth, X_ageg5yr);
df2 <- as.data.frame.matrix(xtabs(n ~ X_ageg5yr + genhlth, data = df2));
#             Fair Good Very good
#Age 50 to 54    0    1         0
#Age 55 to 59    0    1         0
#Age 60 to 64    1    0         1
#Age 65 to 69    0    1         0

这基本上归结为一个从长到长的重新格式,因此围绕该主题进行了大量讨论(例如here)。

样本数据

df <- read.table(text =
    "genhlth     X_ageg5yr
Fair        'Age 60 to 64'
Good        'Age 50 to 54'
Good        'Age 55 to 59'
'Very good'   'Age 60 to 64'
Good        'Age 65 to 69'", header = T)