Just a simple Rmarkdown file to test Choropleths plot on the Japan map from the mapdata package. Note that the map includes regions but not subregions.
Dataset from kaggle regarding some hotel location: https://www.kaggle.com/koki25ando/hostel-world-dataset/downloads/hostel-world-dataset.zip/6
Source:
library(mapdata) # Japan map per region
## Loading required package: maps
library(ggplot2)
library(readr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(viridis)
## Loading required package: viridisLite
library(mapproj) # for coord_map()
library(viridis)
# can be used to focus on a single region
map('japan', "Kyoto")
df <- map_data("japan")
ggplot() + geom_polygon(data = df, aes(x=long, y = lat, group = group)) +
coord_fixed(1.3) # to keep the same ratio
ggplot() +
geom_polygon(data = df, aes(x=long, y = lat, group = group), fill = NA, color = "red") +
coord_fixed(1.3)
The following code was found on https://blog.revolutionanalytics.com/2009/11/choropleth-challenge-result.html
Note the cut function to create bins.
state_df <- map_data(“state”)
choropleth <- merge(county_df, unemp, by = c(“state”, “county”))
choropleth <- choropleth[order(choropleth$order), ]
choropleth\(rate_d <- cut(choropleth\)rate, breaks = c(seq(0, 10, by = 2), 35))
ggplot(choropleth, aes(long, lat, group = group)) +
geom_polygon(aes(fill = rate_d), colour = alpha(“white”, 1/2), size = 0.2) +
geom_polygon(data = state_df, colour = “white”, fill = NA) +
scale_fill_brewer(pal = “PuRd”)
ds <- read_csv("C:/Users/Ndee/Documents/R/own_project/190525_mapping/Hostel.csv")
## Warning: Missing column names filled in: 'X1' [1]
## Parsed with column specification:
## cols(
## X1 = col_double(),
## hostel.name = col_character(),
## City = col_character(),
## price.from = col_double(),
## Distance = col_character(),
## summary.score = col_double(),
## rating.band = col_character(),
## atmosphere = col_double(),
## cleanliness = col_double(),
## facilities = col_double(),
## location.y = col_double(),
## security = col_double(),
## staff = col_double(),
## valueformoney = col_double(),
## lon = col_double(),
## lat = col_double()
## )
ds <- dplyr::filter(ds, lat != is.na(ds$lat))
ggplot() +
geom_polygon(data = df, aes(x=long, y = lat, group = group),
fill= "white", color= "black") +
coord_fixed(1.3) +
geom_point(data = ds[1:2,], aes(x = lon, y = lat), color = "black", size = 5) +
geom_point(data = ds[1:2,], aes(x = lon, y = lat), color = "red", size = 3.5)
uni <- data.frame(region = unique(df$region), dumb_data= 1:47)
uni$region <- as.character(uni$region) # instead of factor
head(uni)
## region dumb_data
## 1 Hokkaido 1
## 2 Aomori 2
## 3 Iwate 3
## 4 Miyagi 4
## 5 Akita 5
## 6 Yamagata 6
ds1 <- left_join(x= df, y= uni, by= "region")
ggplot(data = ds1, aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = dumb_data), color = "white")+
coord_fixed(1.3) +
theme_void() +
theme(axis.line = element_blank(), axis.text = element_blank(),
axis.ticks = element_blank(), axis.title = element_blank()) +
scale_fill_viridis(option="viridis")+
coord_map()+
scale_fill_viridis(trans = "log", breaks=c(1,5,10,20,50,100), name="Legend", guide = guide_legend( keyheight = unit(3, units = "mm"), keywidth=unit(12, units = "mm"), label.position = "bottom", title.position = 'top', nrow=1) ) +
labs(
title = "Japan map with dumb data",
subtitle = "Check r-graph-gallery.com for great code on Choropleths",
caption = "Source: [...]"
) +
theme(
plot.title = element_text(size= 22, hjust=0.01, color = "#4e4d47", margin = margin(b = -0.1, t = 0.4, l = 2, unit = "cm")),
plot.subtitle = element_text(size= 17, hjust=0.01, color = "#4e4d47", margin = margin(b = -0.1, t = 0.43, l = 2, unit = "cm")),
plot.caption = element_text( size=12, color = "#4e4d47", margin = margin(b = 0.3, r=-99, unit = "cm") ),
legend.position = c(0.7, 0.09)
)
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.
## Scale for 'fill' is already present. Adding another scale for 'fill',
## which will replace the existing scale.