R que R

Maps: Municipalities of Norway with links to Wikipedia

Sun, Apr 26, 2020
R maps
#ggplot2 #raster #ggiraph #maps #wikipedia


Packages



library(tidyverse)
library(readxl)
library(sf)
library(raster)
library(ggiraph)



Municipalities of Norway


Norway is divided into eleven regions (counties) and 365 municipalities. The Capital city is Oslo and it is considered both a county and a municipality.



NOR_2 <- getData("GADM", country ="NOR", level =2)

NOR_df <- broom::tidy(NOR_2, region = "NAME_2")

NOR_df$onclick <- sprintf("window.open(\"%s%s\")", "http://en.wikipedia.org/wiki/", as.character(NOR_df$id))

m <- ggplot() +
  labs(title = "Municipalities of Norway",
       subtitle = "",
       fill = NULL) +
  geom_polygon_interactive(data = NOR_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 = 1, hover_css = "cursor:pointer;fill:lightgrey;stroke:darkgrey;"))































.