I am exploring the 2015 NYC Tree Census to discover interesting information and to develop hypotheses based on the data.
# Group the data by 'boroname' and count the number of trees in each borough
Tree2015_boro <- Tree2015_csv %>%
group_by(boroname) %>%
tally()
# Create a bar chart with different colors for each borough and custom legend and title
ggplot(data = Tree2015_boro, aes(x = reorder(boroname, n), y = n, fill = boroname)) +
geom_bar(stat = "identity") +
geom_text(aes(label = n), vjust = -0.5) + # Add text labels on top of bars
scale_fill_discrete(labels = c("Bronx (~42.7)","Brooklyn (~69.4)", "Manhattan (~22.7)", "Queens (~108.9)", "Staten Island (~57.5)")) +
labs(title = "Number of Trees in Each Borough",
x = "Borough",
y = "Count",
fill = "Boroughs ~Square Miles") + # Setting the title for the legend
theme(axis.text.x = element_text(angle = 45, hjust = 1))
This map illustrates the areas of New York City with the highest concentration of trees. I merged the 2015 Tree Census dataset with a shapefile of NYC by zip code to create this map. By doing so, you can visualize the tree data on a drawn map of NYC.
Tree2015_dt <-fread("~/Documents/Rstudio/Trees_nyc/data/2015StreetTreesCensus_TREES.csv")
nyc_sf <- st_read( "~/Documents/Rstudio/Trees_nyc/ZIP_CODE_040114")
# Rename the "ZIPCODE" column in nyc_sf to "zipcode"
nyc_sf <- nyc_sf %>%
dplyr::rename(zipcode = ZIPCODE)
# Calculate total trees in each ZIP code
tree_counts_by_zip <- Tree2015_dt[, .(Total_Trees = .N), by = .(zipcode)]
# Convert "zipcode" to character and then numeric
nyc_sf <- nyc_sf %>%
dplyr::mutate(zipcode = as.character(zipcode),
zipcode = as.numeric(zipcode))
# Merge on common variable, here called 'zipcode'
tree_counts_by_zip <-nyc_sf %>%
merge(tree_counts_by_zip, by = "zipcode")
# Filter out zip codes with less than 2000 trees
tree_counts_filtered <- tree_counts_by_zip %>%
filter(Total_Trees >= 200)
# Plot the map
ggplot() +
geom_sf(data = tree_counts_filtered, aes(fill = Total_Trees)) +
scale_fill_gradient(low = "White", high = "#325425", breaks = seq(0, max(tree_counts_filtered$Total_Trees), by = 6000)) +
labs(title = "Concentration of Trees by Zip Code") +
theme_minimal() +
theme(panel.grid = element_blank(),
panel.border = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank())
Tree2015 <- Tree2015_csv %>%
filter(spc_common != "NA")
# Group the data by 'spc_common' and count the occurrences
tree_counts_15 <- Tree2015 %>%
group_by(spc_common) %>%
tally()
# Convert tree_counts_15 to a data frame explicitly
tree_counts_15 <- as.data.frame(tree_counts_15)
# Sort the tree species by their occurrence count in descending order
tree_counts_15 <- tree_counts_15 %>%
arrange(desc(n))
# Plot the top 10 most popular trees using ggplot2
top_10_trees <- head(tree_counts_15, 10)
# Corrected code using -n for reordering in descending order
ggplot(data = top_10_trees, aes(x = reorder(spc_common, -n), y = n)) +
geom_bar(stat = "identity", fill = "#7D8F69") +
labs(title = "Top 10 Tree species in NYC",
x = "Tree Species",
y = "Count") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
Tree2015 <- Tree2015 %>%
filter(spc_common != "NA")
# Group the data by 'spc_common' and count the occurrences
manhattan_Tree2015 <- Tree2015 %>%
group_by(spc_common) %>%
filter(boroname == "Manhattan") %>%
tally()
# Convert tree_counts_15 to a data frame explicitly
manhattan_Tree2015 <- as.data.frame(manhattan_Tree2015)
# Sort the tree species by their occurrence count in descending order within Manhattan
manhattan_Tree2015 <- manhattan_Tree2015 %>%
arrange(desc(n))
# Filter to get the top 10 trees in Manhattan
top_manhattan_tree_counts_15 <- head(manhattan_Tree2015, 10)
# Plot the top 10 most popular trees in Manhattan using ggplot2
ggplot(data = top_manhattan_tree_counts_15, aes(x = reorder(spc_common, -n), y = n)) +
geom_bar(stat = "identity", fill = "#7D8F69") +
labs(title = "Top 10 Tree Species in Manhattan",
x = "Tree Species",
y = "Count") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
bronx_Tree2015 <- Tree2015 %>%
filter(spc_common != "NA")
# Group the data by 'spc_common' and count the occurrences
bronx_tree_counts_15 <- bronx_Tree2015 %>%
group_by(spc_common) %>%
filter(boroname == "Bronx") %>%
tally()
# Convert tree_counts_15 to a data frame explicitly
bronx_tree_counts_15 <- as.data.frame(bronx_tree_counts_15)
# Sort the tree species by their occurrence count in descending order
bronx_tree_counts_15 <- bronx_tree_counts_15 %>%
arrange(desc(n))
# Plot the top 10 most popular trees in the bronx using ggplot2
top_bronx_tree_counts_15 <- head(bronx_tree_counts_15, 10)
# Corrected code using -n for reordering in descending order
ggplot(data = top_bronx_tree_counts_15, aes(x = reorder(spc_common, -n), y = n)) +
geom_bar(stat = "identity", fill = "#7D8F69") +
labs(title = "Top 10 Tree species in the Bronx",
x = "Tree Species",
y = "Count") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
Tree2015 <- Tree2015 %>%
filter(spc_common != "NA")
# Group the data by 'spc_common' and count the occurrences
staten_island_Tree2015 <- Tree2015 %>%
group_by(spc_common) %>%
filter(boroname == "Staten Island") %>%
tally()
# Convert tree_counts_15 to a data frame explicitly
staten_island_Tree2015 <- as.data.frame(staten_island_Tree2015)
# Sort the tree species by their occurrence count in descending order within Staten Island
staten_island_Tree2015 <- staten_island_Tree2015 %>%
arrange(desc(n))
# Filter to get the top 10 trees in Staten Island
top_staten_island_tree_counts_15 <- head(staten_island_Tree2015, 10)
# Plot the top 10 most popular trees in Staten Island using ggplot2
ggplot(data = top_staten_island_tree_counts_15, aes(x = reorder(spc_common, -n), y = n)) +
geom_bar(stat = "identity", fill = "#7D8F69") +
labs(title = "Top 10 Tree Species in Staten Island",
x = "Tree Species",
y = "Count") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
brooklyn_Tree2015 <- Tree2015_csv %>%
filter(spc_common != "NA")
# Group the data by 'spc_common' and count the occurrences
brooklyn_tree_counts_15 <- brooklyn_Tree2015 %>%
group_by(spc_common) %>%
filter(boroname == "Brooklyn") %>%
tally()
# Convert tree_counts_15 to a data frame explicitly
brooklyn_tree_counts_15 <- as.data.frame(brooklyn_tree_counts_15)
# Sort the tree species by their occurrence count in descending order within Brooklyn
brooklyn_tree_counts_15 <- brooklyn_tree_counts_15 %>%
arrange(desc(n))
# Filter to get the top 10 trees in Brooklyn
top_brooklyn_tree_counts_15 <- head(brooklyn_tree_counts_15, 10)
# Plot the top 10 most popular trees in Brooklyn using ggplot2
ggplot(data = top_brooklyn_tree_counts_15, aes(x = reorder(spc_common, -n), y = n)) +
geom_bar(stat = "identity", fill = "#7D8F69") +
labs(title = "Top 10 Tree Species in Brooklyn",
x = "Tree Species",
y = "Count") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
Tree2015 <- Tree2015_csv %>%
filter(spc_common != "NA")
# Group the data by 'spc_common' and count the occurrences
queens_Tree2015 <- Tree2015 %>%
group_by(spc_common) %>%
filter(boroname == "Queens") %>%
tally()
# Convert queens_Tree2015 to a data frame explicitly
queens_Tree2015 <- as.data.frame(queens_Tree2015)
# Sort the tree species by their occurrence count in descending order within Queens
queens_Tree2015 <- queens_Tree2015 %>%
arrange(desc(n))
# Filter to get the top 10 trees in Queens
top_queens_island_tree_counts_15 <- head(queens_Tree2015, 10)
# Plot the top 10 most popular trees in Brooklyn using ggplot2
ggplot(data = top_queens_island_tree_counts_15, aes(x = reorder(spc_common, -n), y = n)) +
geom_bar(stat = "identity", fill = "#7D8F69") +
labs(title = "Top 10 Tree Species in Queens",
x = "Tree Species",
y = "Count") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
Tree2015_csv <- Tree2015_csv %>%
filter(spc_common != "NA")
# Group the data by 'spc_common' and count the occurrences
Rare_Trees <- Tree2015_csv %>%
group_by(spc_common) %>%
tally() %>%
arrange(n) %>% # Order by occurrences in ascending order
head(5)
# Rename the columns of the Rare_Trees data frame
Rare_Trees <- Rare_Trees %>%
rename(Species = spc_common, `Number Counted` = n)
# Print the ordered list of rare trees without table labels
Rare_Trees %>%
kable() %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"), full_width = FALSE)
| Species | Number Counted |
|---|---|
| Virginia pine | 10 |
| Scots pine | 25 |
| Osage-orange | 29 |
| pitch pine | 33 |
| black pine | 37 |
In the 2015 Tree census only 10 Virginia Pine’s were counted.
According the The New York Natural Heritage Program, the Virginia Pine
is list as endangered and is in imminent danger of extripation in New
York state. Though this tree is commenly found in other parts of the
country its future in the state of New York is unknown.
Virginia_Pine <- Tree2015_csv %>%
filter(spc_common == "Virginia pine")
coordinates(Virginia_Pine) = c("longitude","Latitude")
crs.geo1 = CRS("+proj=longlat")
proj4string(Virginia_Pine) = crs.geo1
nyc_sf <- readOGR( "~/Documents/Rstudio/Trees_nyc/nyc_shape_file")
plot(nyc_sf)
points(x=Virginia_Pine$longitude, y=Virginia_Pine$Latitude, col = "#5F8D4E",pch = 16,cex=1)
library(sp)
Virginia_Pine <- Tree2015_csv %>%
filter(spc_common == "Virginia pine")
# Create a SpatialPointsDataFrame
Virginia_Pine_sp <- SpatialPointsDataFrame(
coords = Virginia_Pine[, c("longitude", "Latitude")],
data = Virginia_Pine,
proj4string = CRS("+proj=longlat")
)
nyc_sf <- readOGR("~/Documents/Rstudio/Trees_nyc/nyc_shape_file")
# Calculate the bounding box for the "Virginia Pine" points
bbox_vp <- bbox(Virginia_Pine_sp)
# Check for valid bounding box values
if (all(is.finite(bbox_vp))) {
# Plot with zoomed-in area
plot(nyc_sf, xlim = bbox_vp[1, ], ylim = bbox_vp[2, ])
points(Virginia_Pine_sp, col = "Red", pch = 16, cex = 1)
} else {
cat("Invalid or missing bounding box values.")
}
Interactive Map of Virginia Pine Locations
Virginia_Pine <- Tree2015_csv %>%
filter(spc_common == "Virginia pine")
Virginia_Pine_sf <- st_as_sf(x = Virginia_Pine,
coords = c("longitude","Latitude"),
crs = 4326) %>%
st_transform(crs = 2263)
# create plot
mapview_Virginia_Pine <- Virginia_Pine_sf %>%
mapview(
col.regions = "#5F8D4E",
legend = T,
layer.name = "Location of Virginia Pine",
alpha = 0.8
)
# view results
mapview_Virginia_Pine