For the case study we will be looking at Philidelphia Neighborhood Shootings. We will be getting this data in the form of a geojson file.
phillyNeighborShooting <- na.omit(st_read("https://pengdsci.github.io/STA553VIZ/w08/PhillyShootings.geojson"))
phillyNeighbor <- st_read("https://pengdsci.github.io/STA553VIZ/w08/Neighborhoods_Philadelphia.geojson")
philly <- st_read("https://pengdsci.github.io/STA553VIZ/w08/PhillyNeighborhood-blocks.geojson") # block level data
SubCrime = phillyNeighborShooting[,c("dc_key","fatal","location","year")]
aggregateCrime = aggregate(SubCrime$fatal, by=list(SubCrime$location,SubCrime$year), FUN=length)
## Longitude and latitude of zip code center
ZipLon = aggregate(as.numeric(phillyNeighborShooting$point_x), by=list(phillyNeighborShooting$location), FUN=mean)
ZipLat = aggregate(as.numeric(phillyNeighborShooting$point_y), by=list(phillyNeighborShooting$location), FUN=mean)
ZipLonLat = merge(ZipLon, ZipLat, by = "Group.1")
names(ZipLonLat) = c("location", "lng", "lat")
## Zip code crime
ZipCrimeTable = table(phillyNeighborShooting$fatal, phillyNeighborShooting$location)
ZipCrime = data.frame(location=names(table(phillyNeighborShooting$location)),
fatal = table(phillyNeighborShooting$fatal, phillyNeighborShooting$location)[1,],
nonfatal = table(phillyNeighborShooting$fatal, phillyNeighborShooting$location)[2,],
total.crime = table(phillyNeighborShooting$location)
)
ZipCrime = ZipCrime[, c("location", "fatal", "nonfatal", "total.crime.Freq")]
colnames(ZipCrime) = c("location", "fatal", "nonfatal", "total.crime")
ZipCrime = merge(ZipLonLat, ZipCrime, by = "location")
plot_ly(
data = phillyNeighborShooting,
x = ~ tolower(wound),
type = "histogram",
nbinsx = 10,
name = "Area Wounded",
alpha = .5,
marker = list(line = list(color = "darkgray", width = 2)) ) %>%
## Adding additional histograms and stacking them
add_histogram(x = ~tolower(wound),
name = "Area Wounded", nbinsx = 10, alpha = 0.5,
marker = list(line = list(color = "darkgray", width = 2))) %>%
layout(barmode = "overlay",
title = "Histogram of Wounded Areas",
xaxis = list(title = "Area Wounded",
zeroline = TRUE),
yaxis = list(title = "Count",
zeroline =TRUE),
## margin of the plot
margin = list(
b = 50,
l = 100,
t = 120,
r = 50
))
barplotdata = aggregate(phillyNeighborShooting[,21], by = list(phillyNeighborShooting$race), FUN = sum)
plot_ly(
data = barplotdata,
x = ~Group.1,
y = ~fatal,
type = "bar",
name = "fatal",
## graphic size
width = 700,
height = 400) %>%
layout( yaxis = list(title ="Number of Fatalities"),
xaxis = list(title = "Race"),
title = "Fatalaty Shootings by Race"
)
# define three densities
male.which <- phillyNeighborShooting[which(phillyNeighborShooting$sex == "M"),]
male <- density(male.which$fatal)
female.which <- phillyNeighborShooting[which(phillyNeighborShooting$sex == "F"),]
female <- density(female.which$fatal)
# plot density curves
fig <- plot_ly(x = ~male$x,
y = ~male$y,
type = 'scatter', #A character string specifying the trace type
mode = 'lines',
name = 'males',
fill = 'tozeroy') %>%
# adding more density curves
add_trace(x = ~female$x,
y = ~female$y,
name = 'female',
fill = 'tozeroy') %>%
layout(xaxis = list(title = 'Sex'),
yaxis = list(title = 'Density'))
fig
## Images to be plot on the map
fatalitybyrace = "https://dannyoc99.github.io/DOCAL/img/FatalitybyRace.png"
SexFatality = "https://dannyoc99.github.io/DOCAL/img/SexFatality.png"
WoundHistogram = "https://dannyoc99.github.io/DOCAL/img/WoundHistogram.png"
fata = st_as_sf(data.frame(x = -75.3877, y = 39.9168),
coords = c("x", "y"),
crs = 4326)
SexFatalityloc = st_as_sf(data.frame(x = -75.3677, y = 39.9168),
coords = c("x", "y"),
crs = 4326)
WoundHistogramloc = st_as_sf(data.frame(x = -75.3477, y = 39.9168),
coords = c("x", "y"),
crs = 4326)
##################################################
## Crime distribution by zip code - bubble plot
pal0 <- colorNumeric(palette = viridis(256, option = "B"), domain = range(ZipCrime$total.crime))
zipfig <- leaflet() %>%
setView(lng=-75.1527, lat=39.9707, zoom = 11) %>%
addProviderTiles(providers$Esri.WorldGrayCanvas) %>%
## plot the neighborhood boundary of Philly
addPolygons(data = phillyNeighbor,
color = 'skyblue',
weight = 1) %>%
##
addCircleMarkers(data = ZipCrime,
radius = ~((total.crime)^(1/3)),
stroke = FALSE,
fillOpacity = 0.5,
popup = ~paste('Location:', location,
'<br>Total Crime:', total.crime,
'<br>Fatal Crime:', fatal,
'<br>Nonfatal Crime:', nonfatal))
##################################################
## popup interactive graphs with plotly
fl = tempfile(fileext = ".html")
saveWidget(zipfig, file = fl)
###
leaflet() %>%
setView(lng=-75.15092, lat=40.00995, zoom = 11) %>%
#addProviderTiles(providers$CartoDB.DarkMatter) %>%
##
addProviderTiles(providers$CartoDB.DarkMatter, group="Dark") %>%
addProviderTiles(providers$CartoDB.DarkMatterNoLabels, group="DarkLabel") %>%
addProviderTiles(providers$Esri.NatGeoWorldMap, group="Esri") %>%
#addTiles(providers$CartoDB.PositronNoLabels) %>%
## mini reference map
addMiniMap() %>%
## neighborhood boundary
addPolygons(data = phillyNeighbor,
color = 'skyblue',
weight = 1) %>%
## plot information on the map
addCircleMarkers(data = phillyNeighborShooting,
radius = ~ifelse(fatal == "1", 5, 3),
stroke = FALSE,
fillOpacity = 0.5,
popup = ~popupTable(phillyNeighborShooting),
clusterOptions = markerClusterOptions(maxClusterRadius = 40)) %>%
# Trend of crimes over the years
addCircleMarkers(data = fata,
color = "red",
weight = 2,
label = "fatality by race",
stroke = FALSE,
fillOpacity = 0.95,
group = "fata") %>%
addPopupImages(fatalitybyrace,
width = 500,
height = 350,
tooltip = FALSE,
group = "fata") %>%
# age distribution within the two types of crimes
addCircleMarkers(data = SexFatalityloc,
color = "black",
weight = 2,
label = "Density of Fatalities by Sex",
stroke = FALSE,
fillOpacity = 0.95,
group = "SexFatalityloc") %>%
addPopupImages(SexFatality,
width = 500,
height = 320,
tooltip = FALSE,
group = "SexFatalityloc") %>%
addCircleMarkers(data = WoundHistogramloc,
color = "blue",
weight = 2,
label = "Wound Location Histogram",
stroke = FALSE,
fillOpacity = 0.95,
group = "WoundHistogramloc") %>%
addPopupImages(WoundHistogram,
width = 500,
height = 400,
tooltip = FALSE,
group = "WoundHistogramloc") %>%
##
browsable()