#Assignment 9

#Set up

library(tmap)
## Warning: package 'tmap' was built under R version 4.0.4
library(tmaptools)
## Warning: package 'tmaptools' was built under R version 4.0.4
library(rgdal)
## Warning: package 'rgdal' was built under R version 4.0.5
## Loading required package: sp
## Warning: package 'sp' was built under R version 4.0.4
## rgdal: version: 1.5-23, (SVN revision 1121)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.2.1, released 2020/12/29
## Path to GDAL shared files: C:/Users/jmhp2/OneDrive/Documents/R/win-library/4.0/rgdal/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
## Path to PROJ shared files: C:/Users/jmhp2/OneDrive/Documents/R/win-library/4.0/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.4-5
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading rgdal.
## Overwritten PROJ_LIB was C:/Users/jmhp2/OneDrive/Documents/R/win-library/4.0/rgdal/proj
library(sf)
## Warning: package 'sf' was built under R version 4.0.4
## Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.0.4
## 
## 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
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.0.4
library(tidyr)

#1

setwd("C:/Users/jmhp2/Downloads/Vector/Vector")
spring <- st_read("springs.shp")
## Reading layer `springs' from data source `C:\Users\jmhp2\Downloads\Vector\Vector\springs.shp' using driver `ESRI Shapefile'
## Simple feature collection with 249 features and 8 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: 478454.2 ymin: 4121569 xmax: 780096.7 ymax: 4393623
## Projected CRS: NAD83 / UTM zone 17N
geol <- st_read("geology.shp")
## Reading layer `geology' from data source `C:\Users\jmhp2\Downloads\Vector\Vector\geology.shp' using driver `ESRI Shapefile'
## Simple feature collection with 5175 features and 16 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 355916.6 ymin: 4117439 xmax: 782816.9 ymax: 4498751
## Projected CRS: NAD83 / UTM zone 17N
inter <- st_read("interstates.shp")
## Reading layer `Interstates' from data source `C:\Users\jmhp2\Downloads\Vector\Vector\Interstates.shp' using driver `ESRI Shapefile'
## Simple feature collection with 47 features and 13 fields
## Geometry type: LINESTRING
## Dimension:     XY
## Bounding box:  xmin: 360769.7 ymin: 4125421 xmax: 772219.9 ymax: 4438517
## Projected CRS: NAD83 / UTM zone 17N
rivers <- st_read("major_rivers.shp")
## Reading layer `major_rivers' from data source `C:\Users\jmhp2\Downloads\Vector\Vector\major_rivers.shp' using driver `ESRI Shapefile'
## Simple feature collection with 30 features and 2 fields
## Geometry type: MULTILINESTRING
## Dimension:     XY
## Bounding box:  xmin: 355922.1 ymin: 4125492 xmax: 783928 ymax: 4499700
## Projected CRS: NAD83 / UTM zone 17N
towns <- st_read("towns.shp")
## Reading layer `towns' from data source `C:\Users\jmhp2\Downloads\Vector\Vector\towns.shp' using driver `ESRI Shapefile'
## Simple feature collection with 277 features and 18 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: 360236 ymin: 4123803 xmax: 780854.4 ymax: 4496379
## Projected CRS: NAD83 / UTM zone 17N
counties <- st_read("counties.shp")
## Reading layer `counties' from data source `C:\Users\jmhp2\Downloads\Vector\Vector\counties.shp' using driver `ESRI Shapefile'
## Simple feature collection with 55 features and 53 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 355937 ymin: 4117435 xmax: 782843.9 ymax: 4498773
## Projected CRS: NAD83 / UTM zone 17N

#2

tm_shape(counties)+
tm_polygons(col="POP2000", title="Population in 2000", style="pretty", palette=get_brewer_pal(palette="Greens", n=5, plot=FALSE))+
tm_shape(towns)+
tm_bubbles(size="POPULATION", title.size = "Population of Towns", col = "black")+
tm_shape(inter)+
tm_lines(col= "red", lwd= 2)+
tm_shape(rivers)+
tm_lines(col= "blue", lwd= 1.5)+
tm_compass(position = c("right", "bottom"))+
tm_scale_bar(position = c("right", "bottom"))+
tm_layout(main.title = "Visualized Data.",title.size = 2.0, legend.outside=TRUE)

