Skip to content

Instantly share code, notes, and snippets.

@erdavis1
Created January 28, 2020 22:18
Show Gist options
  • Save erdavis1/afbf95a51bf6f8094d1ad25f2fa60201 to your computer and use it in GitHub Desktop.
Save erdavis1/afbf95a51bf6f8094d1ad25f2fa60201 to your computer and use it in GitHub Desktop.
basic plotting in R
#run this once to install these packages. Once they're installed, you don't need to run it again
install.packages(c('sf', 'tidyverse', 'rworldmap', 'ggthemes'))
#load up the libraries. You need to do this once per session; when you close out R they'll be unloaded
library(sf)
library(tidyverse)
library(rworldmap)
library(ggthemes)
#----get a map of ireland----
ire <- getMap(resolution = "low") %>% subset(ADMIN=='Ireland') #get the map
ire <- ire %>% st_as_sf #convert it to an sf object
ire <- ire %>% st_transform(29902) #project it to EPSG 29902
plot(ire$geometry) #plot it to see what we've got
#---make a list of points to plot---
#you'll want to look into the function "read.csv" to read in a csv of points. Here I just make up a few random ones as an example.
pts <- data.frame(lat = c(53.560734, 54.791871, 51.998677, 52.156859),
lon = c(-7.497496, -8.081254, -8.877234, -8.932504))
pts <- st_as_sf(pts, coords = c('lon', 'lat'), crs = 4326) #conver the pts into an sf object
pts <- pts %>% st_transform(29902) #project them to same projection as ireland
#---plot it all
ggplot() + theme_map() +
geom_sf(data = ire, fill = NA, color = 'black') +
geom_sf(data = pts, color = 'red')
#now let's say you want to draw 20km circles around each point
circles <- pts %>% st_buffer(20000)
ggplot() + theme_map() +
geom_sf(data = ire, fill = NA, color = 'black') +
geom_sf(data = pts, color = 'red') +
geom_sf(data = circles, fill = NA, color = 'blue')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment