-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dcfcd00
commit 96a5dbd
Showing
4 changed files
with
87 additions
and
3 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
_freeze/guides/2024-06-29-danmarkskort-i-r/index/execute-results/html.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
_freeze/guides/2024-06-29-danmarkskort-i-r/kort-kode/execute-results/html.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"hash": "512a13b1e0a82632ab8ddd38edf16a19", | ||
"result": { | ||
"engine": "knitr", | ||
"markdown": "---\ntitle: Samlet kode til danmarkskort\nexecute: \n echo: true\n eval: false\nlink-external-icon: true\nlink-external-newwindow: true\nother-links:\n - text: Samlet script\n icon: code\n href: https://github.com/aleksanderbl29/aleksanderbldk/blob/main/guides/2024-06-29-danmarkskort-i-r/kort.r\ndraft: true\n# date: 2024-06-29\nauthor: \"\"\n---\n\n\nHerunder er den samlede kode fra [guiden til at lave et nemt danmarkskort](index.qmd).\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(tidyverse) # Her får vi %>%, ggplot2 og andre smarte funktioner.\nlibrary(sf) # Skal bruges til at arbejde med \"simple features\" (figurer).\n\n## Gemmer URL til API-kald\nurl <- \"https://api.dataforsyningen.dk/afstemningsomraader?format=geojson\"\n\n# Sætter timeout til 10 minutter, fordi afstemingsstederne kan tage lang tid om at hente\noptions(timeout = 600)\n\n# Skaber midlertidig fil\ngeofile <- tempfile()\n\n# Henter geojson til tempfile\ndownload.file(url, geofile)\n\n# Læser datafilen ind i R\ngeodata_st <- st_read(geofile)\nafstemningssteder_geodata <- st_as_sf(geodata_st)\n\n# Simplificerer geodata\nafstemningssteder_geodata <- rmapshaper::ms_simplify(afstemningssteder_geodata,\n keep = 0.01,\n keep_shapes = TRUE)\n\n# Skaber danmarkskort\ndanmarkskort <- ggplot(afstemningssteder_geodata) +\n geom_sf() +\n ggthemes::theme_map() +\n labs(title = \"Afstemningssteder i Danmark\",\n caption = \"Kilde: DAWA/DAGI\") +\n theme(legend.position = \"none\",\n plot.title = element_text(size = 20),\n plot.caption = element_text(size = 10))\n\n# Skaber kort over steder i Aarhus\naarhus_kort <- afstemningssteder_geodata %>%\n filter(kommunenavn == \"Aarhus\") %>%\n ggplot(aes(fill = navn)) +\n geom_sf() +\n ggthemes::theme_map() +\n theme(legend.position = \"none\")\n\ndanmarkskort\n\naarhus_kort\n\n# Sætter timout tilbage til 1 minut\noptions(timeout = 60)\n```\n:::\n", | ||
"supporting": [], | ||
"filters": [ | ||
"rmarkdown/pagebreak.lua" | ||
], | ||
"includes": {}, | ||
"engineDependencies": {}, | ||
"preserve": {}, | ||
"postProcess": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
--- | ||
title: Samlet kode til danmarkskort | ||
execute: | ||
echo: true | ||
eval: false | ||
link-external-icon: true | ||
link-external-newwindow: true | ||
other-links: | ||
- text: Samlet script | ||
icon: code | ||
href: https://github.com/aleksanderbl29/aleksanderbldk/blob/main/guides/2024-06-29-danmarkskort-i-r/kort.r | ||
draft: true | ||
# date: 2024-06-29 | ||
author: "" | ||
--- | ||
|
||
Herunder er den samlede kode fra [guiden til at lave et nemt danmarkskort](index.qmd). | ||
|
||
```{r} | ||
library(tidyverse) # Her får vi %>%, ggplot2 og andre smarte funktioner. | ||
library(sf) # Skal bruges til at arbejde med "simple features" (figurer). | ||
## Gemmer URL til API-kald | ||
url <- "https://api.dataforsyningen.dk/afstemningsomraader?format=geojson" | ||
# Sætter timeout til 10 minutter, fordi afstemingsstederne kan tage lang tid om at hente | ||
options(timeout = 600) | ||
# Skaber midlertidig fil | ||
geofile <- tempfile() | ||
# Henter geojson til tempfile | ||
download.file(url, geofile) | ||
# Læser datafilen ind i R | ||
geodata_st <- st_read(geofile) | ||
afstemningssteder_geodata <- st_as_sf(geodata_st) | ||
# Simplificerer geodata | ||
afstemningssteder_geodata <- rmapshaper::ms_simplify(afstemningssteder_geodata, | ||
keep = 0.01, | ||
keep_shapes = TRUE) | ||
# Skaber danmarkskort | ||
danmarkskort <- ggplot(afstemningssteder_geodata) + | ||
geom_sf() + | ||
ggthemes::theme_map() + | ||
labs(title = "Afstemningssteder i Danmark", | ||
caption = "Kilde: DAWA/DAGI") + | ||
theme(legend.position = "none", | ||
plot.title = element_text(size = 20), | ||
plot.caption = element_text(size = 10)) | ||
# Skaber kort over steder i Aarhus | ||
aarhus_kort <- afstemningssteder_geodata %>% | ||
filter(kommunenavn == "Aarhus") %>% | ||
ggplot(aes(fill = navn)) + | ||
geom_sf() + | ||
ggthemes::theme_map() + | ||
theme(legend.position = "none") | ||
danmarkskort | ||
aarhus_kort | ||
# Sætter timout tilbage til 1 minut | ||
options(timeout = 60) | ||
``` | ||
|