Skip to content

Commit

Permalink
Create plot_regions and plot_municipalities and add to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksanderbl29 committed Oct 28, 2024
1 parent 82ef053 commit bc857c3
Show file tree
Hide file tree
Showing 11 changed files with 277 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Generated by roxygen2: do not edit by hand

export(plot_denmark)
export(plot_municipalities)
export(plot_regions)
importFrom(ggplot2,aes)
importFrom(ggplot2,geom_sf)
importFrom(ggplot2,ggplot)
Expand Down
132 changes: 132 additions & 0 deletions R/plot_municipalities.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#' Plot selected (or all) municipalities
#'
#' @description
#' Plot a vector of municipalities in Denmark. Just provide the name.
#'
#' @param region Municipality to plot. Mutiple is supported.
#'
#' @export
#' @importFrom ggplot2 ggplot theme_bw labs geom_sf aes guides
#'
#' @returns Returns a `{ggplot2}` object and prints the plot as well.
#' @examples
#' plot_municipalities(municipality = c("Aarhus", "Favrskov"))
plot_municipalities <- function(
municipality = c(
"København",
"Frederiksberg",
"Ballerup",
"Brøndby",
"Dragør",
"Gentofte",
"Gladsaxe",
"Glostrup",
"Herlev",
"Albertslund",
"Hvidovre",
"Høje-Taastrup",
"Lyngby-Taarbæk",
"Rødovre",
"Ishøj",
"Tårnby",
"Vallensbæk",
"Furesø",
"Allerød",
"Fredensborg",
"Helsingør",
"Hillerød",
"Hørsholm",
"Rudersdal",
"Egedal",
"Frederikssund",
"Greve",
"Køge",
"Halsnæs",
"Roskilde",
"Solrød",
"Gribskov",
"Odsherred",
"Holbæk",
"Faxe",
"Kalundborg",
"Ringsted",
"Slagelse",
"Stevns",
"Sorø",
"Lejre",
"Lolland",
"Næstved",
"Guldborgsund",
"Vordingborg",
"Bornholm",
"Middelfart",
"Christiansø",
"Assens",
"Faaborg-Midtfyn",
"Kerteminde",
"Nyborg",
"Odense",
"Svendborg",
"Nordfyns",
"Langeland",
"Ærø",
"Haderslev",
"Billund",
"Sønderborg",
"Tønder",
"Esbjerg",
"Fanø",
"Varde",
"Vejen",
"Aabenraa",
"Fredericia",
"Horsens",
"Kolding",
"Vejle",
"Herning",
"Holstebro",
"Lemvig",
"Struer",
"Syddjurs",
"Norddjurs",
"Favrskov",
"Odder",
"Randers",
"Silkeborg",
"Samsø",
"Skanderborg",
"Aarhus",
"Ikast-Brande",
"Ringkøbing-Skjern",
"Hedensted",
"Morsø",
"Skive",
"Thisted",
"Viborg",
"Brønderslev",
"Frederikshavn",
"Vesthimmerlands",
"Læsø",
"Rebild",
"Mariagerfjord",
"Jammerbugt",
"Aalborg",
"Hjørring"
)) {
municipality <- match.arg(municipality,
several.ok = TRUE
)

if (length(municipality) == 1) {
title_text <- municipality
} else if (length(municipality) > 1) {
title_text <- title_collapse(municipality)
}

dawaR::get_map_data("kommuner") |>
dplyr::filter(navn %in% municipality) |>
ggplot() +
geom_sf() +
theme_bw() +
labs(title = title_text)
}
38 changes: 38 additions & 0 deletions R/plot_regions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#' Plot selected (or all) regions
#'
#' @description
#' Plot a vector of regions in Denmark. Just provide the name.
#'
#' @param region Region(s) to plot
#'
#' @export
#' @importFrom ggplot2 ggplot theme_bw labs geom_sf aes guides
#'
#' @returns Returns a `{ggplot2}` object and prints the plot as well.
#' @examples
#' plot_regions(region = c("Region Nordjylland", "Region Midtjylland"))
plot_regions <- function(
region = c(
"Region Nordjylland",
"Region Midtjylland",
"Region Syddanmark",
"Region Hovedstaden",
"Region Sjælland"
)) {
region <- match.arg(region,
several.ok = TRUE
)

if (length(region) == 1) {
title_text <- region
} else if (length(region) > 1) {
title_text <- title_collapse(region)
}

dawaR::get_map_data("regioner") |>
dplyr::filter(navn %in% region) |>
ggplot() +
geom_sf() +
theme_bw() +
labs(title = title_text)
}
11 changes: 11 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
title_collapse <- function(input, comma = TRUE) {
if (length(input) == 1) {
return(input)
} else {
paste(
paste(input[-length(input)], collapse = ", "),
input[length(input)],
sep = " & "
)
}
}
10 changes: 10 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,13 @@ library(geodk)
plot_denmark()
```

```{r}
plot_regions(region = c("Region Nordjylland", "Region Midtjylland"))
```

```{r}
plot_municipalities(municipality = c("Aarhus", "Favrskov", "Vejle"))
```



18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,21 @@ plot_denmark()
```

<img src="man/figures/README-example-1.png" width="100%" />

``` r
plot_regions(region = c("Region Nordjylland", "Region Midtjylland"))
#> → Using cached response.
#> Change this behaviour by setting cache = FALSE
```

<img src="man/figures/README-unnamed-chunk-2-1.png" width="100%" />

``` r
plot_municipalities(municipality = c("Aarhus", "Favrskov", "Vejle"))
#> → Getting data on `kommuner`. This usually takes 13.13s.
#> Fetching data from the API. This will take some time.
#> Reading data to `st`.
#> Converting map data to `sf` object
```

<img src="man/figures/README-unnamed-chunk-3-1.png" width="100%" />
Binary file added man/figures/README-unnamed-chunk-2-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added man/figures/README-unnamed-chunk-3-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions man/plot_municipalities.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions man/plot_regions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions tests/testthat/test-get_levels.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ test_that("get_levels() provides the right levels", {
"menighedsraadsafstemningsomraader"
)

expect_equal(get_levels(),
levels)
expect_equal(
get_levels(),
levels
)
})

0 comments on commit bc857c3

Please sign in to comment.