Author: Richard Aubrey White, Chi Zhang
Date: 2021-01-15

# Customize your maps

#### Richard Aubrey White, Chi Zhang

#### 2021-01-15

    library(csmaps)
    library(ggplot2)
    library(data.table)
    library(magrittr)

The maps can be easily customized with color and labels.

## Colored maps

### Automatic coloring by `location_code`

    pd <- copy(csmaps::nor_county_map_b2024_insert_oslo_dt)

    q <- ggplot()
    q <- q + geom_polygon(
      data = pd,
      mapping = aes(
        x = long,
        y = lat,
        group = group,
        fill = location_code
      ),
      color="black",
      linewidth = 0.2
    )
    q <- q + annotate(
      "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 <- q + theme_void()
    q <- q + coord_quickmap()
    q <- q + labs(title = "")
    q

### Customized coloring with external information

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.

    pd <- copy(csmaps::nor_county_map_b2024_insert_oslo_dt)

    # assign each location a random category for different colors
    location_info <- unique(pd[,c("location_code")])
    location_info[,category:=rep(
      c("Good","Normal","Neutral","Bad","Very Bad"),
      each=3)[1:.N]
    ]
    location_info[,category:=factor(
      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
    pd[location_info,on="location_code",category:=category]

    q <- ggplot()
    q <- q + geom_polygon(
      data = pd,
      mapping = aes(
        x = long,
        y = lat,
        group = group,
        fill=category
      ),
      color="black",
      size=0.25
    )
    q <- q + annotate(
      "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 <- q + coord_quickmap()
    q <- q + labs(title="")
    q <- q + theme_void()
    q

## Labeled maps

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()`.

    pd <- copy(csmaps::nor_county_map_b2024_insert_oslo_dt)
    q <- ggplot()
    q <- q + geom_polygon(
      data = pd,
      mapping = aes(
        x = long,
        y = lat,
        group = group,
        fill = location_code
      ),
      color="black",
      linewidth = 0.2
    )
    q <- q + annotate(
      "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 <- q + geom_label(
      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 <- q + theme_void()
    q <- q + coord_quickmap()
    q <- q + labs(title = "")
    q

Labels can be easily added to other layouts, such as Oslo wards.

    q <- ggplot(mapping = aes(x = long, y = lat))
    q <- q + geom_polygon(
      data = csmaps::oslo_ward_map_b2024_default_dt,
      mapping = aes(group = group),
      color = "black",
      fill = "white",
      linewidth = 0.2
    )
    q <- q + geom_label(
      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 <- q + theme_void()
    q <- q + coord_quickmap()
    q

## Enrich plot with additional data

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.

### Add county name and population to Norway map

    # enrich with population and location name
    dpop_2024 <- csdata::nor_population_by_age_cats()[calyear==2024]

    # join, create label
    labels <- copy(csmaps::nor_county_position_geolabels_b2024_insert_oslo_dt)
    labels[
      dpop_2024,
      on = "location_code",
      pop_total := pop_jan1_n
    ]
    labels[
      csdata::nor_locations_names(),
      on = "location_code",
      location_name := location_name
    ]
    labels[, label := paste0(location_name, '\n', pop_total)]
    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
    pd <- copy(csmaps::nor_county_map_b2024_insert_oslo_dt)
    q <- ggplot()
    q <- q + geom_polygon(
      data = pd,
      mapping = aes(
        x = long,
        y = lat,
        group = group,
        fill = location_code
      ),
      color="black",
      linewidth = 0.2
    )
    q <- q + annotate(
      "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 <- q + ggrepel::geom_label_repel(
      data = labels,
      mapping = aes(x = long, y = lat, label = label)
    )
    q <- q + theme_void()
    q <- q + coord_quickmap()
    q <- q + labs(title = "")
    q

### Add location name for ward and population for Oslo map

    # enrich with population and location name
    dpop_2024 <- csdata::nor_population_by_age_cats()[calyear==2024]

    # join, create label
    labels <- copy(csmaps::oslo_ward_position_geolabels_b2024_default_dt)
    labels[
      dpop_2024,
      on = "location_code",
      pop_total := pop_jan1_n
    ]
    labels[
      csdata::nor_locations_names(),
      on = "location_code",
      location_name := location_name
    ]
    labels[, label := paste0(location_name, '\n', pop_total)]
    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

    q <- ggplot(mapping = aes(x = long, y = lat))
    q <- q + geom_polygon(
      data = csmaps::oslo_ward_map_b2024_default_dt,
      mapping = aes(group = group),
      color = "black",
      fill = "white",
      linewidth = 0.2
    )
    q <- q + geom_label(
      data = labels,
      mapping = aes(label = label),
      color = "red",
      size = 3,
      label.size = 0.1,
      label.r = grid::unit(0, "lines")
    )
    q <- q + theme_void()
    q <- q + coord_quickmap()
    q