Skip to content

Instantly share code, notes, and snippets.

@matt-dray
Last active November 5, 2021 10:09
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 matt-dray/b9cc649dd33a606a9f3c43998108c8eb to your computer and use it in GitHub Desktop.
Save matt-dray/b9cc649dd33a606a9f3c43998108c8eb to your computer and use it in GitHub Desktop.
Use R to locate the coordinates of places of interest on a map of Kanto
# See blogpost about this
# https://www.rostrum.blog/2021/11/04/kanto-locator/
# Function: download and plot a PNG file
plot_map <- function(png_path) {
tmp <- tempfile()
download.file(png_path, tmp, quiet = TRUE)
img <- png::readPNG(tmp)
par(mar = rep(0, 4))
plot.new()
grid::grid.raster(img, x = 0.5, y = 0.5)
}
kanto_path <-
"https://cdn2.bulbagarden.net/upload/8/86/Kanto_Town_Map_RBY.png"
plot_map(kanto_path) # plot town map of Kanto
# Function: prompt user to click for list of points on plot
locate_points <- function(places) {
places_list <- vector("list", length(places)) |> setNames(places)
for (i in places) {
cat(paste0("Click on ", i, "... "))
places_list[[i]] <- locator(1, type = "p")
cat("found.\n")
}
places_df <- do.call(rbind, places_list) |> data.frame()
}
kanto_names <- c(
"Pallet Town", "Viridian City", "Viridian Forest", "Pewter City",
"Mt Moon", "Cerulean City", "Rock Tunnel", "Vermilion City",
"Lavender Town", "Celadon City", "Fuchsia City", "Saffron City",
"Seafoam Islands", "Cinnabar Island", "Victory Road",
"Indigo Plateau"
)
kanto_pts <- locate_points(kanto_names) # interactive plot clicking
# Plot: cities are large red points, other places are small blue
# points; labels larger for cities; lines connect the cities
# in the order of playthrough.
kanto_pts$city <- ifelse(
grepl("Town|City|Island$", rownames(kanto_pts)),
TRUE, FALSE
)
par(mar = rep(0, 4))
plot(kanto_pts$x, kanto_pts$y, axes = FALSE)
lines(kanto_pts$x, kanto_pts$y, col = "grey95", lwd = 5)
points(
kanto_pts$x, kanto_pts$y,
pch = 16,
cex = ifelse(kanto_pts$city, 2, 1),
col = ifelse(kanto_pts$city, "red", "blue")
)
text(
kanto_pts$x, kanto_pts$y,
gsub(" ", "\n", row.names(kanto_pts)),
cex = ifelse(kanto_pts$city, 0.7, 0.4),
pos = c(1, 1, 1, 4, 1, 1, 2, 1, 2, 1, 1, 1, 3, 3, 1, 1),
family = "Press Start 2P" # from Google Fonts
)
# Plot: each point is a city coloured with its name
# e.g. Fuchsia City is pink
kanto_cities <-
kanto_pts[kanto_pts$city | rownames(kanto_pts) == "Indigo Plateau", ]
kanto_cities$colour <- c( # close-enough named R colours
"white", "aquamarine4", "grey57", "royalblue3 ",
"red3 ", "lavender", "darkseagreen2", "magenta",
"tomato2", "orangered2", "blue"
)
par(mar = rep(0, 4))
with(kanto_cities, plot(x, y, axes = FALSE))
with(kanto_cities, points(x, y, pch = 22, cex = 4, bg = colour))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment