Qualitative-Qualitative Relationships

This lesson covers relationships between two categorical (a.k.a., qualitative) variables.

Model script

#install.packages("tidyverse")  # uncomment if tidyverse not installed
#install.packages("rio")  # uncomment if rio not installed
#install.packages("gmodels")  # uncomment if gmodels not installed
library(tidyverse)
library(rio)
library(gmodels)

data <- import("https://learndata.net/nycschools.dta")

# we can use the CrossTable function from gmodels to make crosstabs
# first, we create a table using default settings:
CrossTable(data$schooltype, data$overallgrade)

# the table above is very wide and may be difficult to read
# let's switch the order in which we list our two variable so that
# the variable with more values (overallgrade) will be shown as
# rows rather than columns
CrossTable(data$overallgrade, data$schooltype)

# by default, many proportions are shown below each frequency
# let's turn off all proportions except prop.r (row proportions)
CrossTable(data$overallgrade, data$schooltype, prop.c = FALSE,
  prop.t = FALSE, prop.chisq = FALSE)

# an alternative if we prefer seeing column proportions:
CrossTable(data$overallgrade, data$schooltype, prop.r = FALSE,
  prop.t = FALSE, prop.chisq = FALSE)

# to check for statistical significance, we can set chisq to TRUE:
CrossTable(data$overallgrade, data$schooltype, prop.r = FALSE,
  prop.t = FALSE, prop.chisq = FALSE, chisq = TRUE)

# make a simple bar chart with the ggplot2 package (part of core tidyverse)
data |>
  ggplot(aes(overallgrade)) +
  geom_bar()

# filter out the observations where overallgrade is blank (missing)
data |>
  filter(overallgrade != "") |>
  ggplot(aes(overallgrade)) +
  geom_bar()

# create a grouped bar graph, grouping by schooltype
data |>
  filter(overallgrade != "") |>
  ggplot(aes(overallgrade)) +
  geom_bar() +
  facet_wrap(~ schooltype)

# sometimes it is helpful to adjust the nrow option within
# facet_wrap()
# for example, we can use three rows so our subgraphs are
# shown in a column rather than side-by-side
data |>
  filter(overallgrade != "") |>
  ggplot(aes(overallgrade)) +
  geom_bar() +
  facet_wrap(~ schooltype, nrow = 3)

# alternatively, we can specify two variable within aes():
# grade_factor is shown through the "fill" color while
# schooltype is our main x variable
data |>
  filter(overallgrade != "") |>
  ggplot(aes(fill = overallgrade, x = schooltype)) +
  geom_bar(position = "dodge")

# change the label (title) for the "fill" legend to "Grade"
data |>
  filter(overallgrade != "") |>
  ggplot(aes(fill = overallgrade, x = schooltype)) +
  geom_bar(position = "dodge") +
  labs(fill = "Grade")

# we can also display this information in a stacked bar chart
data |>
  filter(overallgrade != "") |>
  ggplot(aes(fill = overallgrade, x = schooltype)) +
  geom_bar(position = "fill") +
  labs(y = "proportion", fill = "Grade")

Key concepts/syntax:

  1. describe(): create a crosstab (contingency table), using the gmodels package

    CrossTable(data$schooltype, data$overallgrade)
  2. ggplot(): a tidyverse function for creating graphs that is very powerful but rather complicated1

    data |>
      ggplot(aes(overallgrade)) +
      geom_bar()
  3. filter(): a tidyverse function to select only certain observations (rows) from a dataset2

    data |>
      filter(overallgrade != "") |>
      ggplot(aes(overallgrade)) +
      geom_bar()

  1. For a more complete explanation, see https://moderndive.com/v2/02-visualization.html↩︎

  2. For more explanation, see https://moderndive.com/v2/03-wrangling.html#sec-filter↩︎