Challenge 5

challenge_5
railroads
cereal
air_bnb
pathogen_cost
australian_marriage
public_schools
usa_households
Introduction to Visualization
Author

Nisarg Shah

Published

August 22, 2022

library(tidyverse)
library(ggplot2)

knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)

Challenge Overview

Today’s challenge is to:

  1. read in a data set, and describe the data set using both words and any supporting information (e.g., tables, etc)
  2. tidy data (as needed, including sanity checks)
  3. mutate variables as needed (including sanity checks)
  4. create at least two univariate visualizations
  • try to make them “publication” ready
  • Explain why you choose the specific graph type
  1. Create at least one bivariate visualization
  • try to make them “publication” ready
  • Explain why you choose the specific graph type

R Graph Gallery is a good starting point for thinking about what information is conveyed in standard graph types, and includes example R code.

(be sure to only include the category tags for the data you use!)

Read in data

Read in one (or more) of the following datasets, using the correct R package and command.

  • cereal.csv ⭐
  • Total_cost_for_top_15_pathogens_2018.xlsx ⭐
  • Australian Marriage ⭐⭐
  • AB_NYC_2019.csv ⭐⭐⭐
  • StateCounty2012.xls ⭐⭐⭐
  • Public School Characteristics ⭐⭐⭐⭐
  • USA Households ⭐⭐⭐⭐⭐
df <- read.csv("_data/cereal.csv", fileEncoding = "UTF-8")
head(df)
               Cereal Sodium Sugar Type
1 Frosted Mini Wheats      0    11    A
2         Raisin Bran    340    18    A
3            All Bran     70     5    A
4         Apple Jacks    140    14    C
5      Captain Crunch    200    12    C
6            Cheerios    180     1    C

Briefly describe the data

Tidy Data (as needed)

Is your data already tidy, or is there work to be done? Be sure to anticipate your end result to provide a sanity check, and document your work here.

The given data is already tidy.

Are there any variables that require mutation to be usable in your analysis stream? For example, do you need to calculate new values in order to graph them? Can string values be represented numerically? Do you need to turn any variables into factors and reorder for ease of graphics and visualization?

Document your work here.

the “Type” variable is a categorical variable with two levels: “A” and “C”. It would be useful to convert this variable into a factor and reorder the levels for ease of graphics and visualization.

df$Type <- factor(df$Type)

# Reorder the levels of "Type"
levels(df$Type) <- c("C", "A")

Univariate Visualizations

ggplot(df, aes(x = Sugar)) +
  geom_histogram() +
  labs(title = "Histogram of Sugar Content in Cereals",
       x = "Sugar Content (g per serving)", y = "Frequency")

# Boxplot of sodium content by type of cereal
ggplot(df, aes(x = Type, y = Sodium)) +
  geom_boxplot() +
  labs(title = "Boxplot of Sodium Content by Cereal Type",
       x = "Cereal Type", y = "Sodium Content (mg per serving)")

Bivariate Visualization(s)

ggplot(df, aes(x = Sugar, y = Sodium, color = Type)) +
  geom_point() +
  ggtitle("Sodium vs Sugar content in Cereals by Type") +
  xlab("Sugar Content") +
  ylab("Sodium Content")

Any additional comments?