#3

Answer: 40.79%

pop_per1 <- as.data.frame(towns %>% select(TOWN_, POPULATION))
pop_per2 <- pop_per1 %>% select(POPULATION, TOWN_) %>% filter(POPULATION > 1500)
count <- nrow(pop_per2) / nrow(pop_per1)*100

#4

Answer: 5

t_true <- as.data.frame(towns %>% select(TOWN_, TYPE))
t_true <- t_true %>% select(TYPE, TOWN_) %>% filter(TYPE == "VILLAGE")

#5

sample_n(towns, 25)
## Simple feature collection with 25 features and 18 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: 400126.6 ymin: 4141354 xmax: 770813.7 ymax: 4482890
## Projected CRS: NAD83 / UTM zone 17N
## First 10 features:
##    AREA PERIMETER TOWN_ TOWN_ID TOWNS_ TOWNS_ID PLACEFP TYPE  INTPTLAT
## 1     0         0   176     176    176      176   58372 CITY +40495984
## 2     0         0   104     104    104      104   32716 CITY +39340566
## 3     0         0   180     180    180      180   59068 CITY +38417632
## 4     0         0     9       9      9        9   03292 TOWN +37422810
## 5     0         0   261     261    261      261   85228 CITY +37432818
## 6     0         0    76      76     76       76   24292 TOWN +38538949
## 7     0         0   117     117    117      117   36436 TOWN +38831459
## 8     0         0   136     136    136      136   44044 CITY +39471053
## 9     0         0   141     141    141      141   48148 CITY +37849110
## 10    0         0    61      61     61       61   18035 TOWN +39299480
##      INTPTLNG      LAT      LONG                  NAME POPULATION FAMILIES
## 1  -080607149 40.49598 -80.60715        NEW CUMBERLAND       1363      353
## 2  -080017153 39.34057 -80.01715               GRAFTON       5524     1486
## 3  -081831112 38.41763 -81.83111                 NITRO       6851     2009
## 4  -081016063 37.42281 -81.01606                ATHENS        741      211
## 5  -081581503 37.43282 -81.58150                 WELCH       3028      809
## 6  -081931189 38.53895 -81.93119               ELEANOR       1256      375
## 7  -082135693 38.83146 -82.13569             HENDERSON        549      150
## 8  -079683065 39.47105 -79.68306              KINGWOOD       3243      888
## 9  -081986858 37.84911 -81.98686                 LOGAN       2206      592
## 10 -077859830 39.29948 -77.85983 CORPORATION OF RANSON       2890      785
##    HOUSEHOLDS  X_COORD Y_COORD                 geometry
## 1         574 533290.8 4482672 POINT (533307.3 4482890)
## 2        2225 584700.1 4354823 POINT (584715.1 4355042)
## 3        2881 427443.6 4252275 POINT (427458.4 4252491)
## 4         342 498579.0 4141574 POINT (498595.8 4141791)
## 5        1298 448553.2 4142843 POINT (448569.7 4143059)
## 6         484 418843.1 4265819 POINT (418857.6 4266036)
## 7         217 401421.3 4298479 POINT (401435.5 4298695)
## 8        1282 613280.4 4369671 POINT (613296.5 4369890)
## 9         953 413176.3 4189327 POINT (413192.1 4189544)
## 10       1075 770796.7 4354505 POINT (770813.7 4354726)

#6

##12

towns <- towns %>% group_by(TYPE) %>% sample_n(3, replace=FALSE)

#7

##30.52%

stm_per1 <- as.data.frame(spring %>% select(SPRINGS_ID, AVEFLOW))
stm_per2 <- stm_per1 %>% select(SPRINGS_ID, AVEFLOW) %>% filter(AVEFLOW > 700)
count <- nrow(stm_per2) / nrow(stm_per1)*100

#8

##4.41

ter_per1 <- as.data.frame(spring %>% select(SPRINGS_ID, TOPO_POS))
ter_per2 <- ter_per1 %>% select(TOPO_POS, SPRINGS_ID) %>% filter(TOPO_POS == "TERRACE")
count1 <- nrow(ter_per2) / nrow(ter_per1)*100

#9

I79 <- as.data.frame(inter %>% select(SIGN1, KM) %>% filter(SIGN1 == "i79"))

