Course Notes: Spatial Statistics in R
  • AI Chat
  • Code
  • Report
  • Beta
    Spinner

    PLAN

    1 Total - map

    2a Trees per km2 - map and table

    2b Most common trees - chart

    3 Top 10 trees vs nta - chart

    4 Curb location

    5 Health - All trees

    6 Health - 10 most common trees

    8 Health - map of nta (add legend)

    9 Trunk

    10 Recommended trees - table with ratings

    11 Recommended trees - map

    12 Recommended trees - details table

    12 Appendix - dead trees

    #install.packages('ggmap') #install.packages('sf') #install.packages('cowplot') #install.packages('jpeg') #install.packages('hrbrthemes') #install.packages('formattable') #webshot::install_phantomjs() #install.packages('kableExtra') library(kableExtra) library(ggmap) library(tidyverse) library(sf) library(cowplot) library(gridExtra) library(grid) library(RColorBrewer) library(jpeg) library(hrbrthemes) library(formattable) library(webshot)

    Which tree species should the city plant?

    Background

    You work for a nonprofit organization advising the planning department on ways to

    improve the quantity and quality of trees in New York City.

    The urban design team believes tree size (using trunk diameter as a proxy for size)

    and health are the most desirable characteristics of city trees.

    The city would like to learn more about which tree species are

    the best choice to plant on the streets of Manhattan.

    Tree Census

    - "tree_id" - Unique id of each tree.

    - "tree_dbh" - The diameter of the tree in inches measured at 54 inches above the ground.

    - "curb_loc" - Location of the tree bed in relation to the curb. Either along the curb (OnCurb) or offset from the curb (OffsetFromCurb).

    - "spc_common" - Common name for the species.

    - "status" - Indicates whether the tree is alive or standing dead.

    - "health" - Indication of the tree's health (Good, Fair, and Poor).

    - "root_stone" - Indicates the presence of a root problem caused by paving stones in the tree bed.

    - "root_grate" - Indicates the presence of a root problem caused by metal grates in the tree bed.

    - "root_other" - Indicates the presence of other root problems.

    - "trunk_wire" - Indicates the presence of a trunk problem caused by wires or rope wrapped around the trunk.

    - "trnk_light" - Indicates the presence of a trunk problem caused by lighting installed on the tree.

    - "trnk_other" - Indicates the presence of other trunk problems.

    - "brch_light" - Indicates the presence of a branch problem caused by lights or wires in the branches.

    - "brch_shoe" - Indicates the presence of a branch problem caused by shoes in the branches.

    - "brch_other" - Indicates the presence of other branch problems.

    - "postcode" - Five-digit zip code where the tree is located.

    - "nta" - Neighborhood Tabulation Area (NTA) code from the 2010 US Census for the tree.

    - "nta_name" - Neighborhood name.

    - "latitude" - Latitude of the tree, in decimal degrees.

    - "longitude" - Longitude of the tree, in decimal degrees.

    Neighborhoods' geographical information

    - "ntacode" - NTA code (matches Tree Census information).

    - "ntaname" - Neighborhood name (matches Tree Census information).

    - "geometry" - Polygon that defines the neighborhood.

    Challenge

    Create a report that covers the following:

    * What are the most common tree species in Manhattan?

    * Which are the neighborhoods with the most trees?

    * A visualization of Manhattan's neighborhoods and tree locations.

    * What ten tree species would you recommend the city plant in the future?

    0.) DATA

    trees <- as.data.frame(read_csv('trees1.csv')) trees <- trees %>% mutate(spc_common = str_to_title(spc_common))

    str_to_title() converts to title case, where only the first letter of each word is capitalized.

    neighborhoods <- st_read("nta.geojson")

    1.) Number of trees - map + background

    1a.) Boroughs

    neighborhoodsBoroughs <- neighborhoods %>% group_by(boroname) %>% summarise(abc = sum(as.numeric(shape_leng)))

    neighborhoodsManhattan <- neighborhoods %>% filter(boroname == 'Manhattan')

    centroids = st_centroid(neighborhoodsBoroughs) #st_centroid() is used to extract the centroid coordinates of each polygon feature and #returns a point geometry. Note that the centroid is not always inside of the polygon that #it is the center of (for example, a “C-shaped” island or a doughnut). st_point_on_surface() #provides an alternative in which a point within the polygon will be returned.

    centroids <- centroids %>% mutate(geometry1 = gsub('[()°c\]', '', geometry)) %>% separate(col = geometry1, into = c('Latitude', 'Longitude'), sep = '\, ') %>% mutate(Latitude = as.numeric(Latitude), Longitude = as.numeric(Longitude)) %>% as.data.frame()

    transformsa point geometry into 2 separate columns latitude and longitude

    plotBoroughs <- ggplot() + geom_sf(data = neighborhoodsBoroughs, #aes(fill = as.numeric(abc)), -> fills polygons with colors depending on abc #lwd = 0, colour = "white" ) + geom_sf(data = subset(neighborhoodsBoroughs, boroname == 'Manhattan'), fill = brewer.pal(n = 9, name = 'Greens')[5]) + geom_sf(data = subset(neighborhoodsBoroughs, boroname != 'Manhattan'), fill = "#FCB97D") + theme_void() + theme(legend.position = "none") + #scale_fill_gradientn(

    colors = c("lightgray")) +

    #geom_point(data = centroids, aes(x = Latitude, y = Longitude)) + geom_text(label = centroidsLatitude, y = centroids$Longitude), size = 3) + geom_rect(mapping = aes( xmin = st_bbox(neighborhoodsManhattan)[1] - 0.01, ymin = st_bbox(neighborhoodsManhattan)[2] - 0.01, xmax = st_bbox(neighborhoodsManhattan)[3] + 0.01, ymax = st_bbox(neighborhoodsManhattan)[4] + 0.01), fill = NA, colour = "black", size = 0.9 )

    plotBoroughs

    1b.) Manhattan

    neighborhoodsManhattan <- neighborhoods %>% filter(boroname == 'Manhattan')

    neighborhoodsManhattanExclParks <- neighborhoodsManhattan %>% filter(ntacode != 'MN99')

    centroidsManhattan = st_centroid(neighborhoodsManhattanExclParks)

    centroidsManhattan <- centroidsManhattan %>% mutate(geometry1 = gsub('[()°c\]', '', geometry)) %>% separate(col = geometry1, into = c('Latitude', 'Longitude'), sep = '\, ') %>% mutate(Latitude = as.numeric(Latitude), Longitude = as.numeric(Longitude)) %>% as.data.frame()

    centroidsManhattanntaname) centroidsManhattanname)

    plotManhattan <- ggplot() + geom_sf(data = neighborhoodsManhattanExclParks, aes(fill = as.numeric(shape_area)), colour = "white" ) + theme_void() + theme(legend.position = "none") + scale_fill_gradientn( colors = c("#FF5E5B")) + #geom_point(data = centroidsManhattan, aes(x = Latitude, y = Longitude)) + geom_text(label = centroidsManhattanLatitude, y = centroidsManhattan$Longitude), size = 1.8)

    #plotManhattan

    1c.) Manhattan trees density

    #neighborhoodsManhattan <- neighborhoods %>%

    filter(boroname == 'Manhattan')

    #neighborhoodsManhattanExclParks <- neighborhoodsManhattan %>%

    filter(ntacode != 'MN99')

    centroidsManhattan = st_centroid(neighborhoodsManhattanExclParks)

    #centroidsManhattan <- centroidsManhattan %>%

    mutate(geometry1 = gsub('[()°c\]', '', geometry)) %>%

    separate(col = geometry1, into = c('Latitude', 'Longitude'), sep = '\, ') %>%

    mutate(Latitude = as.numeric(Latitude), Longitude = as.numeric(Longitude)) %>%

    as.data.frame()

    #centroidsManhattanntaname) #centroidsManhattanname)

    treesSP <- st_as_sf(trees, coords = c('longitude','latitude'), crs = st_crs(neighborhoodsManhattan))

    st_as_sf - convert foreign object to an sf object reference system specified by st_crs(...)

    #treesSPTest <- head(treesSP, n = 100, crs = st_crs(neighborhoodsManhattan))

    st_crs(neighborhoodsManhattan) == st_crs(treesSP)

    neighborhoodsManhattanExclParks$area_sqkm <- st_area(neighborhoodsManhattanExclParks)

    neighborhoodsManhattanAreas <- neighborhoodsManhattanExclParks%>% select(ntacode, area_sqkm) %>% st_drop_geometry() %>% mutate(area_sqkm = as.numeric(area_sqkm)/10^6)

    manhattanTrees <- trees %>% select(nta,nta_name) %>% group_by(nta, nta_name) %>% summarise(n = n()) %>% ungroup() %>% left_join(neighborhoodsManhattanAreas, by = c('nta' = 'ntacode')) %>% mutate(treesPerAreaUnit = n / area_sqkm) %>% as.data.frame()

    customGreen = brewer.pal(n = 9 , name = "Greens")[1] customGreen0 = brewer.pal(n = 9 , name = "Greens")[8] customBlue = brewer.pal(n = 9 , name = "Blues")[1] customBlue0 = brewer.pal(n = 9 , name = "Blues")[6]

    neighborhoodsManhattanExclParksTrees <- neighborhoodsManhattanExclParks %>% left_join(manhattanTrees, by = c('ntacode'='nta'))

    neighborhoodsManhattanExclParksTreesPerKmPlot <- ggplot() + geom_sf(data = neighborhoodsManhattanExclParksTrees, aes(fill = n), colour = "darkgrey" ) + theme_void() + theme(legend.position = "none") + theme(#legend.position="left", legend.key.size = unit(0.5, 'cm'), legend.position = c(-0.05, 0.3), legend.title = element_text(size=7), legend.text = element_text(size=7)) + guides(fill=guide_legend(title="number of trees")) + scale_fill_gradientn(colors = brewer.pal(n = 9 , name = "Greens")[1:8]) + geom_text(label = centroidsManhattanLatitude, y = centroidsManhattan$Longitude), size = 1.8)

    neighborhoodsManhattanExclParksTreesPerKmPlot

    1d.) GRID

    img1 <- readJPEG("pic2.JPG") img1 = abind::abind(img1, img1[,,1]) img1[,,4] = 0.15

    h<-dim(img1)[1] # image height w<-dim(img1)[2] # image width

    plotBackground <- plot(ggplot() + annotation_custom(grid::rasterGrob(img1, width=unit(1,"npc"), height=unit(1,"npc")), 0, w, 0, -h) + # The minus is needed to get the y scale reversed scale_x_continuous(expand=c(0,0),limits=c(0,w)) + scale_y_reverse(expand=c(0,0),limits=c(h,0)) + # The y scale is reversed because in image the vertical positive direction is typically downward coord_equal() + # To keep the aspect ratio of the image. stat_bin2d(binwidth=2,aes(fill = ..density..))+ theme_void() )

    plotBackgroundWithMap <- ggdraw() + draw_plot(plotBackground) + draw_plot(neighborhoodsManhattanExclParksTreesPerKmPlot, hjust =0.075, width = 1, height = 1) + #draw_plot(plotManhattan, hjust =0.1, width = 1, height = 1) + draw_plot(plotBoroughs, hjust = -0.7, vjust = -0.2, width = 0.5, height = 0.5)

    plotBackgroundWithMap

    ##################################################

    2) Trees per km2

    2a.) Trees per km2 - table

    improvement_formatter <- formatter("span", style = x ~ style(font.weight = "bold", display = 'block', width = '100px', color = ifelse(x > 0, 'Green', ifelse(x < 0, 'Red',"black"))), x ~ icontext(ifelse(x>0, "arrow-up", "arrow-down"), x))

    customGreen = brewer.pal(n = 9 , name = "Greens")[1] customGreen0 = brewer.pal(n = 9 , name = "Greens")[8] customBlue = brewer.pal(n = 9 , name = "Blues")[1] customBlue0 = brewer.pal(n = 9 , name = "Blues")[6]

    costum_format1 <- formatter(.tag = "span", style = function(x) style( display = "block", padding = "0 4px", width = '150px', 'border-radius' = "4px", 'background-color' = csscolor(gradient(as.numeric(x),customGreen, customGreen0)), color = ifelse(x >= 3, "black","black")))

    costum_format2 <- formatter(.tag = "span", style = function(x) style( display = "block", width = "50px"))

    costum_format3 <- formatter(.tag = "span", style = function(x) style( display = "block", padding = "0 4px", width = '50px', 'border-radius' = "4px"))

    manhattanTreesTable <- manhattanTrees %>% mutate(treesPerKm2 = round(treesPerAreaUnit)) %>% select(nta, nta_name, treesPerKm2) %>% arrange(desc(treesPerKm2))

    manhattanTreesFormattable = formattable(manhattanTreesTable, align =c("l","l","l"), list('nta' = costum_format3, 'ntaname' = costum_format2, treesPerKm2 = costum_format1 ))

    export_formattable <- function(f, file, width = "60%", height = NULL, background = "white", delay = 0.2) { w <- as.htmlwidget(f, width = width, height = height) path <- htmltools::html_print(w, background = background, viewer = NULL) url <- paste0("file:///", gsub("\\", "/", normalizePath(path))) webshot(url, file = file, selector = ".formattable_widget", delay = delay) }

    export_formattable(manhattanTreesFormattable, 'manhattanTreesFormattable.jpg')

    img2 <- readJPEG('manhattanTreesFormattable.jpg') img2 = abind::abind(img2, img2[,,1]) img2[,,4] = 1

    h <-dim(img2)[1] # image height w <-dim(img2)[2] # image width

    manhattanTreesFormattablePlot <- ggplot() + annotation_custom(grid::rasterGrob(img2, width=unit(1,"npc"), height=unit(1,"npc")), 0, w, 0, -h) + scale_x_continuous(expand=c(0,0),limits=c(0,w)) + scale_y_reverse(expand=c(0,0),limits=c(h,0)) + coord_equal() + stat_bin2d(binwidth=2,aes(fill = ..density..)) + theme_void()

    #manhattanTreesFormattablePlot

    2b.) Trees per km2 - plot

    neighborhoodsManhattan <- neighborhoods %>% filter(boroname == 'Manhattan')

    neighborhoodsManhattanExclParks <- neighborhoodsManhattan %>% filter(ntacode != 'MN99')

    centroidsManhattan = st_centroid(neighborhoodsManhattanExclParks)

    centroidsManhattan <- centroidsManhattan %>% mutate(geometry1 = gsub('[()°c\]', '', geometry)) %>% separate(col = geometry1, into = c('Latitude', 'Longitude'), sep = '\, ') %>% mutate(Latitude = as.numeric(Latitude), Longitude = as.numeric(Longitude)) %>% as.data.frame()

    centroidsManhattanntaname) centroidsManhattanname)

    treesSP <- st_as_sf(trees, coords = c('longitude','latitude'), crs = st_crs(neighborhoodsManhattan)) treesSPTest <- head(treesSP, n = 100, crs = st_crs(neighborhoodsManhattan))

    st_crs(neighborhoodsManhattan) == st_crs(treesSP)

    neighborhoodsManhattanExclParks$area_sqkm <- st_area(neighborhoodsManhattanExclParks)

    neighborhoodsManhattanAreas <- neighborhoodsManhattanExclParks%>% select(ntacode, area_sqkm) %>% st_drop_geometry() %>% mutate(area_sqkm = as.numeric(area_sqkm)/10^6)

    manhattanTrees <- trees %>% select(nta,nta_name) %>% group_by(nta, nta_name) %>% summarise(n = n()) %>% ungroup() %>% left_join(neighborhoodsManhattanAreas, by = c('nta' = 'ntacode')) %>% mutate(treesPerAreaUnit = n / area_sqkm) %>% as.data.frame()

    customGreen = brewer.pal(n = 9 , name = "Greens")[1] customGreen0 = brewer.pal(n = 9 , name = "Greens")[8] customBlue = brewer.pal(n = 9 , name = "Blues")[1] customBlue0 = brewer.pal(n = 9 , name = "Blues")[6]

    neighborhoodsManhattanExclParksTrees <- neighborhoodsManhattanExclParks %>% left_join(manhattanTrees, by = c('ntacode'='nta'))

    neighborhoodsManhattanExclParksTreesPerKmPlot <- ggplot() + geom_sf(data = neighborhoodsManhattanExclParksTrees, aes(fill = treesPerAreaUnit), colour = "darkgrey" ) + theme_void() + theme(legend.position = "none") + theme(#legend.position="left", legend.key.size = unit(0.5, 'cm'), legend.position = c(-0.05, 0.3), legend.title = element_text(size=7), legend.text = element_text(size=7)) + guides(fill=guide_legend(title="trees per km2")) + scale_fill_gradientn(colors = brewer.pal(n = 9 , name = "Greens")[1:8]) + geom_text(label = centroidsManhattanLatitude, y = centroidsManhattan$Longitude), size = 1.8)

    2c.) Trees per km2 - table + plot

    grid.arrange(neighborhoodsManhattanExclParksTreesPerKmPlot, manhattanTreesFormattablePlot, ncol = 2)

    ##################################################

    3.) Most common trees

    3a.) Most common trees - chart

    treesTop20 <- trees %>% group_by(spc_common) %>% summarise(n = n()) %>% arrange(desc(n)) %>% mutate(nPerc = round(n/sum(n)*100,2), cumSum = cumsum(n), cumSumPerc = round(cumSum/sum(n)*100,2)) %>% ungroup() %>% head(n = 21) %>% drop_na() %>% as.data.frame()

    treesTop20 <- treesTop20 %>% ggplot() + geom_col(aes(x = reorder(spc_common,-nPerc), y = nPerc), fill="#69b3a2", alpha= .4) + geom_point(aes(x = reorder(spc_common,-nPerc), y = cumSumPerc/3.03), size = 2, color = rgb(0.2, 0.6, 0.9, 1)) + scale_y_continuous(limits = c(0,30), sec.axis = sec_axis(~.*3.3, name="cum % of total trees")) + theme_ipsum() + ylab('% of total trees') + ggtitle('Fig_2: Most common Manhattan trees species') + theme( plot.title=element_text(size = 10, face = 'plain', hjust=0.5, color = '#576161'), axis.text.x = element_text(color = '#576161', angle = 90, vjust = 0.5, hjust=1, size = 10), #axis.title.x = element_text(color = "#576161"), axis.title.x = element_blank(), axis.title.y = element_text(color = "#69b3a2", size=12), axis.text.y = element_text(color = "#69b3a2"), panel.grid.major = element_line(colour = "aliceblue"), panel.grid.minor = element_line(colour = "aliceblue"), axis.title.y.right = element_text(color = rgb(0.2, 0.6, 0.9, 1), size=12), axis.text.y.right = element_text(color = rgb(0.2, 0.6, 0.9, 1)) ) + annotate("curve", x = 15, y = 12.6, xend = 8, yend = 24, arrow = arrow(length = unit(0.2, "cm"), type = "closed"), color = rgb(0.2, 0.6, 0.9, 1)) + annotate("text", x = 15, y = 12.6, label = "10 most common trees make \n up 80% of all trees ", vjust = 1, size = 3, color = rgb(0.2, 0.6, 0.9, 1)) + annotate("curve", x = 3, y = 24, xend = 1, yend = 19, arrow = arrow(length = unit(0.2, "cm"), type = "closed"), color = '#69b3a2') + annotate("text", x = 4, y = 28, label = "Honeylocust being the most common tree \n makes up 21% of all trees", vjust = 1, size = 3, color = '#69b3a2')

    #treesTop20

    3b.) Most common trees - distribution

    treesTop10SpeciesVector <- trees %>% group_by(spc_common) %>% summarise(n = n()) %>% arrange(desc(n)) %>% mutate(nPerc = round(n/sum(n)*100,2), cumSum = cumsum(n), cumSumPerc = round(cumSum/sum(n)*100,2)) %>% ungroup() %>% head(n = 11) %>% drop_na() %>% as.data.frame()

    treesTop10Species <- trees %>% filter(spc_common %in% c(treesTop10SpeciesVector$spc_common)) %>% left_join(neighborhoods, by = c('nta' = 'ntacode')) %>% left_join(treesTop10SpeciesVector, by = c('spc_common' = 'spc_common')) %>% select(spc_common, ntaname, boroname, n) %>% filter(boroname == 'Manhattan') %>% #head() %>% ggplot(aes(x = reorder(spc_common,n), y = ntaname)) + geom_jitter(alpha=0.1, size = 0.5, width = 0.10, height = 0.20) + theme_minimal() + theme(axis.text.x = element_text(angle = 90, vjust = 1.1, hjust=1, size = 8), axis.text.y = element_text(size = 8) )

    mostCommonTreesDist <- trees %>% filter(spc_common %in% c(treesTop10SpeciesVector$spc_common)) %>% left_join(neighborhoods, by = c('nta' = 'ntacode')) %>% select(spc_common, ntaname, boroname) %>% group_by(spc_common, ntaname) %>% summarize(n = n()) %>% mutate(pct = round(n / sum(n),2) * 100) %>% ungroup %>% left_join(treesTop10SpeciesVector, by = c('spc_common' = 'spc_common')) %>% ggplot(aes(reorder(x = spc_common,-nPerc), y = ntaname, size = pct, group = spc_common, color = spc_common, label = paste0(pct,'%') )) + #scale_size(range = c(1, 10)) + geom_point(position = position_dodge(width = 0.5), alpha = 0.7 ) + geom_text(size = 3, position = position_dodge(width = 0.5), show.legend = FALSE, size = 4, hjust = -1) + theme_minimal() + theme(axis.text.x = element_text(angle = 90, vjust = 1.1, hjust=1, size = 8), axis.title = element_blank(), axis.text.y = element_text(size = 8)) + guides(size = FALSE) + theme(legend.position = "none") + scale_colour_manual(values=c("Honeylocust" = "brown", "Callery Pear" = "darkgreen", "Ginkgo" = "brown",
    "Pin Oak" = "darkgreen",
    "Sophora" = "brown", "London Planetree" = "darkgreen", "Japanese Zelkova" = "brown", "Littleleaf Linden" = "darkgreen", "American Elm" = "brown", "American Linden" = "darkgreen"))

    mostCommonTreesDist

    ##################################################

    4.) Curb location - ON / OFF CURB

    mycols <- c("#E0E0E0", "#E71313")

    treesCurb <- trees %>% select(curb_loc) %>% group_by(curb_loc) %>% summarize(n=n()) %>% mutate(nPerc = round(n/sum(n),3)) %>% mutate(lab.ypos = case_when(curb_loc == 'OffsetFromCurb' ~ cumsum(nPerc) +13.5nPerc, curb_loc == 'OnCurb' ~ cumsum(nPerc) - 0.95nPerc)) %>% ungroup %>% ggplot(aes(x = 3, y = nPerc, fill = curb_loc)) + geom_bar(width = 1, stat = "identity", color = "white") + coord_polar(thet = "y", start = 0.1) + geom_text(aes(y = lab.ypos, label = nPerc*100), color = "black", size = 3)+ scale_fill_manual(values = mycols) + theme_void() + xlim(0.4, 4.5) + ggtitle('Fig_3: Trees location in relation to the curb [%]') + theme(legend.position = "none") + theme(plot.title=element_text(size = 10, face = 'plain', hjust=0.5, color = '#576161'), legend.key.size = unit(0.5, 'cm'), legend.position = c(0.51, 0.5), legend.title = element_blank(), legend.text = element_text(size=8))

    #treesCurb

    treesCurbMapData <- trees %>% select(nta, curb_loc) %>% group_by(nta, curb_loc) %>% summarize(n = n()) %>% mutate(pct = round(n / sum(n),2)*100) %>% ungroup() %>% filter(curb_loc == 'OnCurb')

    neighborhoodsManhattan <- neighborhoods %>% filter(boroname == 'Manhattan')

    neighborhoodsManhattanExclParks <- neighborhoodsManhattan %>% filter(ntacode != 'MN99')

    neighborhoodsManhattanExclParks <- neighborhoodsManhattanExclParks %>% left_join(treesCurbMapData, by = c('ntacode' = 'nta'))

    centroids = st_centroid(neighborhoodsManhattanExclParks)

    centroids <- centroids %>% mutate(geometry1 = gsub('[()°c\]', '', geometry)) %>% separate(col = geometry1, into = c('Latitude', 'Longitude'), sep = '\, ') %>% mutate(Latitude = as.numeric(Latitude), Longitude = as.numeric(Longitude)) %>% as.data.frame()

    onCurbTreesMapPlot <- ggplot() + geom_sf(data = neighborhoodsManhattanExclParks, aes(fill = pct), colour = "grey" ) + theme_void() + theme(legend.position = "none") + ggtitle('Fig_4: OnCurb trees as % of total trees') + theme(plot.title=element_text(size = 10, face = 'plain', hjust=0.5, color = '#576161')) + scale_fill_gradientn(colors = brewer.pal(n = 9, name = 'YlOrRd')[1:9]) + geom_text(label = neighborhoodsManhattanExclParksLatitude, y = centroids$Longitude), size = 3, color = 'lightgrey')

    #onCurbTreesMapPlot

    grid.arrange(treesCurb, onCurbTreesMapPlot, ncol = 2)

    ##################################################

    5.) Health - All trees

    treesTop20 <- trees %>% group_by(spc_common) %>% summarise(n = n()) %>% arrange(desc(n)) %>% mutate(nPerc = round(n/sum(n)*100,2), cumSum = cumsum(n), cumSumPerc = round(cumSum/sum(n)*100,2)) %>% ungroup() %>% head(n = 21) %>% drop_na() %>% as.data.frame()

    treesAll <- trees %>% mutate(spc_common = 'All trees')

    treesHealthData <- rbind(trees, treesAll)

    treesHealtPlot <- treesHealthData %>% filter(spc_common %in% c(treesTop20$spc_common, 'All trees')) %>% filter(status =='Alive') %>% group_by(spc_common, curb_loc, health) %>% summarize(n = n()) %>% mutate(pct = round(n / sum(n),2)*100) %>% mutate(health = factor(health,levels = c("Good","Fair","Poor"))) %>% ggplot(aes(y = spc_common, x = curb_loc, size = pct, group = health,
    label = pct )) + scale_size(range = c(2, 5)) + geom_point(aes(color = health), position = position_dodge(width = 0.5), alpha = 0.5) + geom_text(size = 3, position = position_dodge(width = 0.5),show.legend = FALSE, size = 4, hjust = -0.8) + theme_minimal() + theme(axis.title = element_blank()) + guides(size = FALSE) + scale_color_manual(values = c("Good" = "green", "Fair" = "orange", 'Poor' = 'red'))

    treesHealtPlot

    ##################################################

    6.) Health - Map of ntas

    treesHealth <- trees %>% filter(status =='Alive') %>% select(nta,health) %>% group_by(nta,health) %>% summarize(n = n()) %>% mutate(pct = round(n / sum(n),2)*100) %>% ungroup() %>% spread(key = health, value = pct) %>% select(-n) %>% group_by(nta) %>% replace(is.na(.), 0) %>% summarise(Fair = sum(Fair), Good = sum(Good), Poor = sum(Poor))

    neighborhoodsManhattan <- neighborhoods %>% filter(boroname == 'Manhattan')

    neighborhoodsManhattanExclParks <- neighborhoodsManhattan %>% filter(ntacode != 'MN99')

    neighborhoodsManhattanExclParks <- neighborhoodsManhattanExclParks %>% left_join(treesHealth, by = c('ntacode' = 'nta'))

    centroids = st_centroid(neighborhoodsManhattanExclParks)

    centroids <- centroids %>% mutate(geometry1 = gsub('[()°c\]', '', geometry)) %>% separate(col = geometry1, into = c('Latitude', 'Longitude'), sep = '\, ') %>% mutate(Latitude = as.numeric(Latitude), Longitude = as.numeric(Longitude)) %>% as.data.frame()

    plotManhattanFun <- function(health, colors, labelsTest, plotTitle) { ggplot() + geom_sf(data = neighborhoodsManhattanExclParks, aes_string(fill = health), colour = "grey" ) + ggtitle(plotTitle) + theme_void() + theme(plot.title=element_text(size = 12, face = 'plain', hjust=0.5, color = '#576161'), legend.position = "none") + # theme(legend.position="bottom") + scale_fill_gradientn(colors = brewer.pal(n = 9, name = colors)[2:9]) + geom_text(label = labelsTest, aes(x = centroidsLongitude), size = 3) }

    neighborhoodsManhattanDict <- ggplot() + geom_sf(data = neighborhoodsManhattanExclParksTrees, colour = "darkgrey" ) + theme_void() + ggtitle("NTA names") + theme(plot.title=element_text(size = 12, face = 'plain', hjust=0.5, color = '#576161'), legend.position = "none") + guides(fill=guide_legend(title="number of trees")) + geom_text(label = centroidsManhattanLatitude, y = centroidsManhattan$Longitude), size = 1.8)

    grid.arrange(neighborhoodsManhattanDict, plotManhattanFun("Good", "Greens", neighborhoodsManhattanExclParksFair, '% of trees with fair health'), plotManhattanFun("Poor", "Reds", neighborhoodsManhattanExclParks$Poor, '% of trees with poor health'), ncol = 4)

    ##################################################

    7.) Trunk

    treesTop10 <- trees %>% group_by(spc_common) %>% summarise(n = n()) %>% arrange(desc(n)) %>% mutate(nPerc = round(n/sum(n)*100,2), cumSum = cumsum(n), cumSumPerc = round(cumSum/sum(n)*100,2)) %>% ungroup() %>% head(n = 11) %>% drop_na() %>% as.data.frame()

    outliers

    trees %>% filter(spc_common %in% (treesTop10$spc_common)) %>% select(spc_common, tree_dbh) %>% group_by(spc_common) %>% summarise(IQRRange = IQR(tree_dbh), median = median(tree_dbh), Q3 = quantile(tree_dbh, probs=c(.25, .75))[2], outlier = 1.5 * IQRRange + Q3)

    #boxplot treesTop10BoxPlot <- trees %>% filter(spc_common %in% (treesTop10$spc_common)) %>% select(spc_common, tree_dbh) %>% ggplot(aes(x = tree_dbh, y = reorder(spc_common,tree_dbh,median))) + geom_boxplot(color="black", fill='#4DAF4A', alpha= 0.5, outlier.size = 2, outlier.color = 'grey') + scale_x_continuous(limits=c(0,50)) + theme_minimal() + xlab("medianDiameter") + theme(axis.title.y = element_blank())

    #barChart treesTop10Median <- trees %>% filter(spc_common %in% (treesTop10$spc_common)) %>% group_by(spc_common, curb_loc) %>% summarize(medianDiameter = median(tree_dbh)) %>% ggplot(aes(y = reorder(spc_common,medianDiameter,median), x = medianDiameter, fill = curb_loc, label = paste0(medianDiameter))) + geom_col(width = 0.5, position = position_dodge(0.7), alpha = 0.7) + geom_text(width = 0.5, position = position_dodge(0.7), hjust = -0.2, vjust = 0.4, size = 3) + theme_minimal() + scale_x_continuous(limits = c(0,20)) + scale_fill_brewer(palette="Set1") + theme(axis.title.y = element_blank())

    grid.arrange(treesTop10BoxPlot, treesTop10Median, ncol = 2)

    ##################################################

    8.) Ratings - table

    TOP TEN BEST TREES

    trees <- as.data.frame(read_csv('trees1.csv')) trees <- trees %>% mutate(spc_common = str_to_title(spc_common))

    treesTop20Names <- trees %>% group_by(spc_common) %>% summarise(n = n()) %>% arrange(desc(n)) %>% mutate(nPerc = round(n/sum(n)*100,2), cumSum = cumsum(n), cumSumPerc = round(cumSum/sum(n)*100,2)) %>% ungroup() %>% head(n = 21) %>% drop_na() %>% select(spc_common)

    treesTop10Health <- trees %>% filter(spc_common %in% c(treesTop20Names$spc_common)) %>% select(spc_common,health) %>% group_by(spc_common,health) %>% summarize(n = n()) %>% mutate(pct = round(n / sum(n),2)*100) %>% filter(health == 'Good') %>% arrange((pct)) %>% ungroup() %>% mutate(pointsHealth = row_number())

    treesTop10Trunk <- trees %>% filter(spc_common %in% c(treesTop20Names$spc_common)) %>% select(spc_common,tree_dbh) %>% group_by(spc_common) %>% summarize(medianDiameter = median((tree_dbh))) %>% arrange((medianDiameter)) %>% mutate(pointsDiameter = row_number())

    treesTop10Ranking <- treesTop10Health %>% left_join(treesTop10Trunk, by = 'spc_common') %>% select(spc_common, pointsHealth, pointsDiameter) %>% mutate(pointsTotal = pointsHealth + pointsDiameter) %>% arrange(desc(pointsTotal)) %>% mutate(positionTotal = row_number()) %>% mutate(price = case_when(positionTotal == 1 ~'1st', positionTotal == 2 ~'2nd', positionTotal == 3 ~'3rd', positionTotal == 4 ~'4th', positionTotal == 5 ~'5th', positionTotal == 6 ~'6th', positionTotal == 7 ~'7th', positionTotal == 8 ~'8th', positionTotal == 9 ~'9th', positionTotal == 10 ~'10th' )) %>% mutate(position = price) %>% head(n = 10) %>% select(spc_common, price,position, pointsTotal, pointsHealth, pointsDiameter)

    #install.packages('base64enc') library(base64enc)

    gold <- sprintf("data:image/png;base64,%s", base64encode("gold_medal_1.png")) silver <- sprintf("data:image/png;base64,%s", base64encode("silver_medal_1.png")) bronze <- sprintf("data:image/png;base64,%s", base64encode("bronze_medal_1.png")) flowers <- sprintf("data:image/png;base64,%s", base64encode("flowers_medal_1.png"))

    image_tile <- formatter("img", src = position ~ ifelse(position == "1st", gold, ifelse(position == "2nd", silver, ifelse(position == "3rd", bronze, flowers))), # Control height and width, either directly - width = 25, # Or via a formula height = 20, NA)

    treesTop10RankingFormattable <- formattable( treesTop10Ranking, align =c("l","c"), list(price = image_tile ))

    export_formattable <- function(f, file, width = "60%", height = NULL, background = "white", delay = 0.2) { w <- as.htmlwidget(f, width = width, height = height) path <- htmltools::html_print(w, background = background, viewer = NULL) url <- paste0("file:///", gsub("\\", "/", normalizePath(path))) webshot(url, file = file, selector = ".formattable_widget", delay = delay) }

    export_formattable(treesTop10RankingFormattable, 'treesTop10RankingFormattable.jpg')

    img2 <- readJPEG('treesTop10RankingFormattable.jpg') img2 = abind::abind(img2, img2[,,1]) img2[,,4] = 1

    h <-dim(img2)[1] # image height w <-dim(img2)[2] # image width

    treesTop10RankingFormattablePlot <- ggplot() + annotation_custom(grid::rasterGrob(img2, width=unit(1,"npc"), height=unit(1,"npc")), 0, w, 0, -h) + scale_x_continuous(expand=c(0,0),limits=c(0,w)) + scale_y_reverse(expand=c(0,0),limits=c(h,0)) + coord_equal() + stat_bin2d(binwidth=2,aes(fill = ..density..)) + theme_void()

    ##################################################

    9.) Appendix -dead trees

    trees <- as.data.frame(read_csv('trees1.csv')) trees <- trees %>% mutate(spc_common = str_to_title(spc_common))

    neighborhoods <- st_read("nta.geojson")

    deadTreesPlotFunction <- function(curb, title) { treesDead <- trees %>% filter(curb_loc %in% curb) %>% select(nta, status) %>% group_by(nta, status) %>% summarize(n = n()) %>% mutate(pct = round(n / sum(n),3)*100) %>% ungroup() %>% complete(nta, status, fill = list(n = 0)) %>% replace(is.na(.), 0) %>% filter(status == 'Dead')

    neighborhoodsManhattan <- neighborhoods %>% filter(boroname == 'Manhattan')

    neighborhoodsManhattanExclParks <- neighborhoodsManhattan %>% filter(ntacode != 'MN99')

    neighborhoodsManhattanExclParks <- neighborhoodsManhattanExclParks %>% left_join(treesDead, by = c('ntacode' = 'nta'))

    centroids = st_centroid(neighborhoodsManhattanExclParks)

    centroids <- centroids %>% mutate(geometry1 = gsub('[()°c\]', '', geometry)) %>% separate(col = geometry1, into = c('Latitude', 'Longitude'), sep = '\, ') %>% mutate(Latitude = as.numeric(Latitude), Longitude = as.numeric(Longitude)) %>% as.data.frame()

    ggplot() + geom_sf(data = neighborhoodsManhattanExclParks, aes(fill = pct), colour = "grey" ) + theme_void() + ggtitle(title) + theme(legend.position = 'none', plot.title=element_text(size = 10, face = 'plain', hjust=0.5, color = '#576161')) + scale_fill_gradientn(colors = brewer.pal(n = 9, name = 'Reds')[2:8]) + geom_text(label = neighborhoodsManhattanExclParksLatitude, y = centroids$Longitude), size = 3) }

    #options(repr.plot.width=10, repr.plot.height=7)

    plot1 <- deadTreesPlotFunction(c('OffsetFromCurb','OnCurb'), 'All trees') plot2 <- deadTreesPlotFunction(c('OnCurb'), 'OnCurb') plot3 <- deadTreesPlotFunction(c('OffsetFromCurb'), 'OffsetFromCurb')

    grid.arrange(plot1, plot2, plot3, ncol = 3)

    grid.arrange(arrangeGrob(plot1, plot2, plot3, ncol = 3), heights=c(20, 1),top = textGrob("Fig_6: Percentage of dead trees",gp=gpar(col = '#576161',fontsize=10,font=3)))

    ##################################################

    head(n = 10) %>% select(spc_common, pointsHealth, pointsDiameter, positionTotal) %>% gather(key = "category", value = "points", 2:4) %>% mutate(positionInRanking = case_when(category == 'positionTotal' ~ points*1, category == 'pointsHealth' | category == 'pointsDiameter' ~ 21 - points )) %>% view()

    treesTop10RankingSummary <- treesTop10Ranking %>% filter(category == 'positionTotal') %>% select(spc_common,points) %>% mutate(points = points*1) %>% arrange(desc(points))

    treesTop10Ranking %>% ggplot(aes(y = factor(spc_common,levels = c(treesTop10RankingSummary$spc_common)), x = factor(category, levels = c('positionTotal', 'pointsDiameter', 'pointsHealth')), size = points, group = category, label = positionInRanking)) + scale_size(range = c(2, 5)) + geom_point(aes(color = category), position = position_dodge(width = 0.5), alpha = 0.5) + geom_text(size = 3, position = position_dodge(width = 0.5),show.legend = FALSE, size = 4, hjust = -0.8) + theme_minimal() + theme(axis.title = element_blank()) + guides(size = FALSE) + scale_color_manual(values = c("pointsHealth" = "green", "pointsDiameter" = "orange", 'positionTotal' = 'red'))

    treesTop10RankingMapData <- trees %>% left_join(treesTop10Ranking, by = 'spc_common') %>% replace(is.na(.), 'oth') %>% select(nta, nta_name, topTen) %>% group_by(nta, nta_name, topTen) %>% summarise(n = n()) %>% mutate(pct = round(n / sum(n),2)*100) %>% filter(topTen == 'topTen') %>% arrange(desc(pct))

    neighborhoodsManhattan <- neighborhoods %>% filter(boroname == 'Manhattan')

    neighborhoodsManhattanExclParks <- neighborhoodsManhattan %>% filter(ntacode != 'MN99')

    centroidsManhattan = st_centroid(neighborhoodsManhattanExclParks)

    centroidsManhattan <- centroidsManhattan %>% mutate(geometry1 = gsub('[()°c\]', '', geometry)) %>% separate(col = geometry1, into = c('Latitude', 'Longitude'), sep = '\, ') %>% mutate(Latitude = as.numeric(Latitude), Longitude = as.numeric(Longitude)) %>% as.data.frame()

    neighborhoodsManhattanExclParksTopTenRanking <- neighborhoodsManhattanExclParks %>% left_join(treesTop10RankingMapData, by = c('ntacode' = 'nta'))

    neighborhoodsManhattanExclParksTopTenRankingPlot <- neighborhoodsManhattanExclParksTopTenRanking %>% ggplot() + geom_sf(data = neighborhoodsManhattanExclParksTopTenRanking, aes(fill = pct), colour = "grey") + theme_void() + theme(legend.position = "none") + theme(legend.position="bottom") + theme(legend.position = "none") + theme(#legend.position="left", legend.key.size = unit(0.5, 'cm'), legend.position = c(-0.05, 0.3), legend.title = element_text(size=7), legend.text = element_text(size=7)) + guides(fill=guide_legend(title="top 10 trees [%]")) + scale_fill_gradientn(colors = brewer.pal(n = 9, name = 'Greens')[1:8]) + geom_text(label = centroidsManhattanLatitude, y = centroidsManhattan$Longitude), size = 2)

    improvement_formatter <- formatter("span", style = x ~ style(font.weight = "bold", display = 'block', width = '100px', color = ifelse(x > 0, 'Green', ifelse(x < 0, 'Red',"black"))), x ~ icontext(ifelse(x>0, "arrow-up", "arrow-down"), x))

    customGreen = brewer.pal(n = 9 , name = "Greens")[1] customGreen0 = brewer.pal(n = 9 , name = "Greens")[8] customBlue = brewer.pal(n = 9 , name = "Blues")[1] customBlue0 = brewer.pal(n = 9 , name = "Blues")[6]

    costum_format1 <- formatter(.tag = "span", style = function(x) style( display = "block", padding = "0 4px", width = '150px', 'border-radius' = "4px", 'background-color' = csscolor(gradient(as.numeric(x),customGreen, customGreen0)), color = ifelse(x >= 3, "black","black")))

    costum_format2 <- formatter(.tag = "span", style = function(x) style( display = "block", width = "50px"))

    costum_format3 <- formatter(.tag = "span", style = function(x) style( display = "block", padding = "0 4px", width = '50px', 'border-radius' = "4px"))

    treesTop10RankingMapTable <- treesTop10RankingMapData %>% select(nta, nta_name, pct)

    treesTop10RankingMapTable

    testFormattable = formattable(treesTop10RankingMapTable, align =c("l","l","l"), list('nta' = costum_format3, 'ntaname' = costum_format2, pct = costum_format1 ))

    testFormattable %>%

    kable() %>%

    kable_styling(bootstrap_options = "bordered",

    full_width = FALSE) %>%

    add_header_above(c("", "Category1" = 2)) %>%

    collapse_rows(columns = 1,

    valign = "middle")

    webshot::install_phantomjs()

    export_formattable <- function(f, file, width = "60%", height = NULL, background = "white", delay = 0.2) { w <- as.htmlwidget(f, width = width, height = height) path <- htmltools::html_print(w, background = background, viewer = NULL) url <- paste0("file:///", gsub("\\", "/", normalizePath(path))) webshot(url, file = file, selector = ".formattable_widget", delay = delay) }

    export_formattable(testFormattable, 'testFormattable.jpg')

    img2 <- readJPEG('testFormattable.jpg') img2 = abind::abind(img2, img2[,,1]) img2[,,4] = 1

    h <-dim(img2)[1] # image height w <-dim(img2)[2] # image width

    plotTableTopTen <- ggplot() + annotation_custom(grid::rasterGrob(img2, width=unit(1,"npc"), height=unit(1,"npc")), 0, w, 0, -h) + scale_x_continuous(expand=c(0,0),limits=c(0,w)) + scale_y_reverse(expand=c(0,0),limits=c(h,0)) + coord_equal() + stat_bin2d(binwidth=2,aes(fill = ..density..)) + theme_void()

    plotTableTopTen neighborhoodsManhattanExclParksTopTenRankingPlot

    grid.arrange(neighborhoodsManhattanExclParksTopTenRankingPlot, plotTableTopTen, ncol = 2)

    DEAD TREES

    single trees

    neighborhoodsManhattan <- neighborhoods %>% filter(boroname == 'Manhattan')

    neighborhoodsManhattanExclParks <- neighborhoodsManhattan %>% filter(ntacode != 'MN99')

    treesSP <- st_as_sf(trees, coords = c('longitude','latitude'), crs = st_crs(neighborhoodsManhattan)) treesSPTest <- head(treesSP, n = 100, crs = st_crs(neighborhoodsManhattan))

    ggplot() + #geom_sf(data = subset(treesSP, status == 'Alive'), size = 0.05, alpha = 0.5, color = "green") + geom_sf(data = subset(treesSP, status == 'Dead'), size = 0.05, color = "red") + geom_sf(data = neighborhoodsManhattanExclParks, lwd = 0, alpha = 0.3) + theme_void()

    DEAD TREES

    single trees

    neighborhoodsManhattan <- neighborhoods %>% filter(boroname == 'Manhattan')

    neighborhoodsManhattanExclParks <- neighborhoodsManhattan %>% filter(ntacode != 'MN99')

    treesSP <- st_as_sf(trees, coords = c('longitude','latitude'), crs = st_crs(neighborhoodsManhattan)) treesSPTest <- head(treesSP, n = 100, crs = st_crs(neighborhoodsManhattan))

    neighborhoodsNoGeometry <- st_set_geometry(neighborhoods, NULL)

    neighborhoodsNoGeometryManhattan <- neighborhoodsNoGeometry %>% filter(boroname == 'Manhattan') %>% select(ntacode)

    mycols <- c(brewer.pal(n = 9 , name = "Greens")[4], "#E71313")

    deadTreesPlot <- trees %>% #filter(nta %in% c(neighborhoodsNoGeometryManhattan$ntacode)) %>% select(status) %>% group_by(status) %>% summarize(n=n()) %>% mutate(nPerc = round(n/sum(n),3)) %>% #mutate(lab.ypos = cumsum(nPerc) - 0.48nPerc) %>% mutate(lab.ypos = case_when(status == 'Dead' ~ cumsum(nPerc) - 35.15nPerc, status == 'Alive' ~ cumsum(nPerc) - 0.48nPerc)) %>% ungroup %>% ggplot(aes(x = 3, y = nPerc, fill = status)) + geom_bar(width = 1, stat = "identity", color = 'white') + coord_polar(thet = "y", start = 0) + geom_text(aes(y = lab.ypos, label = nPerc100), color = "black", size = 3.2)+ scale_fill_manual(values = mycols) + theme_void() + ggtitle('All trees') + theme(legend.position = "none", plot.title=element_text(size = 10, face = 'plain', hjust=0.5, color = '#576161')) + xlim(0.5, 4.5)

    deadTreesPlotOffCurb <- trees %>% filter(curb_loc == 'OffsetFromCurb') %>% select(status) %>% group_by(status) %>% summarize(n=n()) %>% mutate(nPerc = round(n/sum(n),3)) %>% #mutate(lab.ypos = cumsum(nPerc) - 0.48nPerc) %>% mutate(lab.ypos = case_when(status == 'Dead' ~ cumsum(nPerc) - 52.15nPerc, status == 'Alive' ~ cumsum(nPerc) - 0.48nPerc)) %>% ungroup %>% ggplot(aes(x = 3, y = nPerc, fill = status)) + geom_bar(width = 1, stat = "identity", color = "white") + coord_polar(thet = "y", start = 0) + geom_text(aes(y = lab.ypos, label = nPerc100), color = "black", size = 3.2)+ scale_fill_manual(values = mycols) + theme_void() + ggtitle('OffsetFromCurb') + theme(legend.position = "none", plot.title=element_text(size = 10, face = 'plain', hjust=0.5, color = '#576161')) + xlim(0.5, 4.5)

    deadTreesPlotOnCurb <- trees %>% filter(curb_loc == 'OnCurb') %>% select(status) %>% group_by(status) %>% summarize(n=n()) %>% mutate(nPerc = round(n/sum(n),3)) %>% #mutate(lab.ypos = cumsum(nPerc) - 0.48nPerc) %>% mutate(lab.ypos = case_when(status == 'Dead' ~ cumsum(nPerc) - 33.9nPerc, status == 'Alive' ~ cumsum(nPerc) - 0.48nPerc)) %>% ungroup %>% ggplot(aes(x = 3, y = nPerc, fill = status)) + geom_bar(width = 1, stat = "identity", color = "white") + coord_polar(thet = "y", start = 0) + geom_text(aes(y = lab.ypos, label = nPerc100), color = "black", size = 3.2)+ scale_fill_manual(values = mycols) + theme_void() + ggtitle('OnCurb') + theme(legend.position = "bottom", plot.title=element_text(size = 10, face = 'plain', hjust=0.5, color = '#576161')) + xlim(0.5, 4.5)

    g_legend<-function(a.gplot){ tmp <- ggplot_gtable(ggplot_build(a.gplot)) leg <- which(sapply(tmpname) == "guide-box") legend <- tmp$grobs[[leg]] return(legend)}

    mylegend<-g_legend(deadTreesPlotOnCurb)

    grid.arrange(arrangeGrob(deadTreesPlot, deadTreesPlotOnCurb + theme(legend.position="none"), deadTreesPlotOffCurb, ncol = 3), mylegend ,heights=c(20, 1),top = textGrob("Fig_5: Percentage of dead trees",gp=gpar(col = '#576161',fontsize=10,font=3)))

    percentages - map

    treesDead <- trees %>% #filter(status =='Dead') %>% select(nta, status) %>% group_by(nta, status) %>% summarize(n = n()) %>% mutate(pct = round(n / sum(n),3)*100) %>% ungroup() %>% filter(status == 'Dead')

    neighborhoodsManhattan <- neighborhoods %>% filter(boroname == 'Manhattan')

    neighborhoodsManhattanExclParks <- neighborhoodsManhattan %>% filter(ntacode != 'MN99')

    neighborhoodsManhattanExclParks <- neighborhoodsManhattanExclParks %>% left_join(treesDead, by = c('ntacode' = 'nta'))

    centroids = st_centroid(neighborhoodsManhattanExclParks)

    centroids <- centroids %>% mutate(geometry1 = gsub('[()°c\]', '', geometry)) %>% separate(col = geometry1, into = c('Latitude', 'Longitude'), sep = '\, ') %>% mutate(Latitude = as.numeric(Latitude), Longitude = as.numeric(Longitude)) %>% as.data.frame()

    deadTreesMapPlot <- ggplot() + geom_sf(data = neighborhoodsManhattanExclParks, aes(fill = pct), colour = "grey" ) + theme_void() + theme(legend.position = "none") + scale_fill_gradientn(colors = brewer.pal(n = 9, name = 'Reds')[1:9]) + geom_text(label = neighborhoodsManhattanExclParksLatitude, y = centroids$Longitude), size = 3)

    dead trees summary

    deadTreesPlot deadTreesPlotOnCurb deadTreesPlotOffCurb deadTreesMapPlot

    grid.arrange(arrangeGrob(deadTreesPlot,arrangeGrob( deadTreesPlotOnCurb, deadTreesPlotOffCurb,ncol = 2), ncol = 1), deadTreesMapPlot, ncol = 2)

    ALIVE TREES

    head(trees) glimpse(trees)

    trees %>% group_by(spc_common) %>% summarise(n = n()) %>% arrange(desc(n)) %>% mutate(nPerc = round(n/sum(n)*100,1), cumSum = cumsum(n), cumSumPerc = round(cumSum/sum(n)*100,1)) %>% view()

    treesTop10SpeciesVector <- trees %>% group_by(spc_common) %>% summarise(n = n()) %>% arrange(desc(n)) %>% mutate(nPerc = round(n/sum(n)*100,2), cumSum = cumsum(n), cumSumPerc = round(cumSum/sum(n)*100,2)) %>% ungroup() %>% head(n = 11) %>% drop_na() %>% as.data.frame()

    dfTest <- trees %>% filter(spc_common %in% c(treesTop10SpeciesVector$spc_common)) %>% left_join(neighborhoods, by = c('nta' = 'ntacode')) %>% select(ntaname) %>% arrange(desc(ntaname)) %>% unique() %>% mutate(lp = row_number())

    dfTest$lp <- rep(c('brown', 'darkgreen'), times = 28/2)

    trees %>% filter(spc_common %in% c(treesTop10SpeciesVector$spc_common)) %>% left_join(neighborhoods, by = c('nta' = 'ntacode')) %>% select(ntaname,spc_common, boroname) %>% group_by(ntaname,spc_common) %>% summarize(n = n()) %>% mutate(pct = round(n / sum(n),2) * 100) %>% ungroup %>% mutate(ntaname = factor(ntaname, unique(ntaname)), spc_common = factor(spc_common, unique(spc_common))) %>% mutate(xstart2 = row_number() - 0.5, xend2 = row_number()+0.5) %>% #mutate(ystart2 = col_number() - 0.5, yend2 = col_number()+0.5) %>% left_join(treesTop10SpeciesVector, by = c('spc_common' = 'spc_common')) %>% left_join(dfTest, by = c('ntaname' = 'ntaname')) %>% ggplot(aes(x = reorder(spc_common,-nPerc), y = ntaname, size = pct, group = spc_common,
    color = ntaname, label = paste0(pct,'%'), )) + #scale_size(range = c(1, 10)) + geom_point(position = position_dodge(width = 0.5), alpha = 0.5) + geom_text(size = 3, position = position_dodge(width = 0.5),show.legend = FALSE, size = 4, hjust = -1, vjust= 0.3) + scale_colour_manual(values = rep(c('black'), times = 28)) + geom_rect(aes(xmin = 1-0.5, xmax = 10+0.5, ymin = xstart2/10+0.5, ymax = xend2/10+0.5, fill = lp), alpha = 0.20, col = NA) + #scale_fill_manual(values = alpha(c("red", "green"))) + theme_minimal() + theme(axis.text.x = element_text(angle = 90, vjust = 1.1, hjust=1, size = 8), axis.text.y = element_text(size = 8)) + guides(size = FALSE) + theme(legend.position = "none") + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

    geom_rect( data = rect_data, # or rect_data for actual season end boundary mapping = aes( xmin = xstart2, xmax = xend2, ymin = 0, ymax = 10, fill = spc_common, alpha = 0.5 ))

    rect_data <- treesTop10SpeciesVector %>% select(spc_common) %>% mutate(xstart2 = row_number() - 0.5, xend2 = row_number()+0.5) %>% distinct()

    ggplot() + geom_rect( data = rect_data, # or rect_data for actual season end boundary mapping = aes( xmin = xstart2, xmax = xend2, ymin = 0, ymax = 10, fill = spc_common, alpha = 0.5 ))

    TREES - HEALTH

    TREES - HEALTH - GENERAL

    trees %>% filter(status =='Alive') %>% mutate(spc_common = 'All trees') %>% group_by(spc_common, curb_loc, health) %>% summarize(n = n()) %>% mutate(pct = round(n / sum(n),2)*100) %>% mutate(health = factor(health,levels = c("Good","Fair","Poor"))) %>% ggplot(aes(y = spc_common, x = curb_loc, size = pct, group = health,
    label = pct )) + scale_size(range = c(2, 5)) + geom_point(aes(color = health), position = position_dodge(width = 0.5), alpha = 0.5) + geom_text(size = 3, position = position_dodge(width = 0.5),show.legend = FALSE, size = 4, hjust = -0.8) + theme_minimal()+ guides(size = FALSE) + scale_color_manual(values = c("Good" = "green", "Fair" = "orange", 'Poor' = 'red'))

    TREES - HEALTH - DETAILES

    treesTop20 <- trees %>% group_by(spc_common) %>% summarise(n = n()) %>% arrange(desc(n)) %>% mutate(nPerc = round(n/sum(n)*100,2), cumSum = cumsum(n), cumSumPerc = round(cumSum/sum(n)*100,2)) %>% ungroup() %>% head(n = 21) %>% drop_na() %>% as.data.frame()

    treesAll <- trees %>% mutate(spc_common = 'All trees')

    treesHealthData <- rbind(trees, treesAll)

    treesHealthData %>% filter(spc_common %in% c(treesTop10Names$spc_common, 'All trees')) %>% filter(status =='Alive') %>% group_by(spc_common, curb_loc, health) %>% summarize(n = n()) %>% mutate(pct = round(n / sum(n),2)*100) %>% mutate(health = factor(health,levels = c("Good","Fair","Poor"))) %>% ggplot(aes(y = spc_common, x = curb_loc, size = pct, group = health,
    label = pct )) + scale_size(range = c(2, 5)) + geom_point(aes(color = health), position = position_dodge(width = 0.5), alpha = 0.5) + geom_text(size = 3, position = position_dodge(width = 0.5),show.legend = FALSE, size = 4, hjust = -0.8) + theme_minimal()+ guides(size = FALSE) + scale_color_manual(values = c("Good" = "green", "Fair" = "orange", 'Poor' = 'red'))

    TREES - HEALTH - MAPS

    treesHealth <- trees %>% filter(status =='Alive') %>% select(nta,health) %>% group_by(nta,health) %>% summarize(n = n()) %>% mutate(pct = round(n / sum(n),2)*100) %>% ungroup() %>% spread(key = health, value = pct) %>% select(-n) %>% group_by(nta) %>% replace(is.na(.), 0) %>% summarise(Fair = sum(Fair), Good = sum(Good), Poor = sum(Poor))

    neighborhoodsManhattan <- neighborhoods %>% filter(boroname == 'Manhattan')

    neighborhoodsManhattanExclParks <- neighborhoodsManhattan %>% filter(ntacode != 'MN99')

    neighborhoodsManhattanExclParks <- neighborhoodsManhattanExclParks %>% left_join(treesHealth, by = c('ntacode' = 'nta'))

    centroids = st_centroid(neighborhoodsManhattanExclParks)

    centroids <- centroids %>% mutate(geometry1 = gsub('[()°c\]', '', geometry)) %>% separate(col = geometry1, into = c('Latitude', 'Longitude'), sep = '\, ') %>% mutate(Latitude = as.numeric(Latitude), Longitude = as.numeric(Longitude)) %>% as.data.frame()

    plotManhattanFun <- function(health, colors, labelsTest) { ggplot() + geom_sf(data = neighborhoodsManhattanExclParks, aes_string(fill = health), colour = "grey" ) + theme_void() + theme(legend.position = "none") +

    theme(legend.position="bottom") +

    scale_fill_gradientn(colors = brewer.pal(n = 9, name = colors)[2:9]) + geom_text(label = labelsTest, aes(x = centroidsLongitude), size = 3) }

    grid.arrange(plotManhattanFun("Good", "Greens", neighborhoodsManhattanExclParksFair), plotManhattanFun("Poor", "Reds", neighborhoodsManhattanExclParks$Poor), ncol = 3)

    TREES - SIZE

    treesTop10 <- trees %>% group_by(spc_common) %>% summarise(n = n()) %>% arrange(desc(n)) %>% mutate(nPerc = round(n/sum(n)*100,2), cumSum = cumsum(n), cumSumPerc = round(cumSum/sum(n)*100,2)) %>% ungroup() %>% head(n = 11) %>% drop_na() %>% as.data.frame()

    outliers

    trees %>% filter(spc_common %in% (treesTop10$spc_common)) %>% select(spc_common, tree_dbh) %>% group_by(spc_common) %>% summarise(IQRRange = IQR(tree_dbh), median = median(tree_dbh), Q3 = quantile(tree_dbh, probs=c(.25, .75))[2], outlier = 1.5 * IQRRange + Q3)

    #boxplot treesTop10BoxPlot <- trees %>% filter(spc_common %in% (treesTop10$spc_common)) %>% select(spc_common, tree_dbh) %>% ggplot(aes(x = tree_dbh, y = reorder(spc_common,tree_dbh,median))) + geom_boxplot(color="black", fill='#4DAF4A', alpha= 0.5, outlier.size = 2, outlier.color = 'grey') + scale_x_continuous(limits=c(0,50)) + theme_minimal()

    #barChart treesTop10Median <- trees %>% filter(spc_common %in% (treesTop10$spc_common)) %>% group_by(spc_common, curb_loc) %>% summarize(medianDiameter = median(tree_dbh)) %>% ggplot(aes(y = reorder(spc_common,medianDiameter,median), x = medianDiameter, fill = curb_loc, label = paste0(medianDiameter))) + geom_col(width = 0.5, position = position_dodge(0.7), alpha = 0.7) + geom_text(width = 0.5, position = position_dodge(0.7), hjust = -0.2, vjust = 0.4, size = 3) + theme_minimal() + scale_x_continuous(limits = c(0,20)) + scale_fill_brewer(palette="Set1") + theme(#axis.text.x = element_blank(), #axis.ticks.x = element_blank() )

    grid.arrange(treesTop10BoxPlot, treesTop10Median, ncol = 2)

    brewer.pal(n = 9, name = 'Set1')

    theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1, size = 10))

    TOP TEN BEST TREES

    trees %>% select(spc_common, health) %>% head()

    trees <- as.data.frame(read_csv('Databases/trees1.csv')) trees <- trees %>% mutate(spc_common = str_to_title(spc_common))

    treesTop20Names <- trees %>% group_by(spc_common) %>% summarise(n = n()) %>% arrange(desc(n)) %>% mutate(nPerc = round(n/sum(n)*100,2), cumSum = cumsum(n), cumSumPerc = round(cumSum/sum(n)*100,2)) %>% ungroup() %>% head(n = 21) %>% drop_na() %>% select(spc_common)

    treesTop10Health <- trees %>% filter(spc_common %in% c(treesTop20Names$spc_common)) %>% select(spc_common,health) %>% group_by(spc_common,health) %>% summarize(n = n()) %>% mutate(pct = round(n / sum(n),2)*100) %>% filter(health == 'Good') %>% arrange((pct)) %>% ungroup() %>% mutate(pointsHealth = row_number())

    treesTop10Trunk <- trees %>% filter(spc_common %in% c(treesTop20Names$spc_common)) %>% select(spc_common,tree_dbh) %>% group_by(spc_common) %>% summarize(medianDiameter = median((tree_dbh))) %>% arrange((medianDiameter)) %>% mutate(pointsDiameter = row_number())

    treesTop10Ranking <- treesTop10Health %>% left_join(treesTop10Trunk, by = 'spc_common') %>% select(spc_common, pointsHealth, pointsDiameter) %>% mutate(pointsTotal = pointsHealth + pointsDiameter) %>% arrange(desc(pointsTotal)) %>% head(n = 10) %>% mutate(topTen = 'topTen')

    treesTop10RankingMapData <- trees %>% left_join(treesTop10Ranking, by = 'spc_common') %>% replace(is.na(.), 'oth') %>% select(nta, nta_name, topTen) %>% group_by(nta, nta_name, topTen) %>% summarise(n = n()) %>% mutate(pct = round(n / sum(n),2)*100) %>% filter(topTen == 'topTen') %>% arrange(desc(pct))

    neighborhoodsManhattan <- neighborhoods %>% filter(boroname == 'Manhattan')

    neighborhoodsManhattanExclParks <- neighborhoodsManhattan %>% filter(ntacode != 'MN99')

    centroidsManhattan = st_centroid(neighborhoodsManhattanExclParks)

    centroidsManhattan <- centroidsManhattan %>% mutate(geometry1 = gsub('[()°c\]', '', geometry)) %>% separate(col = geometry1, into = c('Latitude', 'Longitude'), sep = '\, ') %>% mutate(Latitude = as.numeric(Latitude), Longitude = as.numeric(Longitude)) %>% as.data.frame()

    neighborhoodsManhattanExclParksTopTenRanking <- neighborhoodsManhattanExclParks %>% left_join(treesTop10RankingMapData, by = c('ntacode' = 'nta'))

    neighborhoodsManhattanExclParksTopTenRankingPlot <- neighborhoodsManhattanExclParksTopTenRanking %>% ggplot() + geom_sf(data = neighborhoodsManhattanExclParksTopTenRanking, aes(fill = pct), colour = "grey") + theme_void() + theme(legend.position = "none") + theme(legend.position="bottom") + theme(legend.position = "none") + theme(#legend.position="left", legend.key.size = unit(0.5, 'cm'), legend.position = c(-0.05, 0.3), legend.title = element_text(size=7), legend.text = element_text(size=7)) + guides(fill=guide_legend(title="top 10 trees [%]")) + scale_fill_gradientn(colors = brewer.pal(n = 9, name = 'Greens')[1:8]) + geom_text(label = centroidsManhattanLatitude, y = centroidsManhattan$Longitude), size = 2)

    improvement_formatter <- formatter("span", style = x ~ style(font.weight = "bold", display = 'block', width = '100px', color = ifelse(x > 0, 'Green', ifelse(x < 0, 'Red',"black"))), x ~ icontext(ifelse(x>0, "arrow-up", "arrow-down"), x))

    customGreen = brewer.pal(n = 9 , name = "Greens")[1] customGreen0 = brewer.pal(n = 9 , name = "Greens")[8] customBlue = brewer.pal(n = 9 , name = "Blues")[1] customBlue0 = brewer.pal(n = 9 , name = "Blues")[6]

    costum_format1 <- formatter(.tag = "span", style = function(x) style( display = "block", padding = "0 4px", width = '150px', 'border-radius' = "4px", 'background-color' = csscolor(gradient(as.numeric(x),customGreen, customGreen0)), color = ifelse(x >= 3, "black","black")))

    costum_format2 <- formatter(.tag = "span", style = function(x) style( display = "block", width = "50px"))

    costum_format3 <- formatter(.tag = "span", style = function(x) style( display = "block", padding = "0 4px", width = '50px', 'border-radius' = "4px"))

    treesTop10RankingMapTable <- treesTop10RankingMapData %>% select(nta, nta_name, pct)

    treesTop10RankingMapTable

    testFormattable = formattable(treesTop10RankingMapTable, align =c("l","l","l"), list('nta' = costum_format3, 'ntaname' = costum_format2, pct = costum_format1 ))

    testFormattable %>%

    kable() %>%

    kable_styling(bootstrap_options = "bordered",

    full_width = FALSE) %>%

    add_header_above(c("", "Category1" = 2)) %>%

    collapse_rows(columns = 1,

    valign = "middle")

    webshot::install_phantomjs()

    export_formattable <- function(f, file, width = "60%", height = NULL, background = "white", delay = 0.2) { w <- as.htmlwidget(f, width = width, height = height) path <- htmltools::html_print(w, background = background, viewer = NULL) url <- paste0("file:///", gsub("\\", "/", normalizePath(path))) webshot(url, file = file, selector = ".formattable_widget", delay = delay) }

    export_formattable(testFormattable, 'testFormattable.jpg')

    img2 <- readJPEG('testFormattable.jpg') img2 = abind::abind(img2, img2[,,1]) img2[,,4] = 1

    h <-dim(img2)[1] # image height w <-dim(img2)[2] # image width

    plotTableTopTen <- ggplot() + annotation_custom(grid::rasterGrob(img2, width=unit(1,"npc"), height=unit(1,"npc")), 0, w, 0, -h) + scale_x_continuous(expand=c(0,0),limits=c(0,w)) + scale_y_reverse(expand=c(0,0),limits=c(h,0)) + coord_equal() + stat_bin2d(binwidth=2,aes(fill = ..density..)) + theme_void()

    plotTableTopTen neighborhoodsManhattanExclParksTopTenRankingPlot

    grid.arrange(neighborhoodsManhattanExclParksTopTenRankingPlot, plotTableTopTen, ncol = 2)

    filter(spc_common %in% c(treesTop10SpeciesVector$spc_common)) %>%

    treesTop10SpeciesVector <- trees %>% filter(spc_common %in% c(treesTop10Ranking$spc_common)) %>% group_by(spc_common) %>% summarise(n = n()) %>% arrange(desc(n)) %>% mutate(nPerc = round(n/sum(n)*100,2), cumSum = cumsum(n), cumSumPerc = round(cumSum/sum(n)*100,2)) %>% ungroup() %>% head(n = 10) %>% drop_na() %>% as.data.frame()

    dfTest <- trees %>% filter(spc_common %in% c(treesTop10SpeciesVector$spc_common)) %>% left_join(neighborhoods, by = c('nta' = 'ntacode')) %>% select(ntaname) %>% arrange(desc(ntaname)) %>% unique() %>% mutate(lp = row_number())

    dfTest$lp <- rep(c('brown', 'darkgreen'), times = 28/2)

    trees %>% filter(spc_common %in% c(treesTop10SpeciesVector$spc_common)) %>% left_join(neighborhoods, by = c('nta' = 'ntacode')) %>% select(ntaname,spc_common, boroname) %>% group_by(ntaname,spc_common) %>% summarize(n = n()) %>% mutate(pct = round(n / sum(n),2) * 100) %>% ungroup %>% complete(ntaname,spc_common, fill = list(stat = 0)) %>% replace(is.na(.), 0) %>% mutate(ntaname = factor(ntaname, unique(ntaname)), spc_common = factor(spc_common, unique(spc_common))) %>% mutate(xstart2 = row_number() - 0.5, xend2 = row_number()+0.5) %>% #mutate(ystart2 = col_number() - 0.5, yend2 = col_number()+0.5) %>% left_join(treesTop10SpeciesVector, by = c('spc_common' = 'spc_common')) %>% left_join(dfTest, by = c('ntaname' = 'ntaname')) %>% ggplot(aes(x = reorder(spc_common,-nPerc), y = ntaname, size = pct, group = spc_common,
    color = ntaname, label = paste0(pct,'%'), )) + #scale_size(range = c(1, 10)) + geom_point(position = position_dodge(width = 0.5), alpha = 0.5) + geom_text(size = 3, position = position_dodge(width = 0.5),show.legend = FALSE, size = 4, hjust = -1, vjust= 0.3) + scale_colour_manual(values = rep(c('black'), times = 28)) + geom_rect(aes(xmin = 1-0.5, xmax = 10+0.5, ymin = xstart2/10+0.5, ymax = xend2/10+0.5, fill = lp), alpha = 0.20, col = NA) + #scale_fill_manual(values = alpha(c("red", "green"))) + theme_minimal() + theme(axis.text.x = element_text(angle = 90, vjust = 1.1, hjust=1, size = 8), axis.text.y = element_text(size = 8)) + guides(size = FALSE) + theme(legend.position = "none") + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

    TOP TREES LOCATION vs SPECIES

    treesTop10SpeciesVector <- trees %>% filter(spc_common %in% c(treesTop10Ranking$spc_common)) %>% group_by(spc_common) %>% summarise(n = n()) %>% arrange(desc(n)) %>% mutate(nPerc = round(n/sum(n)*100,2), cumSum = cumsum(n), cumSumPerc = round(cumSum/sum(n)*100,2)) %>% ungroup() %>% head(n = 10) %>% drop_na() %>% as.data.frame()

    treesTop10Species <- trees %>% filter(spc_common %in% c(treesTop10SpeciesVector$spc_common)) %>% left_join(neighborhoods, by = c('nta' = 'ntacode')) %>% left_join(treesTop10SpeciesVector, by = c('spc_common' = 'spc_common')) %>% select(spc_common, ntaname, boroname, n) %>% filter(boroname == 'Manhattan') %>% #head() %>% ggplot(aes(x = reorder(spc_common,n), y = ntaname)) + geom_jitter(alpha=0.1, size = 0.5, width = 0.10, height = 0.20) + theme_minimal() + theme(axis.text.x = element_text(angle = 90, vjust = 1.1, hjust=1, size = 8), axis.text.y = element_text(size = 8) )

    trees %>% filter(spc_common %in% c(treesTop10SpeciesVector$spc_common)) %>% left_join(neighborhoods, by = c('nta' = 'ntacode')) %>% select(spc_common, ntaname, boroname) %>% group_by(spc_common, ntaname) %>% summarize(n = n()) %>% mutate(pct = round(n / sum(n),2) * 100) %>% ungroup %>% complete(ntaname,spc_common, fill = list(stat = 0)) %>% replace(is.na(.), 0) %>% left_join(treesTop10SpeciesVector, by = c('spc_common' = 'spc_common')) %>% ggplot(aes(reorder(x = spc_common,-nPerc), y = ntaname, size = pct, group = spc_common, color = spc_common, label = paste0(pct,'%') )) + #scale_size(range = c(1, 10)) + geom_point(position = position_dodge(width = 0.5), alpha = 0.7 ) + geom_text(size = 3, position = position_dodge(width = 0.5), show.legend = FALSE, size = 4, hjust = -1) + theme_minimal() + theme(axis.text.x = element_text(angle = 90, vjust = 1.1, hjust=1, size = 8), axis.title = element_blank(), axis.text.y = element_text(size = 8)) + guides(size = FALSE) + theme(legend.position = "none") + scale_colour_manual(values=c("Honeylocust" = "brown", "Ginkgo" = "darkgreen", "Pin Oak" = "brown",
    "Sophora" = "darkgreen",
    "London Planetree" = "brown", "American Elm" = "darkgreen", "Willow Oak" = "brown", "Green Ash" = "darkgreen", "Crab Apple" = "brown", "Golden Raintree" = "darkgreen"))

    TREES

    glimpse(trees)

    neighborhoodsManhattan <- neighborhoods %>% filter(boroname == 'Manhattan')

    neighborhoodsManhattanExclParks <- neighborhoodsManhattan %>% filter(ntacode != 'MN99')

    centroidsManhattan = st_centroid(neighborhoodsManhattanExclParks)

    treesSP <- st_as_sf(trees, coords = c('longitude','latitude'), crs = st_crs(neighborhoodsManhattan)) treesSPTest <- head(treesSP, n = 100, crs = st_crs(neighborhoodsManhattan))

    st_crs(neighborhoodsManhattan) == st_crs(treesSP)

    neighborhoodsManhattanExclParksTreesPlot <- ggplot() + geom_sf(data = treesSP, lwd = 0, aes(color = factor(status))) + scale_color_manual(values = c("green", "red")) +

    theme(legend.key.size = unit(3,"point")) +

    guides(color = guide_legend(override.aes = list(size = 2))) + geom_sf(data = neighborhoodsManhattanExclParks, lwd = 0, alpha = 0.3) + theme_void()

    neighborhoodsManhattanExclParks$area_sqkm <- st_area(neighborhoodsManhattanExclParks)

    neighborhoodsManhattanAreas <- neighborhoodsManhattanExclParks%>% select(ntacode, area_sqkm) %>% st_drop_geometry() %>% mutate(area_sqkm = as.numeric(area_sqkm)/10^6)

    manhattanTrees <- trees %>% select(nta,nta_name) %>% group_by(nta, nta_name) %>% summarise(n = n()) %>% ungroup() %>% left_join(neighborhoodsManhattanAreas, by = c('nta' = 'ntacode')) %>% mutate(treesPerAreaUnit = n / area_sqkm) %>% as.data.frame()

    neighborhoodsManhattanExclParksTrees <- neighborhoodsManhattanExclParks %>% left_join(manhattanTrees, by = c('ntacode'='nta'))

    neighborhoodsManhattanExclParksTreesPerKmPlot <- ggplot() + geom_sf(data = neighborhoodsManhattanExclParksTrees, aes(fill = treesPerAreaUnit), colour = "darkgrey" ) + theme_void() + theme(legend.position = "none") + theme(legend.position="left", legend.key.size = unit(0.5, 'cm'), legend.title = element_text(size=8)) + scale_fill_gradientn(colors = brewer.pal(n = 9 , name = "Greens"))

    grid.arrange(neighborhoodsManhattanExclParksTreesPerKmPlot, neighborhoodsManhattanExclParksTreesPlot, ncol = 2)

    DATACAMP

    neighborhoodsManhattan <- neighborhoods %>% filter(boroname == 'Manhattan')

    neighborhoodsManhattanExclParks <- neighborhoodsManhattan %>% filter(ntacode != 'MN99')

    centroidsManhattan = st_centroid(neighborhoodsManhattanExclParks)

    Boroughs

    neighborhoodsBoroughs <- neighborhoods %>% group_by(boroname) %>% summarise(abc = sum(as.numeric(shape_leng)))

    centroids = st_centroid(neighborhoodsBoroughs)

    centroids <- centroids %>% mutate(geometry1 = gsub('[()°c\]', '', geometry)) %>% separate(col = geometry1, into = c('Latitude', 'Longitude'), sep = '\, ') %>% mutate(Latitude = as.numeric(Latitude), Longitude = as.numeric(Longitude)) %>% as.data.frame()

    "#A84268"

    plotBoroughs <- ggplot() + geom_sf(data = neighborhoodsBoroughs, aes(fill = as.numeric(abc)), lwd = 0, colour = "white" ) + theme_void() + theme(legend.position = "none") + scale_fill_gradientn( colors = c("lightgray")) + #geom_point(data = centroids, aes(x = Latitude, y = Longitude)) + geom_text(label = centroidsLatitude, y = centroids$Longitude), size = 3) + geom_rect(mapping= aes( xmin = st_bbox(neighborhoodsManhattan)[1], ymin = st_bbox(neighborhoodsManhattan)[2], xmax = st_bbox(neighborhoodsManhattan)[3], ymax = st_bbox(neighborhoodsManhattan)[4]), fill = NA, colour = "black", size = 0.9 )

    plotBoroughs

    neighborhoodsManhattan <- neighborhoods %>% filter(boroname == 'Manhattan')

    neighborhoodsManhattanExclParks <- neighborhoodsManhattan %>% filter(ntacode != 'MN99')

    centroidsManhattan = st_centroid(neighborhoodsManhattanExclParks)

    centroidsManhattan <- centroidsManhattan %>% mutate(geometry1 = gsub('[()°c\]', '', geometry)) %>% separate(col = geometry1, into = c('Latitude', 'Longitude'), sep = '\, ') %>% mutate(Latitude = as.numeric(Latitude), Longitude = as.numeric(Longitude)) %>% as.data.frame()

    centroidsManhattanntaname) centroidsManhattanname)

    "#A84268"

    plotManhattan <- ggplot() + geom_sf(data = neighborhoodsManhattanExclParks, aes(fill = as.numeric(shape_area)), colour = "white" ) + theme_void() + theme(legend.position = "none") + scale_fill_gradientn( colors = c("#9DBF9E", "#FCB97D")) + #geom_point(data = centroidsManhattan, aes(x = Latitude, y = Longitude)) + geom_text(label = centroidsManhattanLatitude, y = centroidsManhattan$Longitude), size = 1.8) + geom_rect(mapping= aes( xmin = st_bbox(neighborhoodsManhattan)[1], ymin = st_bbox(neighborhoodsManhattan)[2], xmax = st_bbox(neighborhoodsManhattan)[3], ymax = st_bbox(neighborhoodsManhattan)[4]), fill = NA, colour = "black", size = 0.9 )

    plotManhattan

    neighborhoods$area_sqkm <- st_area(neighborhoods)

    neighborhoodsarea_sqkm)

    ggplot() + geom_sf(data = neighborhoodsManhattanExclParks, lwd = 0, alpha = 0.3) + theme_void()+ geom_sf(data = subset(neighborhoods, ntaname == 'East Village'), size = 0.05, color = "green") + geom_sf(data = subset(neighborhoods, ntaname == 'Manhattanville'), size = 0.05, color = "red")

    neighborhoods %>% filter(ntaname %in% c('East Village','Manhattanville'))

    ggplot() + geom_sf(data = neighborhoodsManhattan, lwd = 0, )

    trees %>% filter(status == 'Dead') %>% ggplot() + geom_point(aes(x = latitude, y = longitude, color = status), size = 0.3, alpha= 0.3) + theme_minimal() + geom_sf(data = neighborhoodsManhattan, aes(fill = as.numeric(shape_area)), lwd = 0, colour = "white" ) + theme_void() + theme(legend.position = "none") + scale_fill_gradientn( colors = c("#9DBF9E", "#FCB97D", "#b8b8b8")) + #geom_point(data = centroidsManhattan, aes(x = Latitude, y = Longitude)) + geom_text(label = centroidsManhattanLatitude, y = centroidsManhattan$Longitude), size = 1.8) + geom_rect(mapping= aes( xmin = st_bbox(neighborhoodsManhattan)[1], ymin = st_bbox(neighborhoodsManhattan)[2], xmax = st_bbox(neighborhoodsManhattan)[3], ymax = st_bbox(neighborhoodsManhattan)[4]), fill = NA, colour = "black", size = 0.6 )

    install.packages('formattable') library(formattable)

    formattable(i1, align =c("l","c","c","c","c", "c", "c", "c", "r"), list(Indicator Name = formatter("span", style = ~ style(color = "grey",font.weight = "bold")), 2011= color_tile(customGreen, customGreen0), 2012= color_tile(customGreen, customGreen0), 2013= color_tile(customGreen, customGreen0), 2014= color_tile(customGreen, customGreen0), 2015= color_tile(customGreen, customGreen0), 2016= color_tile(customGreen, customGreen0), Average = color_bar(customRed) ))

    Manhattan trees density

    neighborhoodsManhattan <- neighborhoods %>% filter(boroname == 'Manhattan')

    neighborhoodsManhattanExclParks <- neighborhoodsManhattan %>% filter(ntacode != 'MN99')

    centroidsManhattan = st_centroid(neighborhoodsManhattanExclParks)

    centroidsManhattan <- centroidsManhattan %>% mutate(geometry1 = gsub('[()°c\]', '', geometry)) %>% separate(col = geometry1, into = c('Latitude', 'Longitude'), sep = '\, ') %>% mutate(Latitude = as.numeric(Latitude), Longitude = as.numeric(Longitude)) %>% as.data.frame()

    centroidsManhattanntaname) centroidsManhattanname)

    treesSP <- st_as_sf(trees, coords = c('longitude','latitude'), crs = st_crs(neighborhoodsManhattan)) treesSPTest <- head(treesSP, n = 100, crs = st_crs(neighborhoodsManhattan))

    st_crs(neighborhoodsManhattan) == st_crs(treesSP)

    neighborhoodsManhattanExclParks$area_sqkm <- st_area(neighborhoodsManhattanExclParks)

    neighborhoodsManhattanAreas <- neighborhoodsManhattanExclParks%>% select(ntacode, area_sqkm) %>% st_drop_geometry() %>% mutate(area_sqkm = as.numeric(area_sqkm)/10^6)

    manhattanTrees <- trees %>% select(nta,nta_name) %>% group_by(nta, nta_name) %>% summarise(n = n()) %>% ungroup() %>% left_join(neighborhoodsManhattanAreas, by = c('nta' = 'ntacode')) %>% mutate(treesPerAreaUnit = n / area_sqkm) %>% as.data.frame()

    customGreen = brewer.pal(n = 9 , name = "Greens")[1] customGreen0 = brewer.pal(n = 9 , name = "Greens")[8] customBlue = brewer.pal(n = 9 , name = "Blues")[1] customBlue0 = brewer.pal(n = 9 , name = "Blues")[6]

    neighborhoodsManhattanExclParksTrees <- neighborhoodsManhattanExclParks %>% left_join(manhattanTrees, by = c('ntacode'='nta'))

    neighborhoodsManhattanExclParksTreesPerKmPlot <- ggplot() + geom_sf(data = neighborhoodsManhattanExclParksTrees, aes(), colour = "darkgrey") + theme_void() + theme(legend.position = "none") + theme(#legend.position="left", legend.key.size = unit(0.6, 'cm'), legend.position = c(+1.55, 0.4), legend.title = element_text(size=7), legend.text = element_text(size=7)) + guides(fill=guide_legend(title="trees per km^2")) + #scale_fill_gradientn(colors = brewer.pal(n = 9 , name = "Blues")[1:8]) + geom_text(label = centroidsManhattanLatitude, y = centroidsManhattan$Longitude), size = 3)

    ################### SUMMARY ###################

    1.) Number of trees - map + backgroung

    plotBackgroundWithMap

    2.) Trees per km2 - map and table

    grid.arrange(neighborhoodsManhattanExclParksTreesPerKmPlot, manhattanTreesFormattablePlot, ncol = 2)

    3 Most common trees

    3a.) Most common trees - chart

    treesTop20

    3b.) Most common trees - top 10 trees vs nta - chart

    mostCommonTreesDist

    4.) Curb location - ON / OFF CURB

    grid.arrange(treesCurb, onCurbTreesMapPlot, ncol = 2)

    5.) Health - All trees

    treesHealtPlot

    6.) Health - Map of ntas

    grid.arrange(neighborhoodsManhattanDict, plotManhattanFun("Good", "Greens", neighborhoodsManhattanExclParksFair, '% of trees with fair health'), plotManhattanFun("Poor", "Reds", neighborhoodsManhattanExclParks$Poor, '% of trees with poor health'), ncol = 4)

    7.) Trunk

    grid.arrange(treesTop10BoxPlot, treesTop10Median, ncol = 2)

    8.) Recommended trees - table with ratings

    treesTop10RankingFormattable

    9.) Appendix -dead trees

    grid.arrange(arrangeGrob(plot1, plot2, plot3, ncol = 3), heights=c(20, 1),top = textGrob("Fig_6: Percentage of dead trees",gp=gpar(col = '#576161',fontsize=10,font=3)))

    8 Health - map of nta (add legend)

    9 Trunk

    11 Recommended trees - map

    12 Recommended trees - details table

    formattable(manhattanTreesTable, align =c("l","l","l"), list('nta' = costum_format3, 'ntaname' = costum_format2, treesPerKm2 = image_tile ))