library(tidyverse)
2020 Durham City and County Resident Survey
The main questions we’ll explore today are:
“What are the demographics and priorities of City of Durham residents?”
“How do City of Durham residents feel about quality of services by the city and how, if at all, does that vary by income?”
Goals
Wrapping up discussion on categorical data
Visualizing and summarizing numerical data
Improving visualizations for visual appeal and better communication
Packages
Data
The data for this case study come from the 2020 Durham City and County Resident Survey.
First, let’s load the data:
<- read_csv("data/durham-2020.csv") durham
Rows: 803 Columns: 49
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (7): please_define_other_32_5, primary_language, please_define_other_34...
dbl (42): id, overall_quality_of_services_3_01, overall_quality_of_services_...
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
In the last application exercise we manipulated the data a bit.
<- durham |>
durham rename(
own_rent = do_you_own_or_rent_your_current_resi_31,
income = would_you_say_your_total_annual_hous_35,
# a new one
qos_city = overall_quality_of_services_3_01
|>
) mutate(
income = as_factor(income),
own_rent = as_factor(own_rent)
)
Visualizing relationships between categorical variables
Exercise 1
Visualize and describe the relationship between income and home ownership of Durham residents.
Stretch goal: Customize the colors using named colors from http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf.
Add your answer here.
# add code here
Exercise 2
Calculate the proportions of home owners for each category of Durham residents. Describe the relationship between these two variables, this time with the actual values from the conditional distribution of home ownership based on income level.
Add your answer here.
# add code here
Exercise 3
Recode the levels of these two variables to be more informatively labeled and calculate the proportions from the previous exercise again.
# add code here
Visualizing numerical data
Exercise 4
One of the questions on the survey is “How satisfied are you with the overall quality of services provided by the city?” A response of 1 indicates Very Dissatisfied and a response of 5 indicates Very Satisfied. The responses for this question are in the variable qos_city
. This could be considered an ordinal, categorical variable but can also be treated as a numerical variable in an analysis. Let’s do the latter!
Visualize and describe the distribution of qos_city
. If you get a warning with your visualization, comment on what it means.
Add your response here.
# add code here
Exercise 5
Calculate the mean and median of the distribution of qos_city
. If these values are not exactly the same, can you explain what the difference might be attributed to?
Add your response here.
# add code here
Exercise 6
Based on the shape of the distribution of qos_city
, which measure of spread (variability) is more appropriate? Calculate that value and interpret it in context of the data.
# add code here
Exercise 7
Make a box plot of qos_city
and comment on how the values you calculated map on to the box plot.
# add code here
Exercise 8
How the average level of happiness with quality of services provided by the city vary by income group?
Stretch goal: Visualize mean qos_city
by income group.
Add your response here.
# add code here
Recap
Conceptual
Some of the terms we introduced are:
Marginal distribution: Distribution of a single variable.
Conditional distribution: Distribution of a variable conditioned on the values (or levels, in the context of categorical data) of another.
R
In this application exercise we:
- Defined factors – the data type that R uses for categorical variables, i.e., variables that can take on values from a finite set of levels.
- Reviewed data imports, visualization, and wrangling functions encountered before:
- Import:
read_csv()
: Read data from a CSV (comma separated values) file - Visualization:
ggplot()
: Create a plot using the ggplot2 packageaes()
: Map variables from the data to aesthetic elements of the plot, generally passed as an argument toggplot()
or togeom_*()
functions (define onlyx
ory
aesthetic)geom_bar()
: Represent data with bars, after calculating heights of bars under the hoodgeom_histogram()
: Represent data with a histogramgeom_boxplot()
: Represent data with a box plotgeom_point()
: Represent data with pointslabs()
: Labelx
axis,y
axis, legend forcolor
of plot, title` of plot, etc.
- Wrangling:
mutate()
: Mutate the data frame by creating a new column or overwriting one of the existing columnscount()
: Count the number of observations for each level of a categorical variable (factor) or each distinct value of any other type of variablegroup_by()
: Perform each subsequent action once per each group of the variable, where groups can be defined based on the levels of one or more variablessummarize()
: Calculate summary statistics
- Import:
- Introduced new data wrangling functions:
rename()
: Rename columns in a data frameas_factor()
: Convert a variable to a factordrop_na()
: Drop rows that haveNA
in one ore more specified variablesif_else()
: Write logic for what happens if a condition is true and what happens if it’s notcase_when()
: Write a generalizedif_else()
logic for more than one conditionfct_relevel
: Change the order of levels in a factor
- Introduced new data visualization functions:
geom_col()
: Represent data with bars (columns), for heights that have already been calculated (must definex
andy
aesthetics)scale_fill_viridis_d()
: Customize the discretefill
scale, using a color-blind friendly, ordinal discrete color scalescale_y_discrete()
: Customize the discretey
scalescale_fill_manual()
: Customize thefill
scale by manually adjusting values for colors
Quarto
We also introduced chunk options for managing figure sizes:
fig-width
: Width of figurefig-asp
: Aspect ratio of figure (height / width)fig-height
: Height of figure – but I recommend usingfig-width
andfig-asp
, instead offig-width
andfig-height
Acknowledgements
This dataset was cleaned and prepared for analysis by Duke StatSci PhD student Sam Rosen.