#10

##317.4 km

i79_diss <- I79 %>% group_by(KM) %>%summarize(sum = sum(KM))

#11

q11 <- st_join(inter, towns)
q11_1 <- as.data.frame(q11 %>% select(SIGN1, TOWN_ID) %>% filter(SIGN1 == "I79"))
#inter_town_10km <- st_buffer(q11_1, 10000)
#? 

#12

#tm_shape(counties) +
#tm_polygons(col="tan")+
#tm_shape()+
#tm_polygons(col="red", alpha=.6)+
#tm_shape(i79)+
#tm_lines(lwd=1, col="black")
#Need 11 to complete

#13

##59.84%

s_r <- st_join(spring, geol)
s_r_df1 <- as.data.frame(s_r %>% select(SPRINGS_ID, TYPE))
s_r_df2 <- s_r_df1 %>% select(TYPE, SPRINGS_ID) %>% filter(TYPE == "LIMESTONE")
count3 <- nrow(s_r_df2) / nrow(s_r_df1)*100

#14 ##Limestone 149

s_r_df <- as.data.frame(s_r %>% select(SPRINGS_ID, TYPE))
a <- s_r_df %>% group_by(TYPE) %>% count()

#15

counties_srswor <- counties %>% group_by(NAME) %>% sample_n(5, replace=T)
check_sample <- counties_srswor %>% group_by(NAME) %>% count()
print(check_sample)
## Simple feature collection with 55 features and 2 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 355937 ymin: 4117435 xmax: 782843.9 ymax: 4498773
## Projected CRS: NAD83 / UTM zone 17N
## # A tibble: 55 x 3
##    NAME         n                                                       geometry
##  * <chr>    <int>                                                  <POLYGON [m]>
##  1 Barbour      5 ((571943.8 4343990, 572007.3 4343972, 572113.1 4343939, 57218~
##  2 Berkeley     5 ((749219.5 4385551, 749249.5 4385535, 749301.6 4385513, 74934~
##  3 Boone        5 ((427095 4229428, 427102.6 4229419, 427108.1 4229396, 427099.~
##  4 Braxton      5 ((534180.6 4306216, 534240.4 4306152, 534396.4 4305964, 53456~
##  5 Brooke       5 ((540815 4472242, 540813.4 4472098, 540823.2 4471744, 540819 ~
##  6 Cabell       5 ((393840.2 4272164, 393861.5 4272146, 393957.4 4272055, 39406~
##  7 Calhoun      5 ((495520.2 4317835, 495543.4 4317829, 495579.1 4317825, 49559~
##  8 Clay         5 ((492726.6 4273733, 492765 4273783, 492827.1 4273872, 492872.~
##  9 Doddrid~     5 ((539208.9 4364486, 539206.8 4364483, 539191.6 4364463, 53917~
## 10 Fayette      5 ((479754.3 4235076, 479773.8 4235053, 479801.4 4235016, 47981~
## # ... with 45 more rows

#16

tm_shape(counties)+
tm_polygons(col="beige")+
tm_shape(check_sample)+
tm_bubbles(col="NAME", size = .1)+
tm_layout(legend.outside=TRUE)
## Warning: Number of levels of the variable "NAME" is 55, which is
## larger than max.categories (which is 30), so levels are combined. Set
## tmap_options(max.categories = 55) in the layer function to show all levels.

#17

#unsure how to code
#r_c <- st_join(rivers, counties)
#sub_regions = states %>% group_by(SUB_REGION) %>%
#summarize(totpop = sum(POP2000))
#names(harvest)[2] <- "NAME"
#counties_harvest <- inner_join(wv_counties, harvest, by="NAME")
#s_r <- st_join(spring, geol)
#s_r_df <- as.data.frame(s_r %>% select(SPRINGS_ID, TYPE))
#c <- s_r_df %>% group_by(TYPE) %>% count()
#wv_diss <- wv_counties %>% group_by(STATE) %>%
#summarize(totpop = sum(POP2000))
#tm_shape(wv_diss)+
#tm_polygons(col="tan")

#18

#19

#names(harvest)[2] <- "NAME"
#counties_harvest <- inner_join(wv_counties, harvest, by="NAME")
#city <- st_join(us_cities, states)

#20