Week 1 Assignment: Code Base

Author

Jocelyn Slater

Introduction

I will be using this tree data set from the City of New York for my first assignment to explore the relationship between borough and tree health. As a New Yorker myself, I am curious to see how location, tree density, and tree health are correlated.

# This will read in the first 1000 lines of the data set. 1000 lines is a limit set by the city website of how much can be read in this way. Alternatively I could download the whole data set as a .csv and source from github
url <- "https://data.cityofnewyork.us/resource/uvpi-gqnh.csv"
data_raw <- read_csv(url, show_col_types = FALSE)
head(data_raw)
# A tibble: 6 × 45
  tree_id block_id created_at          tree_dbh stump_diam curb_loc status
    <dbl>    <dbl> <dttm>                 <dbl>      <dbl> <chr>    <chr> 
1  180683   348711 2015-08-27 00:00:00        3          0 OnCurb   Alive 
2  200540   315986 2015-09-03 00:00:00       21          0 OnCurb   Alive 
3  204026   218365 2015-09-05 00:00:00        3          0 OnCurb   Alive 
4  204337   217969 2015-09-05 00:00:00       10          0 OnCurb   Alive 
5  189565   223043 2015-08-30 00:00:00       21          0 OnCurb   Alive 
6  190422   106099 2015-08-30 00:00:00       11          0 OnCurb   Alive 
# ℹ 38 more variables: health <chr>, spc_latin <chr>, spc_common <chr>,
#   steward <chr>, guards <chr>, sidewalk <chr>, user_type <chr>,
#   problems <chr>, root_stone <chr>, root_grate <chr>, root_other <chr>,
#   trunk_wire <chr>, trnk_light <chr>, trnk_other <chr>, brch_light <chr>,
#   brch_shoe <chr>, brch_other <chr>, address <chr>, zipcode <dbl>,
#   zip_city <chr>, cb_num <dbl>, borocode <dbl>, boroname <chr>,
#   cncldist <dbl>, st_assem <dbl>, st_senate <dbl>, nta <chr>, …

Data Tidying

# Subselect columns
df_sub <- data_raw[, c("tree_id", "tree_dbh", "status", "health", "root_stone", "root_grate", "root_other", "brch_light", "brch_shoe", "brch_other", "borocode", "boroname")]

# Make new root_health column
df_sub$root_problem <- ifelse(df_sub$root_stone == "Yes" | df_sub$root_grate == "Yes" | df_sub$root_other == "Yes", "yes", "no")

# Make new branch_health column
df_sub$branch_problem <- ifelse(df_sub$brch_light == "Yes" | df_sub$brch_shoe == "Yes" | df_sub$brch_other == "Yes", "yes", "no")

# Rename columns
df_sub <- df_sub %>% rename(tree_diameter_in = tree_dbh)

# DO NOT Remove rows with NA values in health column (these correspond to a dead tree/stump and therefore have important values we can't lose for the analysis)
# df_clean <- drop_na(df_sub, health)
# Change NA values to "Dead"
df_clean <- df_sub %>% 
  mutate(health = replace_na(health, "Dead"))

Initial Analysis

ggplot(df_clean, aes(x = boroname, fill = status)) +
  geom_bar() +
  scale_fill_brewer(palette = "RdYlGn", direction = -1) +
  labs(
    title = "Tree Status by Borough",
    x = "Borough",
    y = "Number of Trees",
    fill = "Status"
  ) +
  theme_minimal()

ggplot(df_clean, aes(x = boroname, fill = factor(health, levels = c("Good", "Fair", "Poor", "Dead")))) +
  geom_bar() +
  scale_fill_brewer(palette = "RdYlGn", direction = -1) +
  labs(
    title = "Overall Tree Health by Borough",
    x = "Borough",
    y = "Number of Trees",
    fill = "Overall Health"
  ) +
  theme_minimal()

Conclusion

Although more quantitative analysis will have to be done to make firm conclusions, a cursory qualitative analysis shows that Queens and Brooklyn seem to have a higher proportion of trees that are dead or in poor health. Future work should include exploring these relationships per borough area so we can determine the density of trees in each borough. Analysis should also be expanded to include all of the data available. The NYC Open Data has a default limit of providing 1,000 rows. To get around this, the entire .csv data set should be exported, uploaded to github, and sourced from there directly.