library(mapview)
library(sf)
## Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
library(censusxy)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

Create point layer from data from AtoZdatabase

bars<-read.csv("C:/Users/drayr/OneDrive/Desktop/DEM Spring 2022/DEM 7093/HW 6/bars.csv")

bars <- bars%>%
  dplyr::select(names(bars)[c(4,6,14,23:24,41,42)])

results <- st_as_sf(bars, coords=c("Longitude", "Latitude"), crs=4269,agr="constant")

results.proj<-st_transform(results,
                           crs = 2278)
mv1 <- mapview(results.proj)
mapshot(mv1, file = paste0(getwd(), "/map1.png"))
## PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
mv1
library(tmap)
library(tmaptools)
library(ggplot2)
library(ggmap)
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(ggspatial)

mean feature - average of coordinates

mean_feature<-apply(st_coordinates(results.proj), MARGIN = 2, FUN = mean)
mean_feature<-data.frame(place="meanfeature", x=mean_feature[1], y= mean_feature[2])
mean_feature<-st_as_sf(mean_feature, coords = c("x", "y"), crs= 2278)


tmap_mode("view")
## tmap mode set to interactive viewing
tm_basemap("OpenStreetMap.Mapnik")+
  tm_shape(results.proj, size = .2)+
  tm_dots(col = "green")+
  tm_shape(mean_feature)+
  tm_dots(col = "red", size = .2)

Convex hull plot

chull <- st_convex_hull(st_union(results))

tmap_mode("view")
## tmap mode set to interactive viewing
tm_basemap("OpenStreetMap.Mapnik")+
  tm_shape(results.proj)+
  tm_dots(col = "green")+
  tm_shape(chull)+
  tm_polygons(col = "grey", alpha = .5)

Spatial join

library(tidycensus)

#load census tract data
sa_acs<-get_acs(geography = "tract",
                state="TX",
                county = "Bexar", 
                year = 2019,
                variables=c( "DP05_0001E", "DP03_0062E",
                             "DP03_0119PE","DP02_0066PE",
                             "DP03_0062E","DP03_0119PE",
                             "DP05_0073PE","DP05_0066PE") ,
                geometry = T, output = "wide")
## Getting data from the 2015-2019 5-year ACS
## Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
## Using the ACS Data Profile
#rename variables and filter missing cases
sa_acs2<-sa_acs%>%
  mutate(totpop= DP05_0001E,
         pblack=DP05_0073PE,
         phisp=DP05_0066PE,
         phsormore=DP02_0066PE,
         medhhinc=DP03_0062E,
         ppov=DP03_0119PE)%>%
  dplyr::select(GEOID, totpop, pblack, phisp, medhhinc, ppov)

sa_acs2<-st_transform(sa_acs2, crs = 2278)
sa_trol<-st_cast(sa_acs2, "MULTILINESTRING") #make outline
spjoin<-st_join(results.proj, sa_acs2)
#head(spjoin)

Count points in polygons

sa_acs2$nbars<- lengths(st_intersects(sa_acs2, results.proj))

sa_acs2$bars_pc <- 1000*(sa_acs2$nbars/sa_acs2$totpop)

tmap_mode("plot")
## tmap mode set to plotting
m<-tm_basemap("OpenStreetMap.Mapnik")+
  tm_shape(sa_acs2)+
  tm_polygons("bars_pc")+
  tm_shape(spjoin, is.master = T)+
  tm_dots( size = .01)

m

Nearest Neighbor analysis

Nearest neighbor analysis is used to test if a pattern of points is distrubuted randomly or clustered.

library(spatstat)
bar.pp<-as.ppp(as(results.proj, "Spatial"))

plot(nearest.neighbour(bar.pp))