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

preview_player
Показать описание
In this video we plot the map of Tokyo with ggplot2.

## 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)

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() +

# set the coordinates for the labels on the map
colnames(centroid) = c("long", "lat")
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() +
geom_text(data = centroid,
aes(x = long, y = lat, label = id),
size = 3, fontface = "bold",
check_overlap = TRUE)
Рекомендации по теме
Комментарии
Автор

Could you share the code file please? thanks in advance

drahmsha