Skip to content

Instantly share code, notes, and snippets.

@ozjimbob
Last active April 19, 2020 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozjimbob/af618bbeba3ce9df42dbfbcbd56c30cc to your computer and use it in GitHub Desktop.
Save ozjimbob/af618bbeba3ce9df42dbfbcbd56c30cc to your computer and use it in GitHub Desktop.
# If you don't have them installed, install ggmap and ggplot2 packages
install.packages("ggmap")
install.packages("ggplot2")
# Load these packages
library(ggmap)
library(ggplot2)
# Make a table with your point data
# You could also load this from a spreadsheed using the read.csv() function
# This has a column for the label, a column for latitude, and a column for longitude
ptdata <- data.frame("PlaceName" = c(" Hacking R. ", " Cabbage Tree Ck. ",
" Marley Ck. ", " Wattamolla Ck. ",
" Waratah R. "),
"Long" = c(151.054823, 151.124566, 151.139216,
151.115645, 150.960431),
"Lat" = c(-34.074918, -34.090796, -34.111972,
-34.136046, -34.172308))
# Define the bounds of your map - do this automatically by taking the maximum and minimum
# of the point lat/longs, and adding a little bit (0.05 degrees which is about 5km) to them
Pub_bound <- c(left = min(ptdata$Long) - 0.05, bottom = min(ptdata$Lat) - 0.05,
right = max(ptdata$Long) + 0.05 , top = max(ptdata$Lat) + 0.05)
# Get the basemap we want to use - in this case a plain black and white one called "toner-lite"
# Note you may want to change the "zoom" setting to large or smaller depending on your area of interest
Pub_basemap <- get_stamenmap(Pub_bound, zoom = 13, maptype = "toner-lite")
# Make the map:
# Use the basemap
# Then add your points, using lat/long, and with the labels from "PlaceName" column
# Then set the black and white graphing theme
# And remove the axis titles (which would normally say Lat / Long)
map = ggmap(Pub_basemap) +
geom_point(data = ptdata, aes(x=Long, y=Lat)) +
geom_text(data = ptdata,
aes(x=Long, y=Lat, label = PlaceName),
size = 4,
fontface = "bold",
hjust = "right") +
theme_bw() +
theme(axis.title = element_blank())
# Display the map
print(map)
# As a bonus - save this map to disk with an exact size, resolutione tc.
ggsave("mymap.png",map,width=20,height=20,units="cm" ,dpi=150)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment