Maps with R: Tokyo map with ggplot2 | GADM geo data || 10

Published: 01 October 2021
on channel: Lab Time with R & Python
1,393
18

In this video we plot the map of Tokyo with ggplot2.
Geo coordinates from GADM: https://gadm.org/download_country_v3....

code to replicate
Note: I replaced the assignment operator with = because YouTube does not allow to write angle brackets in the description
In this video we plot the map of Tokyo city with ggplot2

library ----
library("ggplot2")
library("sp")
library("raster")
library("broom")
library("stringr")
library("scales")

download geo data from GADM ----
jpn2 = getData("GADM", country = "JPN", level = 2) # town/city

class(jpn2)
View(jpn2)
jpn2@data[["NAME_2"]]

plot(jpn2)

make data as data frame
jpn2_df = tidy(jpn2, region = "NAME_2")
class(jpn2_df)
View(jpn2_df)
View(unique(jpn2_df$id))

clean dataset names ----
we select Aisho to extract the o (small) with bar accent
accent_o = unique(jpn2_df$id)[19]
accent_o
subset fifth letter (from 5th to 5th)
accent_o = str_sub(accent_o, 5, 5)
accent_o
jpn2_df$id = str_replace_all(jpn2_df$id, accent_o, "o")

we select Chuou to extract the u (small) with bar accent
accent_u = unique(jpn2_df$id)[127]
accent_u
subset third letter (from 3rd to 3rd)
accent_u = str_sub(accent_u, 3, 3)
accent_u
jpn2_df$id = str_replace_all(jpn2_df$id, accent_u, "u")

we select Oitsu to extract the O (capital) with bar accent
accent_O = unique(jpn2_df$id)[1112]
accent_O
subset first letter (from 1st to 1st)
accent_O = str_sub(accent_O, 1, 1)
accent_O
jpn2_df$id = str_replace_all(jpn2_df$id, accent_O, "O")

23 special wards
tokyo = subset(jpn2_df,
id == "Adachi" |
id == "Arakawa" |
id == "Bunkyo" |
id == "Chiyoda" |
id == "Chuo" |
id == "Edogawa" |
id == "Itabashi" |
id == "Katsushika" |
id == "Kita" |
id == "Koto" |
id == "Meguro" |
id == "Minato" |
id == "Nakano" |
id == "Nerima" |
id == "Ota" |
id == "Setagaya" |
id == "Shibuya" |
id == "Shinagawa" |
id == "Shinjuku" |
id == "Suginami" |
id == "Sumida" |
id == "Taito" |
id == "Toshima")

head(tokyo)

plot ----
ggplot() +
geom_polygon(data = tokyo,
aes(x = long, y = lat,
group = group,
fill = id)) +
ggtitle("Tokyo Metropolitan Map \n with ggplot2") +
coord_equal(xlim = c(139.5, 140),
ylim = c(35.5, 35.8)) +
theme_void() +
theme(legend.position = "bottom")

set the coordinates for the labels on the map
centroid = as.data.frame(coordinates(jpn2))
colnames(centroid) = c("long", "lat")
centroid$id = jpn2@data$NAME_2
head(centroid)
centroid$id = str_replace_all(centroid$id, accent_o, "o")
centroid$id = str_replace_all(centroid$id, accent_O, "O")
centroid$id = str_replace_all(centroid$id, accent_u, "u")
unique(tokyo$id) %in% centroid$id

centroid = subset(centroid, id == "Adachi" |
id == "Arakawa" |
id == "Bunkyo" |
id == "Chiyoda" |
id == "Chuo" |
id == "Edogawa" |
id == "Itabashi" |
id == "Katsushika" |
id == "Kita" |
id == "Koto" |
id == "Meguro" |
id == "Minato" |
id == "Nakano" |
id == "Nerima" |
id == "Ota" |
id == "Setagaya" |
id == "Shibuya" |
id == "Shinagawa" |
id == "Shinjuku" |
id == "Suginami" |
id == "Sumida" |
id == "Taito" |
id == "Toshima")

ggplot() +
geom_polygon(data = tokyo,
aes(x = long, y = lat,
group = group,
fill = id)) +
ggtitle("Tokyo Metropolitan Map \n with ggplot2") +
coord_equal(xlim = c(139.5, 140),
ylim = c(35.5, 35.8)) +
theme_void() +
theme(legend.position = "none") +
geom_text(data = centroid,
aes(x = long, y = lat, label = id),
size = 3, fontface = "bold",
check_overlap = TRUE)


On this page of the site you can watch the video online Maps with R: Tokyo map with ggplot2 | GADM geo data || 10 with a duration of hours minute second in good quality, which was uploaded by the user Lab Time with R & Python 01 October 2021, share the link with friends and acquaintances, this video has already been watched 1,393 times on youtube and it was liked by 18 viewers. Enjoy your viewing!