BaşlayınÜcretsiz Başlayın

Survey-weighted density plots

We can also explore the shape of a variable with the smooth curve of a density plot. Let's create a density plot of nightly sleep where we facet by gender. Whereas the height of the histogram bars represent counts, the height of the density curve represents probabilities. Therefore, we will need to do some data wrangling before we create the plot.

Bu egzersiz

Analyzing Survey Data in R

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • For NHANESraw, remove rows that are missing SleepHrsNight or Gender.
  • Grouping by Gender, add the column WTMEC4YR_std which equals WTMEC4YR/sum(WTMEC4YR).
  • Pipe your wrangled data directly into a ggplot() where SleepHrsNight is mapped to x and WTMEC4YR_std is mapped to weight. Include a density layer with bw = 0.6 and fill = "gold" and a facetting layer where you facet by Gender.

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

# Density plot of sleep faceted by gender
NHANESraw %>%
    ___(!is.na(___), !is.na(___)) %>%
    group_by(___) %>%
    mutate(WTMEC4YR_std = ___) %>%
    ggplot(mapping = aes(x = ___, weight = ___)) + 
        geom____(bw = 0.6,  fill = "gold") +
        labs(x = "Hours of Sleep") + 
        ____wrap(~___, labeller = "label_both")
Kodu Düzenle ve Çalıştır