library(terra)
## Warning: package 'terra' was built under R version 4.3.3
## terra 1.7.71
library(tidyterra)
## Warning: package 'tidyterra' was built under R version 4.3.3
##
## Attaching package: 'tidyterra'
## The following object is masked from 'package:stats':
##
## filter
library(sp)
## Warning: package 'sp' was built under R version 4.3.2
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.2
library(ggthemes)
## Warning: package 'ggthemes' was built under R version 4.3.2
library(urbnmapr)
library(sf)
## Warning: package 'sf' was built under R version 4.3.2
## Linking to GEOS 3.11.2, GDAL 3.7.2, PROJ 9.3.0; sf_use_s2() is TRUE
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.3.3
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:terra':
##
## intersect, union
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(spData)
## Warning: package 'spData' was built under R version 4.3.3
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source')`
usa_rast <- rast("NE1_50M_SR_W.tif")
usa_rast <- terra::project(usa_rast, "EPSG:32116", method = "bilinear")
##
|---------|---------|---------|---------|
=========================================
plot(usa_rast)
states <- get_urbn_map("states", sf = TRUE)
states %>%
st_transform("EPSG:32116")
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## Warning in CPL_crs_from_input(x): GDAL Message 1: CRS EPSG:2163 is deprecated.
## Its non-deprecated replacement EPSG:9311 will be used instead. To use the
## original CRS, set the OSR_USE_NON_DEPRECATED configuration option to NO.
plot (states)
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
crs_rast <- crs(usa_rast)
crs_states <- st_crs(states)
## old-style crs object detected; please recreate object with a recent sf::st_crs()
US_map <- crop(usa_rast, states)
plot(US_map)
question 2 leaflet
counties <- get_urbn_map("counties", sf = TRUE)
counties <- counties %>%
filter(state_abbv == "NY") %>% #No duplicate counties from other states
st_transform("EPSG:32116")
## old-style crs object detected; please recreate object with a recent sf::st_crs()
soybeans <- st_read("N-7.xls")
## Multiple layers are present in data source C:\Users\jules\Downloads\Spring 24\DIDA 370\Assignment 3\N-7.xls, reading layer `Planted'.
## Use `st_layers' to list all layer names and their type in a data source.
## Set the `layer' argument in `st_read' to read a particular layer.
## Warning in CPL_read_ogr(dsn, layer, query, as.character(options), quiet, :
## automatically selected the first layer in a data source containing more than
## one.
## Reading layer `Planted' from data source
## `C:\Users\jules\Downloads\Spring 24\DIDA 370\Assignment 3\N-7.xls'
## using driver `XLS'
## Warning: no simple feature geometries present: returning a data.frame or tbl_df
soybeans_years <- soybeans %>%
select (Field1, Field2, Field21)
colnames(soybeans_years) <- c("county_name","sb_2019","sb_2000")
#Only include counties in data
soybeans_years = soybeans_years[8:64,]
#misc values set to 0
soybeans_years[soybeans_years == "a"] <- 0
soybeans_years[soybeans_years == "b"] <- 0
make county names identical
soybeans_years$county_name <- gsub(" ", "", soybeans_years$county_name)
counties$county_name <- gsub(" County", "", counties$county_name)
counties$county_name <- gsub(" ", "", counties$county_name)
merge data
county_and_sb_data <- counties %>%
left_join(soybeans_years, by = c("county_name" = "county_name"))
plot(county_and_sb_data)
all_county_data.sf = st_transform(county_and_sb_data, "EPSG:4326")
all_county_data.sf$sb_2000 <- as.numeric(all_county_data.sf$sb_2000)
all_county_data.sf$sb_2019 <- as.numeric(all_county_data.sf$sb_2019)
pal_fun <- colorBin("YlOrRd", NULL, n = 5)
pal_fun2 <- colorBin("Blues", NULL, n = 5)
leaflet() %>%
addPolygons(data = all_county_data.sf,
stroke = FALSE,
fillColor = ~pal_fun(sb_2019),
fillOpacity = 0.8,
weight = 1,
popup = ~paste("County:", all_county_data.sf$county_name, "<br>", "2019 Data:", all_county_data.sf$sb_2019),
group = "Soybean Production in 2019") %>%
addPolygons(data = all_county_data.sf,
stroke = FALSE,
fillColor = ~pal_fun2(sb_2000),
fillOpacity = 1,
smoothFactor = 0.5,
popup = ~paste("County:", all_county_data.sf$county_name, "<br>", "2000 Data:", all_county_data.sf$sb_2000),
group = "Soybean Production in 2000") %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addLayersControl(overlayGroups = c("Soybean Production in 2019", "Soybean Production in 2000"),
options = layersControlOptions(collapsed = FALSE))
Citations: https://epirhandbook.com/en/cleaning-data-and-core-functions.html Leaflet practice in class https://www.geeksforgeeks.org/data-cleaning-in-r/ I took MATH 455 and have had to clean data in R in that class