library(csmaps)
library(ggplot2)
library(data.table)
library(magrittr)
The maps can be easily customized with color and labels.
location_code
<- copy(csmaps::nor_county_map_b2024_insert_oslo_dt)
pd
<- ggplot()
q <- q + geom_polygon(
q data = pd,
mapping = aes(
x = long,
y = lat,
group = group,
fill = location_code
), color="black",
linewidth = 0.2
)<- q + annotate(
q "text",
x = csmaps::nor_xxx_position_title_insert_oslo_b2024_insert_oslo_dt$long,
y = csmaps::nor_xxx_position_title_insert_oslo_b2024_insert_oslo_dt$lat,
label = "Oslo"
)<- q + theme_void()
q <- q + coord_quickmap()
q <- q + labs(title = "")
q q
It is also possible to specify the color by user-defined groups. Here we show an example of assigning different (pseudo) risk level to each county.
<- copy(csmaps::nor_county_map_b2024_insert_oslo_dt)
pd
# assign each location a random category for different colors
<- unique(pd[,c("location_code")])
location_info :=rep(
location_info[,categoryc("Good","Normal","Neutral","Bad","Very Bad"),
each=3)[1:.N]
]:=factor(
location_info[,category
category,levels=c("Good","Normal","Neutral","Bad","Very Bad")
)
]print(location_info)
#> location_code category
#> 1: county_nor01 Good
#> 2: county_nor02 Good
#> 3: county_nor03 Good
#> 4: county_nor11 Normal
#> 5: county_nor15 Normal
#> 6: county_nor18 Normal
#> 7: county_nor33 Neutral
#> 8: county_nor34 Neutral
#> 9: county_nor39 Neutral
#> 10: county_nor40 Bad
#> 11: county_nor42 Bad
#> 12: county_nor46 Bad
#> 13: county_nor50 Very Bad
#> 14: county_nor55 Very Bad
#> 15: county_nor56 Very Bad
# join the map data.table
="location_code",category:=category]
pd[location_info,on
<- ggplot()
q <- q + geom_polygon(
q data = pd,
mapping = aes(
x = long,
y = lat,
group = group,
fill=category
), color="black",
size=0.25
)<- q + annotate(
q "text",
x = csmaps::nor_xxx_position_title_insert_oslo_b2024_insert_oslo_dt$long,
y = csmaps::nor_xxx_position_title_insert_oslo_b2024_insert_oslo_dt$lat,
label = "Oslo"
)<- q + coord_quickmap()
q <- q + labs(title="")
q <- q + theme_void()
q q
We can add labels of county index onto the maps. There are several
options for adding texts on a graph in ggplot2
. We
recommend geom_label()
to add the labels if no label
overlap occurs, otherwise we recommend using
ggrepel::geom_label_repel()
.
<- copy(csmaps::nor_county_map_b2024_insert_oslo_dt)
pd <- ggplot()
q <- q + geom_polygon(
q data = pd,
mapping = aes(
x = long,
y = lat,
group = group,
fill = location_code
), color="black",
linewidth = 0.2
)<- q + annotate(
q "text",
x = csmaps::nor_xxx_position_title_insert_oslo_b2024_insert_oslo_dt$long,
y = csmaps::nor_xxx_position_title_insert_oslo_b2024_insert_oslo_dt$lat,
label = "Oslo"
)<- q + geom_label(
q data = csmaps::nor_county_position_geolabels_b2024_default_dt,
mapping = aes(x = long, y = lat, label = location_code)
)# ggrepel::geom_label_repel() for avoiding overlap
<- q + theme_void()
q <- q + coord_quickmap()
q <- q + labs(title = "")
q q
Labels can be easily added to other layouts, such as Oslo wards.
<- ggplot(mapping = aes(x = long, y = lat))
q <- q + geom_polygon(
q data = csmaps::oslo_ward_map_b2024_default_dt,
mapping = aes(group = group),
color = "black",
fill = "white",
linewidth = 0.2
)<- q + geom_label(
q data = csmaps::oslo_ward_position_geolabels_b2024_default_dt,
mapping = aes(label = location_code),
color = "red",
size = 3,
label.size = 0.1,
label.r = grid::unit(0, "lines")
)<- q + theme_void()
q <- q + coord_quickmap()
q q
It is convenient to use csdata
package to enrich Norway
and Oslo maps with external information, such as location name and
population. We illustrate how to do it here.
# enrich with population and location name
<- csdata::nor_population_by_age_cats()[calyear==2024]
dpop_2024
# join, create label
<- copy(csmaps::nor_county_position_geolabels_b2024_insert_oslo_dt)
labels
labels[
dpop_2024, = "location_code",
on := pop_jan1_n
pop_total
]
labels[::nor_locations_names(),
csdata= "location_code",
on := location_name
location_name
]:= paste0(location_name, '\n', pop_total)]
labels[, label print(head(labels))
#> location_code long lat pop_total location_name label
#> 1: county_nor31 11.26614 59.33375 NA Østfold Østfold\nNA
#> 2: county_nor32 11.20000 60.03851 NA Akershus Akershus\nNA
#> 3: county_nor33 8.85000 60.60000 NA Buskerud Buskerud\nNA
#> 4: county_nor03 20.85000 62.00000 709037 Oslo Oslo\n709037
#> 5: county_nor34 11.00000 61.86886 373628 Innlandet Innlandet\n373628
#> 6: county_nor39 10.00000 59.32481 NA Vestfold Vestfold\nNA
# plot
<- copy(csmaps::nor_county_map_b2024_insert_oslo_dt)
pd <- ggplot()
q <- q + geom_polygon(
q data = pd,
mapping = aes(
x = long,
y = lat,
group = group,
fill = location_code
),color="black",
linewidth = 0.2
)<- q + annotate(
q "text",
x = csmaps::nor_xxx_position_title_insert_oslo_b2024_insert_oslo_dt$long,
y = csmaps::nor_xxx_position_title_insert_oslo_b2024_insert_oslo_dt$lat,
label = "Oslo"
)<- q + ggrepel::geom_label_repel(
q data = labels,
mapping = aes(x = long, y = lat, label = label)
)<- q + theme_void()
q <- q + coord_quickmap()
q <- q + labs(title = "")
q q
# enrich with population and location name
<- csdata::nor_population_by_age_cats()[calyear==2024]
dpop_2024
# join, create label
<- copy(csmaps::oslo_ward_position_geolabels_b2024_default_dt)
labels
labels[
dpop_2024, = "location_code",
on := pop_jan1_n
pop_total
]
labels[::nor_locations_names(),
csdata= "location_code",
on := location_name
location_name
]:= paste0(location_name, '\n', pop_total)]
labels[, label print(head(labels))
#> location_code long lat pop_total location_name
#> 1: wardoslo_nor030101 10.79760 59.91010 61756 Gamle Oslo
#> 2: wardoslo_nor030102 10.78000 59.92567 64270 Grünerløkka
#> 3: wardoslo_nor030103 10.76683 59.93981 46984 Sagene
#> 4: wardoslo_nor030104 10.73555 59.91230 39487 St. Hanshaugen
#> 5: wardoslo_nor030105 10.66500 59.89925 60163 Frogner
#> 6: wardoslo_nor030106 10.65000 59.92500 35198 Ullern
#> label
#> 1: Gamle Oslo\n61756
#> 2: Grünerløkka\n64270
#> 3: Sagene\n46984
#> 4: St. Hanshaugen\n39487
#> 5: Frogner\n60163
#> 6: Ullern\n35198
<- ggplot(mapping = aes(x = long, y = lat))
q <- q + geom_polygon(
q data = csmaps::oslo_ward_map_b2024_default_dt,
mapping = aes(group = group),
color = "black",
fill = "white",
linewidth = 0.2
)<- q + geom_label(
q data = labels,
mapping = aes(label = label),
color = "red",
size = 3,
label.size = 0.1,
label.r = grid::unit(0, "lines")
)<- q + theme_void()
q <- q + coord_quickmap()
q q