T2

tm_shape(county) + 
tm_polygons(col = "#e5d3b3") + 
tm_shape(rivers) + 
tm_lines(col = "blue", lwd = 0.75) + 
tm_shape(interstate) + 
tm_lines(col = "black", lwd =2) + 
tm_shape(towns) +
tm_bubbles(col = "red", size = 0.1) + 
tm_compass(position = c("right", 0.15)) + 
tm_scale_bar(position = c("right", 0.02)) + 
tm_layout(inner.margins = c(0.1, 0.05 ,0.05, 0.05), main.title = "West Virginia (Towns, Interstates, Rivers)", main.title.size = 1.1, main.title.position = "center")

T3

t3 <- towns %>%
  filter(POPULATION > 1500)

per15 <- (nrow(t3) / nrow(towns)) * 100
per15
[1] 40.79422

# 40.8% of towns have a population greater than 1500

T4

t4 <- towns %>%
  filter(TYPE == "VILLAGE") 
nrow(t4)
[1] 5

# There are 5 towns classified as a village

T5

t5 <- sample_n(towns, 25)

T6

t6 <- towns %>%
  group_by(TYPE) %>%
  sample_n(3)

T7

t7 <- spring %>%
  filter(AVEFLOW > 700) 

per_springs <- (nrow(t7) / nrow(spring)) * 100
per_springs
[1] 30.52209

# 30.5% of springs have an average flow greater than 700

T8

t8 <- spring %>%
  filter(TOPO_POS == "TERRACE") 

per_terrace <- (nrow(t8) / nrow(spring)) * 100
per_terrace
[1] 4.417671

# 4.4% of springs occur on terraces

T9

i79 <- filter(interstate, SIGN1 == "I79")
tm_shape(county) + 
tm_polygons(col = "#e5d3b3") + 
tm_shape(i79) + 
tm_lines(col = "black", lwd = 3) + 
tm_layout(inner.margins = c(0.08, 0.05 ,0.075, 0.075), main.title = "I-79", main.title.position = "center") +
tm_compass(position = c(0.75, 0.125)) + 
tm_scale_bar(position = c(0.5, 0.02), size = .6) 

T10

i79_diss <- i79 %>%
  group_by(SIGN1) %>%
  summarize(dist = sum(KM))
i79_diss$dist
[1] 414.76
# I-79 has a length of 414.76 km

T11

i79_10km <- st_buffer(i79, 10000)
i79_towns <- st_intersection(towns, i79_10km) %>%
  group_by(NAME) %>%
  count()
nrow(i79_towns)
[1] 36

# There are 36 towns within 10km of I-79

T12

tm_shape(county) + 
tm_polygons(col = "#e5d3b3") + 
tm_shape(i79_10km) + 
tm_polygons(col = "black") + 
tm_shape(i79_towns) + 
tm_bubbles(col = "red", size = 0.3) + 
tm_layout(inner.margins = c(0.075, 0.05 ,0.075, 0.07), main.title = "I-79 and Towns within a 10km Buffer", main.title.size = 1.2,  main.title.position = "center") +
tm_compass(position = c(0.75, 0.125)) + 
tm_scale_bar(position = c(0.5, 0.02), size = .6) 

T13

limestone <- geol %>%
  filter(TYPE == "limestone")

springs_lime <- st_intersection(spring, limestone) %>%
  group_by(SPRING_NAM) %>%
  count()

t13 <- (nrow(springs_lime) / nrow(spring)) * 100
t13
[1] 54.21687

# 54.2% of all springs occur in limestone 

T14

t14 <- st_join(spring, geol) %>%
  group_by(TYPE) %>%
  count(name = "No.ofSprings")
t14
Simple feature collection with 8 features and 2 fields
Geometry type: GEOMETRY
Dimension:     XY
Bounding box:  xmin: 478454.2 ymin: 4121569 xmax: 780096.7 ymax: 4393623
Projected CRS: NAD83 / UTM zone 17N
# A tibble: 8 x 3
  TYPE     No.ofSprings                                                 geometry
* <chr>           <int>                                           <GEOMETRY [m]>
1 alluvium            2      MULTIPOINT ((663609.4 4317565), (719261.5 4348041))
2 limesto~          149 MULTIPOINT ((483765 4124787), (503951.5 4130175), (5071~
3 phyllite            2      MULTIPOINT ((774753.2 4343218), (780096.7 4355079))
4 sandsto~           44 MULTIPOINT ((546160.2 4153412), (562242.7 4184950), (56~
5 shale              40 MULTIPOINT ((486719 4126025), (490541.2 4127251), (4932~
6 shale/ss            1                                 POINT (779709.8 4364390)
7 ss/ls               9 MULTIPOINT ((586375.8 4227092), (586497.1 4227249), (58~
8 <NA>                2      MULTIPOINT ((478454.2 4121726), (479075.1 4121569))
# Limestone has the largest number of springs with 149.

T15

rnd_sam <- county %>% 
  group_by(NAME) %>% 
  st_sample(rep(5, 55), type="random")

T16

tm_shape(county) + 
tm_polygons(col = "lightgray") + 
tm_shape(rnd_sam) + 
tm_bubbles(col = "red", size = 0.075) + 
tm_layout(inner.margins = c(0.08, 0.05 ,0.075, 0.07), main.title = "Random Points per County", main.title.position = "center", main.title.size = 1.1) +
tm_compass(position = c(0.75, 0.125)) + 
tm_scale_bar(position = c(0.5, 0.02), size = .6) 

T17

t17 <- st_intersection(rivers, county) 
t17$Shape_Leng <- st_length(t17)
county_ln <- t17 %>%
  group_by(NAME) %>%
  summarise(total_len = sum(Shape_Leng))

# Randolph County has the longest length of major rivers 

T18

county_ln_d <- st_drop_geometry(county_ln)
county_ln_join <- left_join(county, county_ln_d, by = "NAME")

tm_shape(county_ln_join) + 
tm_polygons(col = "total_len", title = "Length", palette = c("Purples"),) + 
tm_layout(inner.margins = c(0.15, 0.05 ,0.075, 0.2), main.title = "Length of Major Rivers by County", main.title.size = 1.25, main.title.position = "center") + 
tm_compass(position = c(0.55, 0.1), size = 2) + 
tm_scale_bar(position = c(0.2, 0.02), size = .6) 

T19

towns_den <- st_intersection(county, towns) %>%
  group_by(NAME) %>% 
  count()

towns_county <- st_join(county, towns_den) %>%
  mutate(density_t = n / SQUARE_MIL) 

## Ohio County has the largest density of towns 

T20

tm_shape(towns_county) + 
tm_polygons(col = "density_t", palette = c("Greens"), title = "Town Density (sq. mi.)", title.size = 1, style = "quantile") +
tm_layout(inner.margins = c(0.2, 0.085 ,0.08, 0.08), main.title = "Town Density by County (per square mile)", main.title.size = 1.15, main.title.position = "center") + 
tm_compass(position = c(0.01, 0.1), size = 2) + 
tm_scale_bar(position = c(0.15, 0.02), size = .6)