# Setting up the packages and datasets
library(sf)
library(tidyverse)
library(ggplot2)
library(ggiraph)
library(viridis)
mainTrainStations <- read_csv("Shinkansen_stations_inJapan.csv")
JapanPrefectureMap <- read_sf("prefecturePolygons", "np872wp5062")
# Making a frequency table as a prerequisite to wrangle the data
countPrefecture <- table(mainTrainStations$Prefecture)
countPrefecture <- as.data.frame(countPrefecture) %>% rename(Prefecture = Var1)
countPrefecture$Prefecture <- as.character(countPrefecture$Prefecture)
# Data wrangling and cleaning
InteractiveJapan <- JapanPrefectureMap %>%
select(name_1, geometry) %>%
rename(Prefecture = name_1) %>%
left_join(mainTrainStations) %>%
select(Prefecture, geometry) %>%
group_by(Prefecture, geometry) %>%
summarise() %>%
left_join(countPrefecture) %>%
mutate(Freq = coalesce(Freq, 0)) %>%
rename("Train Station Count" = Freq) %>%
ggplot() +
geom_sf_interactive(mapping = aes(fill = `Train Station Count`,
tooltip = paste(Prefecture, "<br>", "Train Station Count: ", `Train Station Count`)),
color = "white") +
labs(title = "Distribution of Shinkansen Stations in Japan",
subtitle = "The number of Shinkansen (Bullet Train) stations in each prefecture of Japan to display
which prefecture is best connected with the capital, Tokyo, in 2022",
caption = "Source: https://www.kaggle.com/datasets/japandata509/shinkansen-stations-in-japan.
Prefecture data came from the Geospatial Information Authority of Japan website.
Plotted by: Andre Mojica 12/1/2023") +
scale_fill_viridis_c(option = "plasma",
guide=guide_legend(
direction = 'horizontal',
title = NULL,
label.hjust = .5,
label.position = 'top',
keywidth = 2.2,
keyheight = .2
)) +
theme_void() +
theme(legend.position = 'bottom',
plot.subtitle = element_text(
color = "darkgrey",
size = 8),
plot.caption = element_text(
color = "lightgrey",
size = 6
))
girafe(ggobj = InteractiveJapan)