可视化分类变量

用 R 分析调查数据

Kelly McConville

Assistant Professor of Statistics

NHANES:可视化种族

library(dplyr)
tab_unw <- NHANESraw %>% group_by(Race1) %>%
   summarize(Freq = n()) %>% mutate(Prop = Freq / sum(Freq)) %>%
   arrange(desc(Prop))

tab_unw
# A tibble: 5 x 3
     Race1  Freq      Prop
    <fctr> <int>     <dbl>
 1    White  7393 0.3643128
 2    Black  4640 0.2286503
 3  Mexican  3739 0.1842507
 4    Other  2312 0.1139309
 5 Hispanic  2209 0.1088553

NHANES 文档

用 R 分析调查数据

NHANES:可视化种族

library(ggplot2)
ggplot(data = tab_unw, mapping = aes(x = Race1, y = Prop)) + 
  geom_col() + 
  coord_flip() + 
  scale_x_discrete(limits = tab_unw$Race1) # Labels layer omitted

未加权的种族分布条形图

用 R 分析调查数据

NHANES:可视化种族

library(ggplot2)
ggplot(data = tab_unw, mapping = aes(x = Race1, y = Prop)) + 
  geom_bar(stat = "identity") + 
  coord_flip() + 
  scale_x_discrete(limits = tab_unw$Race1) # Labels layer omitted

未加权的种族分布条形图

用 R 分析调查数据

NHANES:可视化种族

library(ggplot2)
ggplot(data = tab_unw, mapping = aes(x = Race1, y = Prop)) + 
  geom_col() + 
  coord_flip() + 
  scale_x_discrete(limits = tab_unw$Race1) # Labels layer omitted

未加权的种族分布条形图

用 R 分析调查数据

NHANES:可视化种族

tab_w <- svytable(~Race1, design = NHANES_design) %>%
   as.data.frame() %>%
   mutate(Prop = Freq / sum(Freq)) %>%
   arrange(desc(Prop))

tab_w
     Race1      Freq       Prop
1    White 193966274 0.63748664
2    Black  37241616 0.12239773
3  Mexican  30719158 0.10096112
4    Other  23389002 0.07686994
5 Hispanic  18951150 0.06228456
用 R 分析调查数据

NHANES:可视化种族

ggplot(data = tab_w, mapping = aes(x = Race1, y = Prop)) + 
  geom_col() + 
  coord_flip() + 
  scale_x_discrete(limits = tab_w$Race1) # Labels layer omitted

基于抽样权重的种族分布条形图

用 R 分析调查数据

Passons à la pratique !

用 R 分析调查数据

Preparing Video For Download...