Packages
library(tidyverse)
library(readxl)
library(sf)
library(raster)
library(ggiraph)
Departments
Peru is divided into 24 regions plus El Callao and Lima Province (or metropolitan Lima city). El Callao is its own region, containing only one province, the Constitutional Province of Callao. Lima Province is the only province in the country that does not belong to any of the other regions. Its capital is Lima, which is also the capital of the country.
##### {Raster} package (level 1 = departaments)
PER_1 <- getData("GADM", country ="PE", level =1)
PERU_1_df <-broom::tidy(PER_1, region = "NAME_1")
##### Plot with animated tooltip:
# link to wikipedia:
PERU_1_df$onclick <- sprintf("window.open(\"%s%s\")",
"http://en.wikipedia.org/wiki/", as.character(PERU_1_df$id))
#plot:
m <- ggplot() +
labs(title = "Peru: Regions",
subtitle = "",
fill = NULL) +
geom_polygon_interactive(data = PERU_1_df,
aes(x = long, y = lat, group = group,
tooltip = id, data_id = id, onclick = onclick),
fill = "orange",
color = "white",
size = 0.2,
alpha = 0.8) +
coord_map() +
theme_void()+
theme(
axis.line = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
plot.background = element_rect(fill = "snow", color = NA),
panel.background = element_rect(fill= "snow", color = NA),
plot.title = element_text(size = 16, hjust = 0.5),
plot.subtitle = element_text(size = 12, hjust = 0.5),
plot.caption = element_text(size = 8, hjust = 1),
legend.title = element_text(color = "grey40", size = 8),
legend.text = element_text(color = "grey40", size = 7, hjust = 0),
legend.position = c(0.05, 0.25),
plot.margin = unit(c(0.5,2,0.5,1), "cm")) +
guides(fill = FALSE)
widgetframe::frameWidget(ggiraph(code=print(m), width = 0.7, hover_css = "cursor:pointer;fill:lightgrey;stroke:darkgrey;"))
Provinces
The provinces of Peru are the second level administrative subdivision of the country. There are 195 provinces in Peru, grouped in the 25 regions previously mentioned, except for the Lima Province (that does not belong to any region)
##### {Raster} package (level 2 = provinces)
PER_2 <- getData("GADM", country ="PE", level = 2)
PERU_2_df <-broom::tidy(PER_2, region = "NAME_2")
##### Plot with animated tooltip:
# link to wikipedia:
PERU_2_df$onclick <- sprintf("window.open(\"%s%s\")",
"http://en.wikipedia.org/wiki/", as.character(PERU_2_df$id))
#plot:
m <- ggplot() +
labs(title = "Peru: Provinces",
subtitle = "",
fill = NULL) +
geom_polygon_interactive(data = PERU_2_df,
aes(x = long, y = lat, group = group,
tooltip = id, data_id = id, onclick = onclick),
fill = "orange",
color = "white",
size = 0.2,
alpha = 0.8) +
coord_map() +
theme_void()+
theme(
axis.line = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
plot.background = element_rect(fill = "snow", color = NA),
panel.background = element_rect(fill= "snow", color = NA),
plot.title = element_text(size = 16, hjust = 0.5),
plot.subtitle = element_text(size = 12, hjust = 0.5),
plot.caption = element_text(size = 8, hjust = 1),
legend.title = element_text(color = "grey40", size = 8),
legend.text = element_text(color = "grey40", size = 7, hjust = 0),
legend.position = c(0.05, 0.25),
plot.margin = unit(c(0.5,2,0.5,1), "cm")) +
guides(fill = FALSE)
widgetframe::frameWidget(ggiraph(code=print(m), width = 0.7, hover_css = "cursor:pointer;fill:lightgrey;stroke:darkgrey;"))
Districts
The districts are the third level country administrative subdivision of Peru. The districts are the subdivision of the provinces. There are 1.838 districts in total.
##### {Raster} package (level 3 = districts)
PER_3 <- getData("GADM", country ="PE", level = 3)
PERU_3_df <-broom::tidy(PER_3, region = "NAME_3")
##### Plot with animated tooltip:
# link to wikipedia:
PERU_3_df$onclick <- sprintf("window.open(\"%s%s\")",
"http://en.wikipedia.org/wiki/", as.character(PERU_3_df$id))
#plot:
m <- ggplot() +
labs(title = "Peru: Districts",
subtitle = "",
fill = NULL) +
geom_polygon_interactive(data = PERU_3_df,
aes(x = long, y = lat, group = group,
tooltip = id, data_id = id, onclick = onclick),
fill = "orange",
color = "white",
size = 0.2,
alpha = 0.8) +
coord_map() +
theme_void()+
theme(
axis.line = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
plot.background = element_rect(fill = "snow", color = NA),
panel.background = element_rect(fill= "snow", color = NA),
plot.title = element_text(size = 16, hjust = 0.5),
plot.subtitle = element_text(size = 12, hjust = 0.5),
plot.caption = element_text(size = 8, hjust = 1),
legend.title = element_text(color = "grey40", size = 8),
legend.text = element_text(color = "grey40", size = 7, hjust = 0),
legend.position = c(0.05, 0.25),
plot.margin = unit(c(0.5,2,0.5,1), "cm")) +
guides(fill = FALSE)
widgetframe::frameWidget(ggiraph(code=print(m), width = 0.7, hover_css = "cursor:pointer;fill:lightgrey;stroke:darkgrey;"))