You will see # TASK ///// through out this template. This indicates the beginning of a task. Right below it will be instructions for the task. Each # TASK ///// will be paired with # //TASK ///// to indicate where that specific task ends.
For example, if you need something like below…
# TASK ////////////////////////////////////////////////////////////////////////
# create a vector with element 1,2,3 and assign it into `my_vec` object
# **YOUR CODE HERE..**
# //TASK //////////////////////////////////////////////////////////////////////
What I expect you to do is to replace where it says
# **YOUR CODE HERE..** with your answer, like below.
# TASK ////////////////////////////////////////////////////////////////////////
# create a vector with element 1,2,3 and assign it into `my_vec` object
# my_vec <- c(1,2,3)
# //TASK //////////////////////////////////////////////////////////////////////
There can be multi-step instructions, like shown below. You may use pipe (%>%) to link multiple functions to perform the task in the instruction. Make sure that you assign the output of your task into an object with the specified name. This is to make sure that your code will run smoothly - if you change the name of the object (i.e., subset_car in the example below), all the subsequent code will NOT run properly.
# TASK ////////////////////////////////////////////////////////////////////////
# 1. Using mtcars object, extract rows where cyl equals 4
# 2. Select mpg and disp columns
# 3. Create a new column 'summation' by adding mpg and disp
# 4. assign it into `subset_car` object
# subset_car <- # **YOUR CODE HERE..**
# //TASK //////////////////////////////////////////////////////////////////////
I expect you to repalce where it says
# **YOUR CODE HERE..** with your answer, like below.
# TASK ////////////////////////////////////////////////////////////////////////
# 1. Using mtcars object, extract rows where cyl equals 4
# 2. Select mpg and disp columns
# 3. Create a new column 'summation' by adding mpg and disp
# 4. assign it into `subset_car` object
# subset_car <- mtcars %>%
# filter(cyl == 4) %>%
# select(mpg, disp) %>%
# mutate(summation = mpg + disp)
# //TASK //////////////////////////////////////////////////////////////////////
There will also be multiple ‘NO MODIFICATION ZONE’. Do not modify code in the No Modification Zone.
You will need to knit it, publish it on Rpubs, and submit the link. If there is any question about this template, do not hesitate to reach out to Bonwoo.
In the first section, you need to select one Census Tract that you think is the most walkable and another one that you think is least walkable within Fulton and DeKalb Counties, GA. As long as the two Census Tracts are within the two counties, you can pick any two you want. If the area you want to use as walkable/unwalkable area is not well-covered by one Census Tract, you can select multiple tracts (e.g., selecting three adjacent tracts as one walkable area). The definition of ‘walkable’ can be your own - you can choose solely based on your experience (e.g., had best/worst walking experience), refer to Walk Score, or any other mix of criteria you want. After you make the selection, provide a short write-up of why you chose those Census Tracts.
The second section is the main part of this assignment in which you prepare OSM data, download GSV images, apply computer vision.
In the third section, you will summarise and analyze the output and provide your findings. After you apply computer vision to the images, you will have the number of pixels in each image that represent 150 categories in your data. You will focus on the following categories in your analysis: building, sky, tree, road, and sidewalk. Specifically, you will (1) create maps to visualize the spatial distribution of different objects, (2) compare the mean of each category between the two Census Tract and (3) draw boxplots to compare the distributions.
Provide a brief description of your census tracts. Why do you think the Census Tracts are walkable and unwalkable? What were the contributing factors? ——-I am new to the city and I dont know much about it hence I have chosen the two which are a bit far away and let the calculations give answers in section 3.
Fill out the template to complete the script.
library(tidyverse)
library(tidycensus)
library(osmdata)
library(sfnetworks)
library(units)
library(sf)
library(tidygraph)
library(tmap)
library(here)
The getbb() function, which we used in the class
material to download OSM data, isn’t suitable for downloading just two
Census Tracts. We will instead use an alternative method.
# TASK ////////////////////////////////////////////////////////////////////////
# 1. Set up your api key here
census_api_key(Sys.getenv("google_api"))
## To install your API key for use in future sessions, run this function with `install = TRUE`.
# //TASK //////////////////////////////////////////////////////////////////////
# =========== NO MODIFICATION ZONE STARTS HERE ===============================
# Download Census Tract polygon for Fulton and DeKalb
tract <- get_acs("tract",
variables = c('tot_pop' = 'B01001_001'),
year = 2020,
state = "GA",
county = c("Fulton", "DeKalb"),
geometry = TRUE)
## Getting data from the 2016-2020 5-year ACS
## Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
##
|
| | 0%
|
| | 1%
|
|= | 1%
|
|== | 2%
|
|== | 3%
|
|=== | 4%
|
|==== | 5%
|
|==== | 6%
|
|===== | 7%
|
|====== | 8%
|
|====== | 9%
|
|======= | 10%
|
|======== | 11%
|
|======== | 12%
|
|========= | 13%
|
|========== | 14%
|
|========== | 15%
|
|=========== | 16%
|
|============ | 17%
|
|============ | 18%
|
|============= | 19%
|
|============== | 19%
|
|============== | 20%
|
|=============== | 22%
|
|================ | 22%
|
|================ | 23%
|
|================= | 24%
|
|================== | 25%
|
|================== | 26%
|
|=================== | 27%
|
|==================== | 28%
|
|==================== | 29%
|
|===================== | 30%
|
|====================== | 31%
|
|====================== | 32%
|
|======================= | 32%
|
|======================= | 33%
|
|======================== | 34%
|
|======================== | 35%
|
|========================= | 35%
|
|========================= | 36%
|
|========================== | 37%
|
|=========================== | 39%
|
|============================ | 40%
|
|============================= | 41%
|
|============================= | 42%
|
|============================== | 42%
|
|============================== | 43%
|
|=============================== | 44%
|
|=============================== | 45%
|
|================================ | 45%
|
|================================ | 46%
|
|================================= | 47%
|
|================================= | 48%
|
|================================== | 48%
|
|=================================== | 50%
|
|=================================== | 51%
|
|==================================== | 51%
|
|==================================== | 52%
|
|===================================== | 53%
|
|====================================== | 54%
|
|======================================= | 55%
|
|======================================= | 56%
|
|======================================== | 57%
|
|========================================= | 58%
|
|========================================= | 59%
|
|========================================== | 59%
|
|========================================== | 60%
|
|=========================================== | 61%
|
|=========================================== | 62%
|
|============================================ | 62%
|
|============================================= | 64%
|
|============================================== | 65%
|
|============================================== | 66%
|
|=============================================== | 67%
|
|=============================================== | 68%
|
|================================================ | 68%
|
|================================================= | 69%
|
|================================================= | 70%
|
|================================================== | 71%
|
|=================================================== | 73%
|
|==================================================== | 74%
|
|===================================================== | 75%
|
|===================================================== | 76%
|
|====================================================== | 76%
|
|====================================================== | 77%
|
|======================================================= | 78%
|
|======================================================= | 79%
|
|======================================================== | 80%
|
|======================================================== | 81%
|
|========================================================= | 81%
|
|========================================================= | 82%
|
|=========================================================== | 84%
|
|=========================================================== | 85%
|
|============================================================ | 86%
|
|============================================================= | 87%
|
|============================================================= | 88%
|
|============================================================== | 88%
|
|=============================================================== | 90%
|
|=============================================================== | 91%
|
|================================================================ | 92%
|
|================================================================= | 92%
|
|================================================================= | 93%
|
|=================================================================== | 95%
|
|=================================================================== | 96%
|
|==================================================================== | 97%
|
|===================================================================== | 98%
|
|===================================================================== | 99%
|
|======================================================================| 100%
# =========== NO MODIFY ZONE ENDS HERE ========================================
# TASK ////////////////////////////////////////////////////////////////////////
# The purpose of this TASK is to create one bounding box for walkable Census Tract and another bounding box for unwalkable Census Tract.
# As long as you generate what's needed for the subsequent codes, you are good. The numbered list of tasks below is to provide some hints.
# 1. Write the GEOID of walkable & unwalkable Census Tracts. e.g., tr1_ID <- c("13121001205", "13121001206")
# 2. Extract the selected Census Tracts using tr1_ID & tr2_ID
# 3. Create their bounding boxes using st_bbox(), and
# 4. assign them to tract_1_bb and tract_1_bb, respectively.
# For the walkable Census Tract(s)
# 1.
tr1_ID <- c("13089023425")
# **YOUR CODE HERE..** --> For example, tr1_ID <- c("13121001205", "13121001206").
# 2~4
tract_1_bb <- tract %>% filter(GEOID == tr1_ID) %>% st_bbox()
# **YOUR CODE HERE..**
# For the unwalkable Census Tract(s)
# 1.
tr2_ID <- c("13121008907")# **YOUR CODE HERE..**
# 2~4
tract_2_bb <- tract %>% filter(GEOID == tr2_ID) %>% st_bbox()
# st_bbox(tr2_ID)
# **YOUR CODE HERE..**
# //TASK //////////////////////////////////////////////////////////////////////
# =========== NO MODIFICATION ZONE STARTS HERE ===============================
# Get OSM data for the two bounding box
osm_1 <- opq(bbox = tract_1_bb) %>%
add_osm_feature(key = 'highway',
value = c("motorway", "trunk", "primary",
"secondary", "tertiary", "unclassified",
"residential")) %>%
osmdata_sf() %>%
osm_poly2line()
osm_2 <- opq(bbox = tract_2_bb) %>%
add_osm_feature(key = 'highway',
value = c("motorway", "trunk", "primary",
"secondary", "tertiary", "unclassified",
"residential")) %>%
osmdata_sf() %>%
osm_poly2line()
# =========== NO MODIFY ZONE ENDS HERE ========================================
# TASK ////////////////////////////////////////////////////////////////////////
# 1. Convert osm_1 and osm_2 to sfnetworks objects (set directed = FALSE)
# 2. Clean the network by (1) deleting parallel lines and loops, (2) create missing nodes, and (3) remove pseudo nodes,
# 3. Add a new column named length using edge_length() function.
# net1_1 <- sfnetworks::as_sfnetwork(osm_1$osm_lines, directed = FALSE) %>% activate("edges") %>%
# filter(!edge_is_multiple()) %>%
# filter(!edge_is_loop())
#
# net1 <- convert(net1_1, sfnetworks::to_spatial_subdivision)
net1 <- osm_1$osm_lines %>%
sfnetworks::as_sfnetwork(directed = FALSE) %>%
activate("edges") %>%
filter(!edge_is_multiple()) %>%
filter(!edge_is_loop()) %>%
convert(., sfnetworks::to_spatial_subdivision) %>%
convert(., sfnetworks::to_spatial_smooth) %>%
mutate(length = edge_length())
## Warning: to_spatial_subdivision assumes attributes are constant over geometries
net2 <- osm_2$osm_lines %>%
sfnetworks::as_sfnetwork(directed = FALSE) %>%
activate("edges") %>%
filter(!edge_is_multiple()) %>%
filter(!edge_is_loop()) %>%
convert(., sfnetworks::to_spatial_subdivision) %>%
convert(., sfnetworks::to_spatial_smooth) %>%
mutate(length = edge_length())
## Warning: to_spatial_subdivision assumes attributes are constant over geometries
# **YOUR CODE HERE..**
# //TASK //////////////////////////////////////////////////////////////////////
# =========== NO MODIFICATION ZONE STARTS HERE ===============================
# OSM for the walkable part
edges_1 <- net1 %>%
# Extract 'edges'
st_as_sf("edges") %>%
# Drop redundant columns
select(osm_id, highway,length) %>%
# Drop segments that are too short (100m)
mutate(length = as.vector(length))%>%
filter(length > 50) %>%
# Add a unique ID for each edge
mutate(edge_id = seq(1,nrow(.)),
is_walkable = "walkable")
# OSM for the unwalkable part
edges_2 <- net2 %>%
# Extract 'edges'
st_as_sf("edges") %>%
# Drop redundant columns
select(osm_id, highway,length) %>%
# Drop segments that are too short (100m)
mutate(length = as.vector(length))%>%
filter(length > 50) %>%
# Add a unique ID for each edge
mutate(edge_id = seq(1,nrow(.)),
is_walkable = "unwalkable")
# Merge the two
edges <- bind_rows(edges_1, edges_2)
# =========== NO MODIFY ZONE ENDS HERE ========================================
get_azi <- function(line){
# This function takes one edge (i.e., a street segment) as an input and
# outputs a data frame with four points (start, mid1, mid2, and end) and their azimuth.
# TASK ////////////////////////////////////////////////////////////////////////
# 1. From `line` object, extract the coordinates using st_coordinates() and extract the first two rows.
# 2. Use atan2() function to calculate the azimuth in degree.
# Make sure to adjust the value such that 0 is north, 90 is east, 180 is south, and 270 is west.
# 1
start_p <- line %>% st_coordinates() %>%
.[1:2,1:2]
# **YOUR CODE HERE..**
# 2
start_azi <- atan2(start_p[2,"X"] - start_p[1, "X"],
start_p[2,"Y"] - start_p[1, "Y"])*180/pi# **YOUR CODE HERE..** --> For example, atan2()..
# //TASK //////////////////////////////////////////////////////////////////////
# TASK ////////////////////////////////////////////////////////////////////////
# Repeat what you did above, but for last two rows (instead of the first two rows).
# Remember to flip the azimuth so that the camera would be looking at the street that's being measured
end_p <- line %>% st_coordinates() %>%
.[(nrow(.)-1):nrow(.),1:2]
# **YOUR CODE HERE..**
end_azi <- atan2(end_p[2,"X"] - end_p[1, "X"],
end_p[2,"Y"] - end_p[1, "Y"])*180/pi# **YOUR CODE HERE..** --> For example, atan2()..
end_azi <- if (end_azi < 180) {end_azi + 180} else {end_azi - 180}
# //TASK //////////////////////////////////////////////////////////////////////
# TASK ////////////////////////////////////////////////////////////////////////
# 1. From `line` object, use st_line_sample() function to generate points at 0.45 and 0.55 locations. These two points will be used to calculate the azimuth.
# 2. Use st_case() function to convert 'MULTIPOINT' object to 'POINT' object.
# 3. Extract coordinates using st_coordinates().
# 4. Use atan2() functino to Calculate azimuth.
# 5. Use st_line_sample() again to generate a point at 0.5 location and get its coordinates. This point will be the location at which GSV image will be downloaded.
mid_p <- line %>%
st_geometry() %>%
.[[1]] %>%
st_line_sample(sample = c(0.45, 0.55)) %>%
st_cast("POINT") %>%
st_coordinates()
mid_azi <- atan2(mid_p[2,"X"] - mid_p[1, "X"],
mid_p[2,"Y"] - mid_p[1, "Y"])*180/pi
mid_p <- line %>%
st_geometry() %>%
.[[1]] %>%
st_line_sample(sample = 0.5) %>%
st_coordinates() %>%
.[1,1:2]
# **YOUR CODE HERE..** --> For 0.5 point
# //TASK //////////////////////////////////////////////////////////////////////
# =========== NO MODIFICATION ZONE STARTS HERE ===============================
return(tribble(
~type, ~X, ~Y, ~azi,
"start", start_p[1,"X"], start_p[1,"Y"], start_azi,
"mid1", mid_p["X"], mid_p["Y"], mid_azi,
"mid2", mid_p["X"], mid_p["Y"], ifelse(mid_azi < 180, mid_azi + 180, mid_azi - 180),
"end", end_p[2,"X"], end_p[2,"Y"], end_azi))
# =========== NO MODIFY ZONE ENDS HERE ========================================
}
We can apply get_azi() function to the edges object. We
finally append edges object to make use of the columns in
edges object (e.g., is_walkable column). When
you are finished with this code chunk, you will be ready to download GSV
images.
# TASK ////////////////////////////////////////////////////////////////////////
# Apply get_azi() function to all edges.
# Remember that you need to pass edges object to st_geometry()
# before you apply get_azi()
endp_azi <- edges %>%
st_geometry() %>%
map_df(get_azi)
# //TASK //////////////////////////////////////////////////////////////////////
# =========== NO MODIFICATION ZONE STARTS HERE ===============================
endp <- endp_azi %>%
bind_cols(edges %>%
st_drop_geometry() %>%
slice(rep(1:nrow(edges),each=4))) %>%
st_as_sf(coords = c("X", "Y"), crs = 4326, remove=FALSE) %>%
mutate(node_id = seq(1, nrow(.)))
# =========== NO MODIFY ZONE ENDS HERE ========================================
get_image <- function(iterrow){
# This function takes one row of endp and downloads GSV image using the information from endp.
# TASK ////////////////////////////////////////////////////////////////////////
# Finish this function definition.
# 1. Extract required information from the row of endp, including
# type (i.e., start, mid1, mid2, end), location, heading, edge_id, node_id, source (i.e., outdoor vs. default) and key.
# 2. Format the full URL and store it in furl. Refer to this page: https://developers.google.com/maps/documentation/streetview/request-streetview
# 3. Format the full path (including the file name) of the image being downloaded and store it in fpath
type = iterrow$type
location <- paste0(iterrow$Y %>% round(4), ",", iterrow$X %>% round(4))
heading <- iterrow$azi %>% round(1)
edge_id <- iterrow$edge_id
node_id <- iterrow$node_id
highway <- iterrow$highway
key <- Sys.getenv("google_api_2")
furl <- glue::glue("https://maps.googleapis.com/maps/api/streetview?size=640x640&location={location}&heading={heading}&fov=90&pitch=0&key={key}")
fname <- glue::glue("GSV-nid_{node_id}-eid_{edge_id}-type_{type}-Location_{location}-heading_{heading}.jpg") # Don't change this code for fname
fpath <- here("D:/Georgia Tech/Spec topic_",fname)
download.file(furl, fpath, mode = 'wb')
# **YOUR CODE HERE..**
# //TASK //////////////////////////////////////////////////////////////////////
# =========== NO MODIFICATION ZONE STARTS HERE ===============================
# Download images
if (!file.exists(fpath)){
download.file(furl, fpath, mode = 'wb')
}
# =========== NO MODIFY ZONE ENDS HERE ========================================
}
Before you download GSV images, make sure
the row number of endp is not too large! The row number of
endp will be the number of GSV images you will be
downloading. Before you download images, always double-check your Google
Cloud Console’s Billing tab to make sure that you will not go above the
free credit of $200 each month. The price is $7 per 1000 images.
# =========== NO MODIFICATION ZONE STARTS HERE ===============================
# Loop!
for (i in seq(1,nrow(endp))){
get_image(endp[i,])
}
# =========== NO MODIFY ZONE ENDS HERE ========================================
Now, you need to upload the images you downloaded to Google Drive. You should upload the images to the same folder that we used in class - the ‘demo_images’ folder in the root directory of Google Drive. Then, use Google Colab to apply a semantic segmentation model called Pyramid Scene Parsing Network.
Once all of the images are processed and saved in your Google Drive as a CSV file, download the CSV file and merge it back to edges.
# Read the downloaded CSV file from Google Drive
pspnet <- read.csv("D:/Georgia Tech/Spec topic_/seg_output.csv")
# =========== NO MODIFICATION ZONE STARTS HERE ===============================
# Join the pspnet object back to endp object using node_id as the join key.
pspnet_nodes <- endp %>% inner_join(pspnet, by="node_id") %>%
select(type, X, Y, node_id, building, sky, tree, road, sidewalk, is_walkable) %>%
mutate(across(c(building, sky, tree, road, sidewalk), function(x) x/(640*640)))
# =========== NO MODIFY ZONE ENDS HERE ========================================
At the beginning of this assignment, you defined one Census Tract as walkable and the other as unwalkable. The key to the following analysis is the comparison between walkable/unwalkable Census Tracts.
You need to create maps of the proportion of building, sky, tree, road, and sidewalk for walkable and unwalkable areas. In total, you will have 10 maps (two Census Tracts times five categories).
Below the maps, provide a brief description of your findings from the maps.
# TASK ////////////////////////////////////////////////////////////////////////
# Create map(s) to visualize the `pspnet_nodes` objects.
# As long as you can deliver the message clearly, you can use any format/package you want.
pspnet_nodes <- pspnet_nodes %>%
mutate(pct_building = building*100) %>%
mutate(pct_sky = sky*100) %>%
mutate(pct_tree = tree*100) %>%
mutate(pct_road = road*100) %>%
mutate(pct_sidewalk = sidewalk*100)
walkable <- pspnet_nodes %>%
filter(is_walkable == "walkable")
unwalkable <- pspnet_nodes %>%
filter(is_walkable == "unwalkable")
tmap_mode("view")
## tmap mode set to interactive viewing
tm_basemap("OpenStreetMap")+ tm_shape(pspnet_nodes) + tm_dots(col = "is_walkable", style = "quantile")
The above map shows the positions and the distribution of point of nodes through out the city. As we can see the bottom cluster is more dense in comparison to the other area.
map1<- tm_basemap("OpenStreetMap")+ tm_shape(pspnet_nodes %>%
filter(is_walkable == "walkable"))+tm_dots(col = "pct_tree", style="quantile")+ tm_layout(legend.width = 0.25, title = "Percentage of tree in the image")
map1_1 <- tm_basemap("OpenStreetMap") + tm_shape(pspnet_nodes %>% filter(is_walkable == "unwalkable")) + tm_dots(col = "pct_tree", style="quantile")+ tm_layout(legend.width = 0.25, title = "Percentage of tree in the image ")
tmap_mode("view")
## tmap mode set to interactive viewing
tmap_arrange(map1, map1_1, outer.margins = 0, ncol = 2)
# //TASK //////////////////////////////////////////////////////////////////////
The above map shows the points darker in the left map which indicates the denser distribution of trees as we can also confirm with the green patches on the baseman. This comparison shows that the area on the right side is more unwalkable and has lesser shade areas.
map2 <- tm_basemap("OpenStreetMap")+ tm_shape(pspnet_nodes %>%
filter(is_walkable == "walkable"))+tm_dots(col = "pct_sky", style="quantile")+ tm_layout(legend.width = 0.25, title = "Percentage of sky in the image (Walkable)")
map2_2 <- tm_basemap("OpenStreetMap") + tm_shape(pspnet_nodes %>% filter(is_walkable == "unwalkable")) + tm_dots(col = "pct_sky", style="quantile")+ tm_layout(legend.width = 0.25, title = "Percentage of sky in the image ")
tmap_mode("view")
## tmap mode set to interactive viewing
tmap_arrange(map2, map2_2, outer.margins = 0, ncol = 2)
The higher amount of sky in the images indicates that there is more sense of openness and helps enhance the urban design aspect of the area. As we can see in both the cases the points are darker in the left image in comparison to the right image.
map3<- tm_basemap("OpenStreetMap")+ tm_shape(pspnet_nodes %>%
filter(is_walkable == "walkable"))+tm_dots(col = "pct_building", style="quantile")+ tm_layout(legend.width = 0.25, title = "Percentage of building in the image (Walkable)")
map3_3 <- tm_basemap("OpenStreetMap") + tm_shape(pspnet_nodes %>% filter(is_walkable == "unwalkable")) + tm_dots(col = "pct_building", style="quantile")+ tm_layout(legend.width = 0.25, title = "Percentage of building in the image ")
tmap_mode("view")
## tmap mode set to interactive viewing
tmap_arrange(map3, map3_3, outer.margins = 0, ncol = 2)
map4<- tm_basemap("OpenStreetMap")+ tm_shape(pspnet_nodes %>%
filter(is_walkable == "walkable"))+tm_dots(col = "pct_sidewalk", style="quantile")+ tm_layout(legend.width = 0.25, title = "Percentage of sidewalk in the image (Walkable)")
map4_4 <- tm_basemap("OpenStreetMap") + tm_shape(pspnet_nodes %>% filter(is_walkable == "unwalkable")) + tm_dots(col = "pct_sidewalk", style="quantile")+ tm_layout(legend.width = 0.25, title = "Percentage of siewalk in the image ")
tmap_mode("view")
## tmap mode set to interactive viewing
tmap_arrange(map4, map4_4, outer.margins = 0, ncol = 2)
The maps above show the comparison of the perrcentage of sidewalks present in both the areas. We can observe that both has medium amount of sidewalk distribution with some node points with good sidewalks.
map5<- tm_basemap("OpenStreetMap")+ tm_shape(pspnet_nodes %>%
filter(is_walkable == "walkable"))+tm_dots(col = "pct_road", style="quantile")+ tm_layout(legend.width = 0.25, title = "Percentage of road in the image (Walkable)")
# map5
map5_5 <- tm_basemap("OpenStreetMap") + tm_shape(pspnet_nodes %>% filter(is_walkable == "unwalkable")) + tm_dots(col = "pct_road", style="quantile") +tm_layout(legend.width = 0.25, title = "Percentage of road in the image ")
# map5_5
tmap_mode("view")
## tmap mode set to interactive viewing
tmap_arrange(map5, map5_5, outer.margins = 0, ncol = 2)
The amount of roads in the image is darker in the left image in comparison to the right image. And we can also observe that the points are darker where the roads are broader as it leads to more percentage of roads in that area. ### Analysis 2 - Compare the means.
You need to calculate the mean of the proportion of building, sky, tree, road, and sidewalk for walkable and unwalkable areas. For example, you need to calculate the mean of building category for each of walkable and unwalkable Census Tracts. Then, you need to calculate the mean of sky category for each of walkable and unwalkable Census Tracts. In total, you will have 10 mean values. After the calculation, provide a brief description of your findings.
#for walkable caculation
tree_mean <- mean(walkable$pct_tree)
print(tree_mean)
## [1] 20.45717
sky_mean <- mean(walkable$sky *100)
print(sky_mean)
## [1] 26.40795
build_mean <- mean(walkable$building*100)
print(build_mean)
## [1] 2.08171
road_mean <- mean(walkable$road *100 )
print(road_mean)
## [1] 29.8378
sdw_mean <- mean(walkable$sidewalk*100)
print(sdw_mean)
## [1] 0.6962871
#for unwalkbale means calculations
un_tree_mean <- mean(unwalkable$tree*100)
print(un_tree_mean)
## [1] 24.21465
un_sky_mean <- mean(unwalkable$sky*100)
print(un_sky_mean)
## [1] 24.71843
un_build_mean <- mean(unwalkable$building*100)
print(un_build_mean)
## [1] 2.274819
un_road_mean <- mean(unwalkable$road *100)
print(un_road_mean)
## [1] 31.78144
un_sdw_mean <- mean (unwalkable$sidewalk*100)
print(un_sdw_mean)
## [1] 0.6546045
df <- data.frame(Factors= c("Buildings", "Sky", "Trees", "Roads", "Sidewalks"), Walkbale_means = c(tree_mean, sky_mean, build_mean, road_mean, sdw_mean), Unwalkable_means = c(un_tree_mean, un_sky_mean, un_build_mean, un_road_mean, un_sdw_mean))
df
## Factors Walkbale_means Unwalkable_means
## 1 Buildings 20.4571710 24.2146459
## 2 Sky 26.4079517 24.7184276
## 3 Trees 2.0817105 2.2748192
## 4 Roads 29.8377970 31.7814421
## 5 Sidewalks 0.6962871 0.6546045
# TASK ////////////////////////////////////////////////////////////////////////
# Perform the calculation as described above.
# As long as you can deliver the message clearly, you can use any format/package you want.
# //TASK //////////////////////////////////////////////////////////////////////
You need to calculate the mean of the proportion of building, sky, tree, road, and sidewalk for walkable and unwalkable areas. For example, you need to calculate the mean of building category for each of walkable and unwalkable Census Tracts. Then, you need to calculate the mean of sky category for each of walkable and unwalkable Census Tracts. In total, you will have 10 mean values. After the calculation, provide a brief description of your findings.
# TASK ////////////////////////////////////////////////////////////////////////
# Create boxplot(s) using geom_boxplot() function from ggplot2 package.
# You may find the code from mini-assignment 4 useful here.
# //TASK //////////////////////////////////////////////////////////////////////
pspnet_longer <- pspnet_nodes %>%
pivot_longer(
cols = pct_building:pct_sidewalk,
names_to = "Image_Elements",
values_to = "Percentages"
)
ggplot(data = pspnet_longer %>%
separate(col = "Image_Elements", into=c("Image_Elements"),sep=", ") %>%
drop_na(Image_Elements)
) +
geom_boxplot(mapping = aes(x = is_walkable, y = Percentages), color="black", outlier.size = 0.5, lwd=0.25) +
labs(x = "",
y = "Percentage",
title = "Boxplots Comparing Image Elements")+
facet_wrap(~Image_Elements, scales = "free_y",
labeller = labeller(Image_Elements =
c("pct_building" = "Building",
"pct_sky" = "Sky",
"pct_tree" = "Trees",
"pct_road" = "Roads",
"pct_sidewalk" = "Sidewalks")))
Conclusion: It is very clear with the box plot that both the areas have similardistribution of factors throughout the area. The sidewalks has the least amount in comparison to other Factors and on the other hand, sky covers the largest part of the images.