From 21093721a8564e0a267670b6aaf6054d996da2cf Mon Sep 17 00:00:00 2001 From: Manuel Garcia Date: Sun, 18 Feb 2024 14:49:50 +0100 Subject: [PATCH 1/3] apply tydeverse coding style to code blocks --- episodes/01-intro-to-r.Rmd | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/episodes/01-intro-to-r.Rmd b/episodes/01-intro-to-r.Rmd index cccdbb1d..3030a067 100644 --- a/episodes/01-intro-to-r.Rmd +++ b/episodes/01-intro-to-r.Rmd @@ -105,11 +105,11 @@ An alternative solution is to create the folders using R command `dir.create()`. In the console type: ```{r create-directories} -dir.create('data') -dir.create('data_output') -dir.create('documents') -dir.create('fig_output') -dir.create('scripts') +dir.create("data") +dir.create("data_output") +dir.create("documents") +dir.create("fig_output") +dir.create("scripts") ``` @@ -218,7 +218,7 @@ We will however need to install the `here` package. To do so, please go to your script and type: ```{r install-here-package, eval=FALSE} -install.packages('here') +install.packages("here") ``` :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: callout @@ -311,8 +311,10 @@ In the script, we will write: ```{r download-files} # Download the data -download.file('https://bit.ly/geospatial_data', - here('data','gapminder_data.csv')) +download.file( + "https://bit.ly/geospatial_data", + here("data", "gapminder_data.csv") +) ``` @@ -337,9 +339,9 @@ will not cover these in the workshop. You can use R as calculator, you can for example write: ```{r calculator} -1+100 -1*100 -1/100 +1 + 100 +1 * 100 +1 / 100 ``` @@ -351,7 +353,7 @@ use them whenever we need to. We using the assignment operator `<-`, like this: ```{r asignment-operator} -x <- 1/40 +x <- 1 / 40 ``` Notice that assignment does not print a value. Instead, we've stored it for later From e37711f538123419b3a21710e1afb5011f6a5893 Mon Sep 17 00:00:00 2001 From: Manuel Garcia Date: Sun, 18 Feb 2024 14:50:07 +0100 Subject: [PATCH 2/3] apply tydeverse coding style to code blocks --- episodes/02-data-structures.Rmd | 34 +++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/episodes/02-data-structures.Rmd b/episodes/02-data-structures.Rmd index 6071354e..4fadc987 100644 --- a/episodes/02-data-structures.Rmd +++ b/episodes/02-data-structures.Rmd @@ -211,11 +211,13 @@ You can reorder the categories using `factor()` function. This can be useful, fo ```{r factor-reorder1} nordic_cat <- factor( - nordic_cat, levels = c( - 'Norway', - 'Denmark', - 'Sweden' - )) + nordic_cat, + levels = c( + "Norway", + "Denmark", + "Sweden" + ) +) # now Norway will be the first category, Denmark second and Sweden third nordic_cat @@ -230,12 +232,12 @@ we will use `fct_relevel()` function from `forcats` package to do the reordering library(forcats) nordic_cat <- fct_relevel( - nordic_cat, - 'Norway' , - 'Denmark', - 'Sweden' - ) # With this, Norway will be first category, - # Denmark second and Sweden third + nordic_cat, + "Norway", + "Denmark", + "Sweden" +) # With this, Norway will be first category, +# Denmark second and Sweden third nordic_cat ``` @@ -263,13 +265,13 @@ outside of this set, it will become an unknown/missing value detonated by ```{r factor-missing-level} nordic_str nordic_cat2 <- factor( - nordic_str, - levels = c('Norway', 'Denmark') - ) + nordic_str, + levels = c("Norway", "Denmark") +) -# because we did not include Sweden in the list of +# because we did not include Sweden in the list of # factor levels, it has become NA. -nordic_cat2 +nordic_cat2 ``` :::::::::::::::::::::::::::::::::::::::::::::::::::: From b32d7bbe7c588f517df100393b04580ba6c45932 Mon Sep 17 00:00:00 2001 From: Manuel Garcia Date: Sun, 18 Feb 2024 15:11:19 +0100 Subject: [PATCH 3/3] apply tydeverse coding style to code blocks --- episodes/03-explore-data.Rmd | 42 ++-- episodes/04-intro-to-visualisation.Rmd | 143 ++++++----- episodes/09-open-and-plot-vector-layers.Rmd | 2 +- ...re-and-plot-by-vector-layer-attributes.Rmd | 120 ++++++---- episodes/11-plot-multiple-shape-files.Rmd | 226 ++++++++++-------- ...12-handling-spatial-projection-and-crs.Rmd | 103 ++++---- 6 files changed, 348 insertions(+), 288 deletions(-) diff --git a/episodes/03-explore-data.Rmd b/episodes/03-explore-data.Rmd index 489b0826..5e1fd1bc 100644 --- a/episodes/03-explore-data.Rmd +++ b/episodes/03-explore-data.Rmd @@ -95,7 +95,7 @@ There are multiple ways to explore a data set. Here are just a few examples: head(gapminder) # shows first 6 rows of the data set -summary(gapminder) # basic statistical information about each column. +summary(gapminder) # basic statistical information about each column. # Information format differes by data type. nrow(gapminder) # returns number of rows in a dataset @@ -110,9 +110,9 @@ When you're analyzing a data set, you often need to access its specific columns. One handy way to access a column is using it's name and a dollar sign `$`: ```{r subset-dollar-sign} -# This notation means: From dataset gapminder, give me column country. You can -# see that the column accessed in this way is just a vector of characters. -country_vec <- gapminder$country +# This notation means: From dataset gapminder, give me column country. You can +# see that the column accessed in this way is just a vector of characters. +country_vec <- gapminder$country head(country_vec) @@ -128,7 +128,7 @@ Let's start manipulating the data. First, we will adapt our data set, by keeping only the columns we're interested in, using the `select()` function from the `dplyr` package: ```{r dplyr-select} -year_country_gdp <- select(gapminder, year, country, gdpPercap) +year_country_gdp <- select(gapminder, year, country, gdpPercap) head(year_country_gdp) @@ -145,8 +145,8 @@ The `select()` statement with pipe would look like that: ```{r dplyr-pipe} -year_country_gdp <- gapminder %>% - select(year,country,gdpPercap) +year_country_gdp <- gapminder %>% + select(year, country, gdpPercap) head(year_country_gdp) @@ -160,8 +160,8 @@ We already know how to select only the needed columns. But now, we also want to In the `gapminder` data set, we want to see the results from outside of Europe for the 21st century. ```{r} -year_country_gdp_euro <- gapminder %>% - filter(continent != "Europe" & year >= 2000) %>% +year_country_gdp_euro <- gapminder %>% + filter(continent != "Europe" & year >= 2000) %>% select(year, country, gdpPercap) # '&' operator (AND) - both conditions must be met @@ -181,9 +181,9 @@ Write a single command (which can span multiple lines and includes pipes) that w ```{r ex5, class.source="bg-info"} -year_country_gdp_eurasia <- gapminder %>% - filter(continent == "Europe" | continent == "Asia") %>% - select(year, country, gdpPercap) +year_country_gdp_eurasia <- gapminder %>% + filter(continent == "Europe" | continent == "Asia") %>% + select(year, country, gdpPercap) # '|' operator (OR) - one of the conditions must be met nrow(year_country_gdp_eurasia) @@ -215,10 +215,10 @@ Calculate the average life expectancy per country. Which country has the longest ```{r ex6 , class.source="bg-info"} gapminder %>% - group_by(country) %>% - summarize(avg_lifeExp=mean(lifeExp)) %>% - filter(avg_lifeExp == min(avg_lifeExp) | - avg_lifeExp == max(avg_lifeExp) ) + group_by(country) %>% + summarize(avg_lifeExp = mean(lifeExp)) %>% + filter(avg_lifeExp == min(avg_lifeExp) | + avg_lifeExp == max(avg_lifeExp)) ``` ### Multiple groups and summary variables @@ -235,14 +235,14 @@ gapminder %>% On top of this, you can also make multiple summaries of those groups: ```{r dplyr-summ} gdp_pop_bycontinents_byyear <- gapminder %>% - group_by(continent,year) %>% + group_by(continent, year) %>% summarize( avg_gdpPercap = mean(gdpPercap), sd_gdpPercap = sd(gdpPercap), avg_pop = mean(pop), sd_pop = sd(pop), n_obs = n() - ) + ) ``` @@ -252,8 +252,8 @@ If you need only a number of observations per group, you can use the `count()` f ```{r dplyr-count} gapminder %>% - group_by(continent) %>% - count() + group_by(continent) %>% + count() ``` @@ -263,7 +263,7 @@ Frequently you’ll want to create new columns based on the values in existing c ```{r dplyr-mutate} gapminder_gdp <- gapminder %>% - mutate(gdpBillion = gdpPercap*pop/10^9) + mutate(gdpBillion = gdpPercap * pop / 10^9) head(gapminder_gdp) diff --git a/episodes/04-intro-to-visualisation.Rmd b/episodes/04-intro-to-visualisation.Rmd index 188b74ac..8fd4158e 100644 --- a/episodes/04-intro-to-visualisation.Rmd +++ b/episodes/04-intro-to-visualisation.Rmd @@ -68,10 +68,11 @@ Now, lets plot the distribution of life expectancy in the `gapminder` dataset: ```{r ggplot} -ggplot(data = gapminder, # data - aes(x = lifeExp) # aesthetics layer - ) + -geom_histogram() # geometry layer +ggplot( + data = gapminder, # data + aes(x = lifeExp) # aesthetics layer +) + + geom_histogram() # geometry layer ``` @@ -83,22 +84,24 @@ already learned: `%>%` ( or `|>`) and follow them by ggplot syntax. Let's create another plot, this time only on a subset of observations: ```{r ggplot-col} -gapminder %>% # we select a data set - filter(year == 2007 & continent == 'Americas') %>% # filter to keep one year and one continent - ggplot(aes(x = country, y = gdpPercap)) + # the x and y axes represent values of columns - geom_col() # we select a column graph as a geometry +gapminder %>% # we select a data set + filter(year == 2007 & continent == "Americas") %>% # filter to keep one year and one continent + ggplot(aes(x = country, y = gdpPercap)) + # the x and y axes represent values of columns + geom_col() # we select a column graph as a geometry ``` Now, you can iteratively improve how the plot looks like. For example, you might want to flip it, to better display the labels. ```{r ggplot-coord-flip} -gapminder %>% - filter(year == 2007, - continent == 'Americas') %>% - ggplot(aes(x = country, y = gdpPercap)) + - geom_col() + - coord_flip() # flip axes +gapminder %>% + filter( + year == 2007, + continent == "Americas" + ) %>% + ggplot(aes(x = country, y = gdpPercap)) + + geom_col() + + coord_flip() # flip axes ``` One thing you might want to change here is the order in which countries @@ -110,11 +113,13 @@ Now the order of the levels will depend on another variable - GDP per capita. ```{r ggplot-color} -gapminder %>% - filter(year == 2007, - continent == 'Americas') %>% - mutate(country = fct_reorder(country, gdpPercap )) %>% # reorder factor levels - ggplot(aes(x = country , y = gdpPercap)) + +gapminder %>% + filter( + year == 2007, + continent == "Americas" + ) %>% + mutate(country = fct_reorder(country, gdpPercap)) %>% # reorder factor levels + ggplot(aes(x = country, y = gdpPercap)) + geom_col() + coord_flip() @@ -124,16 +129,18 @@ Let's make things more colourful - let's represent the average life expectancy of a country by colour ```{r ggplot-colors} -gapminder %>% - filter(year == 2007, - continent == 'Americas') %>% - mutate(country = fct_reorder(country, gdpPercap )) %>% +gapminder %>% + filter( + year == 2007, + continent == "Americas" + ) %>% + mutate(country = fct_reorder(country, gdpPercap)) %>% ggplot(aes( - x = country, - y = gdpPercap, + x = country, + y = gdpPercap, fill = lifeExp # use 'fill' for surfaces; 'colour' for points and lines )) + - geom_col() + + geom_col() + coord_flip() ``` @@ -143,14 +150,16 @@ readability and colorblind-proofness are the palettes available in the `viridis` package. ```{r ggplot-colors-adapt} -gapminder %>% - filter(year == 2007, - continent == 'Americas') %>% +gapminder %>% + filter( + year == 2007, + continent == "Americas" + ) %>% mutate(country = fct_reorder(country, gdpPercap)) %>% - ggplot(aes(x = country, y = gdpPercap, fill = lifeExp)) + - geom_col() + - coord_flip() + - scale_fill_viridis_c() # _c stands for continuous scale + ggplot(aes(x = country, y = gdpPercap, fill = lifeExp)) + + geom_col() + + coord_flip() + + scale_fill_viridis_c() # _c stands for continuous scale ``` @@ -158,23 +167,27 @@ Maybe we don't need that much information about the life expectancy. We only want to know if it's below or above average. We will make use of the `if_else()` function inside `mutate()` to create a new column `lifeExpCat` with the value `high` if life expectancy is above average and `low` otherwise. Note the usage of the `if_else()` function: `if_else(, , )`. ```{r ggplot-colors-discrete} -p <- # this time let's save the plot in an object - gapminder %>% - filter(year == 2007 & - continent == 'Americas') %>% - mutate(country = fct_reorder(country, gdpPercap), - lifeExpCat = if_else( - lifeExp >= mean(lifeExp), - 'high', - 'low')) %>% - ggplot(aes(x = country, y = gdpPercap, fill = lifeExpCat)) + - geom_col() + +p <- # this time let's save the plot in an object + gapminder %>% + filter(year == 2007 & + continent == "Americas") %>% + mutate( + country = fct_reorder(country, gdpPercap), + lifeExpCat = if_else( + lifeExp >= mean(lifeExp), + "high", + "low" + ) + ) %>% + ggplot(aes(x = country, y = gdpPercap, fill = lifeExpCat)) + + geom_col() + coord_flip() + - scale_fill_manual(values = c( - 'light blue', - 'orange' + scale_fill_manual( + values = c( + "light blue", + "orange" ) # customize the colors - ) + ) ``` @@ -193,9 +206,9 @@ Let's also give it a title and name the axes: ```{r ggplot-titles} p <- p + - ggtitle('GDP per capita in Americas', subtitle = 'Year 2007') + - xlab('Country')+ - ylab('GDP per capita') + ggtitle("GDP per capita in Americas", subtitle = "Year 2007") + + xlab("Country") + + ylab("GDP per capita") # show plot p @@ -209,10 +222,11 @@ Once we are happy with our plot we can save it in a format of our choice. Remember to save it in the dedicated folder. ```{r save-plot, eval=FALSE} -ggsave(plot = p, - filename = here('fig_output','plot_americas_2007.pdf') - ) -# By default, ggsave() saves the last displayed plot, but +ggsave( + plot = p, + filename = here("fig_output", "plot_americas_2007.pdf") +) +# By default, ggsave() saves the last displayed plot, but # you can also explicitly name the plot you want to save ``` @@ -240,15 +254,16 @@ save the data only for Americas: ```{r writing-data, eval=FALSE} gapminder_amr_2007 <- gapminder %>% - filter(year == 2007 & continent == 'Americas') %>% - mutate(country_reordered = fct_reorder(country, gdpPercap), - lifeExpCat = if_else(lifeExp >= mean(lifeExp), 'high', 'low') - ) - -write.csv(gapminder_amr_2007, - here('data_output', 'gapminder_americas_2007.csv'), - row.names=FALSE - ) + filter(year == 2007 & continent == "Americas") %>% + mutate( + country_reordered = fct_reorder(country, gdpPercap), + lifeExpCat = if_else(lifeExp >= mean(lifeExp), "high", "low") + ) + +write.csv(gapminder_amr_2007, + here("data_output", "gapminder_americas_2007.csv"), + row.names = FALSE +) ``` ::::::::::::::::::::::::::::::::::::: keypoints diff --git a/episodes/09-open-and-plot-vector-layers.Rmd b/episodes/09-open-and-plot-vector-layers.Rmd index 22e97d18..e7cc460d 100644 --- a/episodes/09-open-and-plot-vector-layers.Rmd +++ b/episodes/09-open-and-plot-vector-layers.Rmd @@ -141,7 +141,7 @@ Now, let's plot this shapefile. You are already familiar with the `ggplot2` pack ggplot(data = boundary_Delft) + geom_sf(size = 3, color = "black", fill = "cyan1") + labs(title = "Delft Administrative Boundary") + - coord_sf(datum = st_crs(28992)) # displays the axes in meters + coord_sf(datum = st_crs(28992)) # displays the axes in meters ``` ::::::::::::::::::::::::::::::::::::: challenge diff --git a/episodes/10-explore-and-plot-by-vector-layer-attributes.Rmd b/episodes/10-explore-and-plot-by-vector-layer-attributes.Rmd index bf947df7..81f848d1 100644 --- a/episodes/10-explore-and-plot-by-vector-layer-attributes.Rmd +++ b/episodes/10-explore-and-plot-by-vector-layer-attributes.Rmd @@ -5,9 +5,9 @@ exercises: 20 --- ```{r setup, include=FALSE} -library(tidyverse) # tools for wrangling, reshaping and visualizing data -library(here) # managing paths -library(sf) # work with spatial vector data +library(tidyverse) # tools for wrangling, reshaping and visualizing data +library(here) # managing paths +library(sf) # work with spatial vector data boundary_Delft <- st_read("data/delft-boundary.shp") lines_Delft <- st_read("data/delft-streets.shp") @@ -128,20 +128,20 @@ head(point_Delft) We can increase the number of rows with the n argument (e.g., `head(n = 10)` to show 10 rows) until we see at least three distinct values in the leisure column. Note that printing an `sf` object will also display the first 10 rows. ```{r} -head(point_Delft, 10) +head(point_Delft, 10) # you might be lucky to see three distinct values ``` We have our answer (`sports_centre` is the third value), but in general this is not a good approach as the first rows might still have many `NA`s and three distinct values might still not be present in the first `n` rows of the data frame. To remove `NA`s, we can use the function `na.omit()` on the leisure column to remove `NA`s completely. Note that we use the `$` operator to examine the content of a single variable. ```{r} -head(na.omit(point_Delft$leisure)) # this is better +head(na.omit(point_Delft$leisure)) # this is better ``` To show only unique values, we can use the `levels()` function on a factor to only see the first occurrence of each distinct value. Note `NA`s are dropped in this case and that we get the first three of the unique alphabetically ordered values. ```{r} -head(levels(factor(point_Delft$leisure)), n = 3) +head(levels(factor(point_Delft$leisure)), n = 3) # this is even better ``` @@ -161,7 +161,7 @@ names(point_Delft) We can use the `filter()` function to select a subset of features from a spatial object, just like with data frames. Let's select only cycleways from our street data. ```{r} -cycleway_Delft <- lines_Delft %>% +cycleway_Delft <- lines_Delft %>% filter(highway == "cycleway") ``` @@ -175,7 +175,7 @@ nrow(cycleway_Delft) This can be useful, for instance, to calculate the total length of cycleways. ```{r} -cycleway_Delft <- cycleway_Delft %>% +cycleway_Delft <- cycleway_Delft %>% mutate(length = st_length(.)) cycleway_Delft %>% @@ -187,9 +187,10 @@ Now we can plot only the cycleways. ```{r fig.cap="Map of cycleways in Delft."} ggplot(data = cycleway_Delft) + geom_sf() + - labs(title = "Slow mobility network in Delft", - subtitle = "Cycleways" - ) + + labs( + title = "Slow mobility network in Delft", + subtitle = "Cycleways" + ) + coord_sf(datum = st_crs(28992)) ``` @@ -214,7 +215,7 @@ unique(lines_Delft$highway) We extract only the features with the value `motorway`. ```{r} -motorway_Delft <- lines_Delft %>% +motorway_Delft <- lines_Delft %>% filter(highway == "motorway") motorway_Delft @@ -223,8 +224,8 @@ motorway_Delft 2. There are `r nrow(motorway_Delft)` features with the value `motorway`. ```{r} -motorway_Delft_length <- motorway_Delft %>% - mutate(length = st_length(.)) %>% +motorway_Delft_length <- motorway_Delft %>% + mutate(length = st_length(.)) %>% select(everything(), geometry) %>% summarise(total_length = sum(length)) ``` @@ -238,22 +239,23 @@ nrow(motorway_Delft) 4. Plot the motorways. ```{r} -ggplot(data = motorway_Delft) + +ggplot(data = motorway_Delft) + geom_sf(linewidth = 1.5) + - labs(title = "Fast mobility network", - subtitle = "Motorways" - ) + + labs( + title = "Fast mobility network", + subtitle = "Motorways" + ) + coord_sf(datum = st_crs(28992)) ``` 5. Follow the same steps with pedestrian streets. ```{r} -pedestrian_Delft <- lines_Delft %>% +pedestrian_Delft <- lines_Delft %>% filter(highway == "pedestrian") -pedestrian_Delft %>% - mutate(length = st_length(.)) %>% +pedestrian_Delft %>% + mutate(length = st_length(.)) %>% select(everything(), geometry) %>% summarise(total_length = sum(length)) @@ -263,9 +265,10 @@ nrow(pedestrian_Delft) ```{r} ggplot() + geom_sf(data = pedestrian_Delft) + - labs(title = "Slow mobility network", - subtitle = "Pedestrian" - ) + + labs( + title = "Slow mobility network", + subtitle = "Pedestrian" + ) + coord_sf(datum = st_crs(28992)) ``` @@ -286,8 +289,8 @@ If we look at all the unique values of the highway field of our street network w ```{r} road_types <- c("motorway", "primary", "secondary", "cycleway") -lines_Delft_selection <- lines_Delft %>% - filter(highway %in% road_types) %>% +lines_Delft_selection <- lines_Delft %>% + filter(highway %in% road_types) %>% mutate(highway = factor(highway, levels = road_types)) ``` @@ -301,11 +304,13 @@ We can use the defined color palette in ggplot. ```{r} ggplot(data = lines_Delft_selection) + - geom_sf(aes(color = highway)) + + geom_sf(aes(color = highway)) + scale_color_manual(values = road_colors) + - labs(color = 'Road Type', - title = "Road network of Delft", - subtitle = "Roads & Cycleways") + + labs( + color = "Road Type", + title = "Road network of Delft", + subtitle = "Roads & Cycleways" + ) + coord_sf(datum = st_crs(28992)) ``` @@ -321,10 +326,12 @@ line_widths <- c(1, 0.75, 0.5, 0.25) ggplot(data = lines_Delft_selection) + geom_sf(aes(color = highway, linewidth = highway)) + scale_color_manual(values = road_colors) + - labs(color = 'Road Type', - linewidth = 'Road Type', - title = "Mobility network of Delft", - subtitle = "Roads & Cycleways") + + labs( + color = "Road Type", + linewidth = "Road Type", + title = "Mobility network of Delft", + subtitle = "Roads & Cycleways" + ) + scale_linewidth_manual(values = line_widths) + coord_sf(datum = st_crs(28992)) ``` @@ -356,9 +363,10 @@ line_width <- c(0.25, 0.75, 0.5, 1) ggplot(data = lines_Delft_selection) + geom_sf(aes(linewidth = highway)) + scale_linewidth_manual(values = line_width) + - labs(title = "Mobility network of Delft", - subtitle = "Roads & Cycleways - Line width varies" - ) + + labs( + title = "Mobility network of Delft", + subtitle = "Roads & Cycleways - Line width varies" + ) + coord_sf(datum = st_crs(28992)) ``` @@ -372,12 +380,14 @@ ggplot(data = lines_Delft_selection) + Let’s add a legend to our plot. We will use the `road_colors` object that we created above to color the legend. We can customize the appearance of our legend by manually setting different parameters. ```{r fig.cap="Mobility network in Delft using thicker lines than the previous example."} -p1 <- ggplot(data = lines_Delft_selection) + +p1 <- ggplot(data = lines_Delft_selection) + geom_sf(aes(color = highway), linewidth = 1.5) + scale_color_manual(values = road_colors) + - labs(color = 'Road Type') + - labs(title = "Mobility network of Delft", - subtitle = "Roads & Cycleways - Default Legend") + + labs(color = "Road Type") + + labs( + title = "Mobility network of Delft", + subtitle = "Roads & Cycleways - Default Legend" + ) + coord_sf(datum = st_crs(28992)) # show plot @@ -386,8 +396,10 @@ p1 ```{r fig.cap="Map of the mobility network in Delft with large-font and border around the legend."} p2 <- p1 + - theme(legend.text = element_text(size = 20), - legend.box.background = element_rect(linewidth = 1)) + theme( + legend.text = element_text(size = 20), + legend.box.background = element_rect(linewidth = 1) + ) # show plot p2 @@ -411,22 +423,24 @@ levels(factor(lines_Delft$highway)) ``` ```{r} -# First, create a data frame with only roads where bicycles +# First, create a data frame with only roads where bicycles # are allowed -lines_Delft_bicycle <- lines_Delft %>% +lines_Delft_bicycle <- lines_Delft %>% filter(highway == "cycleway") # Next, visualise it using ggplot ggplot(data = lines_Delft) + geom_sf() + - geom_sf(data = lines_Delft_bicycle, - aes(color = highway), - linewidth = 1 - ) + + geom_sf( + data = lines_Delft_bicycle, + aes(color = highway), + linewidth = 1 + ) + scale_color_manual(values = "magenta") + - labs(title = "Mobility network in Delft", - subtitle = "Roads dedicated to Bikes" - ) + + labs( + title = "Mobility network in Delft", + subtitle = "Roads dedicated to Bikes" + ) + coord_sf(datum = st_crs(28992)) ``` @@ -455,7 +469,7 @@ levels(factor(municipal_boundaries_NL$ligtInPr_1)) ```{r} ggplot(data = municipal_boundaries_NL) + geom_sf(aes(color = ligtInPr_1), linewidth = 1) + - labs(title = "Contiguous NL Municipal Boundaries") + + labs(title = "Contiguous NL Municipal Boundaries") + coord_sf(datum = st_crs(28992)) ``` diff --git a/episodes/11-plot-multiple-shape-files.Rmd b/episodes/11-plot-multiple-shape-files.Rmd index 7acb16bf..218bf266 100644 --- a/episodes/11-plot-multiple-shape-files.Rmd +++ b/episodes/11-plot-multiple-shape-files.Rmd @@ -5,9 +5,9 @@ exercises: 5 --- ```{r setup, include=FALSE} -library(tidyverse) # tools for wrangling, reshaping and visualizing data -library(here) # managing paths -library(sf) # work with spatial vector data +library(tidyverse) # tools for wrangling, reshaping and visualizing data +library(here) # managing paths +library(sf) # work with spatial vector data boundary_Delft <- st_read("data/delft-boundary.shp") lines_Delft <- st_read("data/delft-streets.shp") @@ -16,8 +16,8 @@ point_Delft <- st_read("data/delft-leisure.shp") road_types <- c("motorway", "primary", "secondary", "cycleway") road_colors <- c("blue", "green", "navy", "purple") -lines_Delft_selection <- lines_Delft %>% - filter(highway %in% road_types) %>% +lines_Delft_selection <- lines_Delft %>% + filter(highway %in% road_types) %>% mutate(highway = factor(highway, levels = road_types)) ``` @@ -56,17 +56,19 @@ We will create a plot that combines our leisure locations (`point_Delft`), munic To begin, we will create a plot with the site boundary as the first layer. Then layer the leisure locations and street data on top using `+`. ```{r} -ggplot() + - geom_sf(data = boundary_Delft, - fill = "lightgrey", - color = "lightgrey" - ) + - geom_sf(data = lines_Delft_selection, - aes(color = highway), - size = 1 - ) + +ggplot() + + geom_sf( + data = boundary_Delft, + fill = "lightgrey", + color = "lightgrey" + ) + + geom_sf( + data = lines_Delft_selection, + aes(color = highway), + size = 1 + ) + geom_sf(data = point_Delft) + - labs(title = "Mobility network of Delft") + + labs(title = "Mobility network of Delft") + coord_sf(datum = st_crs(28992)) ``` @@ -76,49 +78,60 @@ Next, let’s build a custom legend using the functions `scale_color_manual()` a leisure_colors <- rainbow(15) point_Delft$leisure <- factor(point_Delft$leisure) -ggplot() + - geom_sf(data = boundary_Delft, - fill = "lightgrey", - color = "lightgrey" - ) + - geom_sf(data = lines_Delft_selection, - aes(color = highway), - size = 1 - ) + - geom_sf(data = point_Delft, - aes(fill = leisure), - shape = 21) + - scale_color_manual(values = road_colors, - name = "Road Type" - ) + - scale_fill_manual(values = leisure_colors, - name = "Lesiure Location" - ) + - labs(title = "Mobility network and leisure in Delft") + +ggplot() + + geom_sf( + data = boundary_Delft, + fill = "lightgrey", + color = "lightgrey" + ) + + geom_sf( + data = lines_Delft_selection, + aes(color = highway), + size = 1 + ) + + geom_sf( + data = point_Delft, + aes(fill = leisure), + shape = 21 + ) + + scale_color_manual( + values = road_colors, + name = "Road Type" + ) + + scale_fill_manual( + values = leisure_colors, + name = "Lesiure Location" + ) + + labs(title = "Mobility network and leisure in Delft") + coord_sf(datum = st_crs(28992)) ``` ```{r} ggplot() + - geom_sf(data = boundary_Delft, - fill = "lightgrey", - color = "lightgrey" - ) + - geom_sf(data = lines_Delft_selection, - aes(color = highway), - size = 1 - ) + - geom_sf(data = point_Delft, - aes(fill = leisure), - shape = 22 - ) + - scale_color_manual(values = road_colors, - name = "Line Type" - ) + - scale_fill_manual(values = leisure_colors, - name = "Leisure Location" - ) + - labs(title = "Mobility network and leisure in Delft") + + geom_sf( + data = boundary_Delft, + fill = "lightgrey", + color = "lightgrey" + ) + + geom_sf( + data = lines_Delft_selection, + aes(color = highway), + size = 1 + ) + + geom_sf( + data = point_Delft, + aes(fill = leisure), + shape = 22 + ) + + scale_color_manual( + values = road_colors, + name = "Line Type" + ) + + scale_fill_manual( + values = leisure_colors, + name = "Leisure Location" + ) + + labs(title = "Mobility network and leisure in Delft") + coord_sf(datum = st_crs(28992)) ``` @@ -136,7 +149,7 @@ Modify the plot above. Tell R to plot each point, using a different symbol of sh :::::::::::::::::::::::: solution ```{r} -leisure_locations_selection <- st_read("data/delft-leisure.shp") %>% +leisure_locations_selection <- st_read("data/delft-leisure.shp") %>% filter(leisure %in% c("playground", "picnic_table")) ``` @@ -149,58 +162,67 @@ blue_orange <- c("cornflowerblue", "darkorange") ``` ```{r} -ggplot() + - geom_sf(data = lines_Delft_selection, - aes(color = highway) - ) + - geom_sf(data = leisure_locations_selection, - aes(fill = leisure), - shape = 21 - ) + - scale_color_manual(name = "Line Type", - values = road_colors, - guide = guide_legend(override.aes = list( - linetype = "solid", - shape = NA - )) - ) + - scale_fill_manual(name = "Soil Type", - values = blue_orange, - guide = guide_legend(override.aes = list( - linetype = "blank", - shape = 21, - colour = NA - )) - ) + - labs(title = "Traffic and leisure") + +ggplot() + + geom_sf( + data = lines_Delft_selection, + aes(color = highway) + ) + + geom_sf( + data = leisure_locations_selection, + aes(fill = leisure), + shape = 21 + ) + + scale_color_manual( + name = "Line Type", + values = road_colors, + guide = guide_legend(override.aes = list( + linetype = "solid", + shape = NA + )) + ) + + scale_fill_manual( + name = "Soil Type", + values = blue_orange, + guide = guide_legend(override.aes = list( + linetype = "blank", + shape = 21, + colour = NA + )) + ) + + labs(title = "Traffic and leisure") + coord_sf(datum = st_crs(28992)) ``` ```{r} -ggplot() + - geom_sf(data = lines_Delft_selection, - aes(color = highway), - size = 1 - ) + - geom_sf(data = leisure_locations_selection, - aes(fill = leisure, shape = leisure), - size = 2 - ) + - scale_shape_manual(name = "Leisure Type", - values = c(21, 22) - ) + - scale_color_manual(name = "Line Type", - values = road_colors - ) + - scale_fill_manual(name = "Leisure Type", - values = rainbow(15), - guide = guide_legend(override.aes = list( - linetype = "blank", - shape = c(21, 22), - color = "black" - )) - ) + - labs(title = "Road network and leisure") + +ggplot() + + geom_sf( + data = lines_Delft_selection, + aes(color = highway), + size = 1 + ) + + geom_sf( + data = leisure_locations_selection, + aes(fill = leisure, shape = leisure), + size = 2 + ) + + scale_shape_manual( + name = "Leisure Type", + values = c(21, 22) + ) + + scale_color_manual( + name = "Line Type", + values = road_colors + ) + + scale_fill_manual( + name = "Leisure Type", + values = rainbow(15), + guide = guide_legend(override.aes = list( + linetype = "blank", + shape = c(21, 22), + color = "black" + )) + ) + + labs(title = "Road network and leisure") + coord_sf(datum = st_crs(28992)) ``` diff --git a/episodes/12-handling-spatial-projection-and-crs.Rmd b/episodes/12-handling-spatial-projection-and-crs.Rmd index ff0d77b0..6feab58f 100644 --- a/episodes/12-handling-spatial-projection-and-crs.Rmd +++ b/episodes/12-handling-spatial-projection-and-crs.Rmd @@ -5,12 +5,12 @@ exercises: 5 --- ```{r setup, include=FALSE} -library(tidyverse) # tools for wrangling, reshaping and visualizing data -library(here) # managing paths -library(sf) # work with spatial vector data -library(terra) # work with spatial vector and raster metadata +library(tidyverse) # tools for wrangling, reshaping and visualizing data +library(here) # managing paths +library(sf) # work with spatial vector data +library(terra) # work with spatial vector and raster metadata -leisure_locations_selection <- st_read("data/delft-leisure.shp") %>% +leisure_locations_selection <- st_read("data/delft-leisure.shp") %>% filter(leisure %in% c("playground", "picnic_table")) ``` @@ -50,13 +50,15 @@ country_boundary_NL <- st_read("data/nl-boundary.shp") ```{r} ggplot() + - geom_sf(data = country_boundary_NL, - color = "gray18", - linewidth = 2 - ) + - geom_sf(data = municipal_boundary_NL, - color = "gray40" - ) + + geom_sf( + data = country_boundary_NL, + color = "gray18", + linewidth = 2 + ) + + geom_sf( + data = municipal_boundary_NL, + color = "gray40" + ) + labs(title = "Map of Contiguous NL Municipal Boundaries") + coord_sf(datum = st_crs(28992)) ``` @@ -81,17 +83,20 @@ boundary_Delft <- st_transform(boundary_Delft, 28992) ```{r} ggplot() + - geom_sf(data = country_boundary_NL, - linewidth = 2, - color = "gray18" - ) + - geom_sf(data = municipal_boundary_NL, - color = "gray40" - ) + - geom_sf(data = boundary_Delft, - color = "purple", - fill = "purple" - ) + + geom_sf( + data = country_boundary_NL, + linewidth = 2, + color = "gray18" + ) + + geom_sf( + data = municipal_boundary_NL, + color = "gray40" + ) + + geom_sf( + data = boundary_Delft, + color = "purple", + fill = "purple" + ) + labs(title = "Map of Contiguous NL Municipal Boundaries") + coord_sf(datum = st_crs(28992)) ``` @@ -112,32 +117,36 @@ Create a map of South Holland as follows: ::: solution ```{r} -boundary_ZH <- municipal_boundary_NL %>% +boundary_ZH <- municipal_boundary_NL %>% filter(ligtInPr_1 == "Zuid-Holland") ``` ```{r} ggplot() + - geom_sf(data = boundary_ZH, - aes(color ="color"), - show.legend = "line" - ) + - scale_color_manual(name = "", - labels = "Municipal Boundaries in South Holland", - values = c("color" = "gray18") - ) + - geom_sf(data = boundary_Delft, - aes(shape = "shape"), - color = "purple", - fill = "purple" - ) + - scale_shape_manual(name = "", - labels = "Municipality of Delft", - values = c("shape" = 19) - ) + - labs(title = "Delft location") + - theme(legend.background = element_rect(color = NA)) + - coord_sf(datum = st_crs(28992)) + geom_sf( + data = boundary_ZH, + aes(color = "color"), + show.legend = "line" + ) + + scale_color_manual( + name = "", + labels = "Municipal Boundaries in South Holland", + values = c("color" = "gray18") + ) + + geom_sf( + data = boundary_Delft, + aes(shape = "shape"), + color = "purple", + fill = "purple" + ) + + scale_shape_manual( + name = "", + labels = "Municipality of Delft", + values = c("shape" = 19) + ) + + labs(title = "Delft location") + + theme(legend.background = element_rect(color = NA)) + + coord_sf(datum = st_crs(28992)) ``` ::: @@ -152,9 +161,9 @@ To save a file, use the `st_write()` function from the `sf` package. Although `s ```{r, eval=FALSE} st_write(leisure_locations_selection, - "data/leisure_locations_selection.shp", - driver = "ESRI Shapefile" - ) + "data/leisure_locations_selection.shp", + driver = "ESRI Shapefile" +) ```