Challenge 5 Solutions

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

Moira Chiong

Published

June 15, 2023

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

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 ⭐⭐⭐⭐⭐
::: {.cell}

```{.r .cell-code}
library(readr)
AB_NYC_2019 <-read_csv("_data/AB_NYC_2019.csv")
View(AB_NYC_2019)

:::

::: {.cell}

```{.r .cell-code}
install.packages("rmarkdown")
Error in contrib.url(repos, "source"): trying to use CRAN without setting a mirror
library(rmarkdown)

:::

Briefly describe the data

The data pertains to key indicators for AirBnB homes. There are short descriptions including type of home, location, and price per evening. There is also data about the number of customer reviews the unit has had, as well data about availability (365 days a year or less) and host name. The mean minimum cost is around $1284 while the average stay length required is 7 or a week. The homes are most frequently located in Manhattan, followed by Brooklyn , Queens, Bronx and Staten Island. The most common type of home being rented is an entire home/apartment, followed by private rooms and shared rooms.

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.

#Answer The data is already tidy, but for practical accounting ease, I will calculate the total minimum cost per stay using the mutate variable.

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?

See above

Document your work here.

install.packages("dplyr")    # alternative installation of the %>
library(dplyr)    # alternatively, this also loads %>%
airbnb_NYC <-AB_NYC_2019%>%
  mutate(`minimum_cost`= `price`*`minimum_nights`)

airbnb_NYC
# A tibble: 48,895 x 17
      id name       host_id host_name neighbourhood_group neighbourhood latitude
   <dbl> <chr>        <dbl> <chr>     <chr>               <chr>            <dbl>
 1  2539 Clean & q~    2787 John      Brooklyn            Kensington        40.6
 2  2595 Skylit Mi~    2845 Jennifer  Manhattan           Midtown           40.8
 3  3647 THE VILLA~    4632 Elisabeth Manhattan           Harlem            40.8
 4  3831 Cozy Enti~    4869 LisaRoxa~ Brooklyn            Clinton Hill      40.7
 5  5022 Entire Ap~    7192 Laura     Manhattan           East Harlem       40.8
 6  5099 Large Coz~    7322 Chris     Manhattan           Murray Hill       40.7
 7  5121 BlissArts~    7356 Garon     Brooklyn            Bedford-Stuy~     40.7
 8  5178 Large Fur~    8967 Shunichi  Manhattan           Hell's Kitch~     40.8
 9  5203 Cozy Clea~    7490 MaryEllen Manhattan           Upper West S~     40.8
10  5238 Cute & Co~    7549 Ben       Manhattan           Chinatown         40.7
# i 48,885 more rows
# i 10 more variables: longitude <dbl>, room_type <chr>, price <dbl>,
#   minimum_nights <dbl>, number_of_reviews <dbl>, last_review <date>,
#   reviews_per_month <dbl>, calculated_host_listings_count <dbl>,
#   availability_365 <dbl>, minimum_cost <dbl>
#next I'll find the mean minimum cost
mean(airbnb_NYC$minimum_cost,na.rm=TRUE)
[1] 1284.433
mean(airbnb_NYC$minimum_nights, na.rm=TRUE)
[1] 7.029962
table(airbnb_NYC$neighbourhood_group)

        Bronx      Brooklyn     Manhattan        Queens Staten Island 
         1091         20104         21661          5666           373 
table(airbnb_NYC$room_type)

Entire home/apt    Private room     Shared room 
          25409           22326            1160 
## Univariate Visualizations

install.packages("tidyverse")
library(tidyverse)
library(ggplot2)

ggplot(airbnb_NYC, aes(x=`minimum_cost`)) +
  geom_histogram(bins=60) +
  scale_x_continuous(limits = c(0, 7000)) +
  ggtitle("Minimum Cost") +
  labs(y = "Number of Apartments", x = "Minimum Cost Per Listing")

print(vis1)
Error in print(vis1): object 'vis1' not found
vis2 <- ggplot(airbnb_NYC, aes(x=`price`, fill=`room_type`)) +
  geom_bar(width=25) +
  scale_x_continuous(limits = c(0, 1000)) +
  ggtitle("Price per Night by Room Type") +
  labs(y = "Number of Apartments", x = "Cost per Night", fill = "Room Type")
print(vis2)

## Bivariate Visualization(s)

vis3 <- ggplot(airbnb_NYC, aes(x=`minimum_nights`, y=`price`, color=`room_type`)) +
  geom_point() +
  scale_x_continuous(limits = c(0, 500)) +
  ggtitle("Price per Night Divided by Room Type") +
  labs(y = "Cost Per Night", x = "Mimimum Nights", color = "Room Type")
print(vis3)