Skip to content

Instantly share code, notes, and snippets.

@bradlindblad
Created October 8, 2019 01:02
Show Gist options
  • Save bradlindblad/80a061bdde4de4168d1011a8da4a2bda to your computer and use it in GitHub Desktop.
Save bradlindblad/80a061bdde4de4168d1011a8da4a2bda to your computer and use it in GitHub Desktop.
Take simple lat lon pairs and plot them in tidyUSDA and Leaflet
library(sf)
library(tidyverse)
library(tidyUSDA)
library(leaflet)
# Make some random lon lat coord pairs ------------------------------------
# Get random points to sample from
sample.lon <- seq(-97, -96, 0.01)
sample.lat <- seq(44, 45, 0.01)
sample.val <- seq(0, 100, 1)
# Sample random points
lon <- sample(sample.lon, size = 40, replace = F)
lat <- sample(sample.lat, size = 40, replace = F)
Value <- sample(sample.val, size = 40, replace = F)
# Make that into a tibble
uniform.points <- tibble(lon = lon,
lat = lat,
id = seq_along(lon),
Value = Value)
# Convert that to simple features, "sf"
now.as.sf <- sf::st_as_sf(x = uniform.points,
coords = c("lon", "lat"))
class(now.as.sf)
glimpse(now.as.sf)
# Plot generic way
plot(now.as.sf$geometry)
# Plot with tidyUSDA
tidyUSDA::plotUSDA(now.as.sf) # since we're using points, not polygons, tidyUSDA can plot it, but it's probs
# not the best wa to do it
# Plot with Leaflet -------------------------------------------------------
leaflet() %>%
addTiles() %>%
addCircleMarkers(data = now.as.sf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment