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.
This is a part of the course
“Analyzing Survey Data in R”
Exercise instructions
- For
NHANESraw
, remove rows that are missingSleepHrsNight
orGender
. - Grouping by
Gender
, add the columnWTMEC4YR_std
which equalsWTMEC4YR/sum(WTMEC4YR)
. - Pipe your wrangled data directly into a
ggplot()
whereSleepHrsNight
is mapped tox
andWTMEC4YR_std
is mapped to weight. Include a densitylayer
withbw = 0.6
andfill = "gold"
and a facetting layer where you facet byGender
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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")
This exercise is part of the course
Analyzing Survey Data in R
Learn survey design using common design structures followed by visualizing and analyzing survey results.
Of course not all survey data are categorical and so in this chapter, we will explore analyzing quantitative survey data. We will learn to compute survey-weighted statistics, such as the mean and quantiles. For data visualization, we'll construct bar-graphs, histograms and density plots. We will close out the chapter by conducting analytic inference with survey-weighted t-tests.
Exercise 1: Summarizing quantitative dataExercise 2: Survey statisticsExercise 3: Estimating quantilesExercise 4: Visualizing quantitative dataExercise 5: Bar plots of survey-weighted meansExercise 6: Output of svyby()Exercise 7: Bar plots with errorExercise 8: Survey-weighted histogramsExercise 9: Survey-weighted density plotsExercise 10: Inference for quantitative dataExercise 11: Survey-weighted t-testExercise 12: Tying it all together!What is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.