You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Intro/1_preparations_course.qmd
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -91,6 +91,6 @@ Now we will set some RStudio Global options. Go to *Tools → Global options*.
91
91
92
92
Click on "Ok" to apply the change and close the options menu.
93
93
94
-
[^restore]: We recommend that you start each RStudio session with a blank slate, as recommended by @wickham2017 see [here](https://r4ds.had.co.nz/workflow-projects.html)
94
+
[^restore]: We recommend that you start each RStudio session with a blank slate, as recommended by @wickham2023 see [here](https://r4ds.hadley.nz/workflow-scripts#projects)
95
95
[^saveworkspace]: If we don't restore the workspace at startup, there is no need to save it on exit.
96
96
[^pipe]: Our group has adapted the native pipe operator `|>` to reduce package dependencies. If you use the `magrittr` pipe `%>%` and would like to stick to it, feel free.
Copy file name to clipboardExpand all lines: Intro/2_preparations_project.qmd
+3-5Lines changed: 3 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -4,9 +4,7 @@ As your course assignment (Leistungsnachweis) you will develop a semester projec
4
4
5
5
In fact, we give you the opportunity to track your own movement behavior, generating trajectory data you will subsequently analyze yourself. There are two ways for you to generate your own trajectory data.
6
6
7
-
<!-- During the course, we will give you the opportunity to share your data with the other students in the course, so that the shared data pool for all can grow. -->
8
-
9
-
Please let us know which option you choose by filling out the following [survey](https://moodle.zhaw.ch/mod/choice/view.php?id=1192128) on Moodle.
7
+
Please let us know which option you choose by filling out the following [survey](https://moodle.zhaw.ch/mod/choice/view.php?id=1539895) on Moodle.
10
8
11
9
## Option 1: Tracking App
12
10
@@ -35,7 +33,7 @@ Tracking Methods
35
33
36
34
:::
37
35
38
-
Please check after a view days if the tracking is working properly. If you have any issues during installation or you are not able to track yourself after the installtion please get in contact with [Nils](mailto:rata@zhaw.ch) or [Dominic](mailto:luoe@zhaw.ch).
36
+
Please check after a view days if the tracking is working properly. If you have any issues during installation or you are not able to track yourself after the installtion [please get in contact with us](mailto:terz@zhaw.ch).
39
37
40
38
***If, however, for privacy reasons, you dont want to use a Tracking App we can hand out a GPS tracker to you.***
41
39
@@ -45,4 +43,4 @@ You can use a GPS tracker, provided by our research group. By using a GPS tracke
45
43
46
44
The drawback off this option is, that in order to retreave the data, you need to bring the tracker to us. Also more manual preprocessing is necessary when preparing your data for the project.
47
45
48
-
If you choose this option please send us a quick [E-Mail](mailto:luoe@zhaw.ch) so we can arrange the handover of the GPS tracker. You can get the tracker in our office GC 134 at the [Campus Grüental, Wädenswil](https://goo.gl/maps/xDvGFMBEvwBMdWiz7).
46
+
If you choose this option please send us a quick [E-Mail](mailto:terz@zhaw.ch) so we can arrange the handover of the GPS tracker. You can get the tracker in our office GC 134 at the [Campus Grüental, Wädenswil](https://goo.gl/maps/xDvGFMBEvwBMdWiz7).
Copy file name to clipboardExpand all lines: Week1/2_tasks_and_inputs.qmd
+34-42Lines changed: 34 additions & 42 deletions
Original file line number
Diff line number
Diff line change
@@ -10,16 +10,22 @@ Before starting with the task:
10
10
11
11
1. Make sure you have read and followed the instructions in section [Preparation](#w0-preparation)
12
12
2. In RStudio, open the RStudio Project you created for this week. You can see that you are in an RStudio Project if the project's name is visible next to the little RStudio logo in the top right corner of RStudio (otherwise it will read `Project: (None)`).
13
-
3. Download the wild boar movement data here: [wildschwein_BE.csv](https://moodle.zhaw.ch/mod/folder/view.php?id=1192125)
13
+
3. Download the wild boar movement data from [moodle](https://moodle.zhaw.ch/) (*RStuff* → *Datasets*)
14
14
15
15
16
16
17
17
### Task 1: Import data
18
18
19
19
Move the file `wildschwein_BE.csv` into your project directory and import it into `r` as a `data.frame`. Assign correct column types as necessary and make sure the time zone is set correctly for the date/time column.
20
20
21
-
Note: We recommend using the `readr` package to import data (see @sec-readr). These functions have an underscore in their name, e.g. `read_delim` in oppose to the base `R` functions, which have a period (e.g. `read.delim`). Specifically for the wild boar data, we recommend `read_delim()`.
22
21
22
+
:::{.callout-note}
23
+
24
+
## `read.delim` vs. `read_delim`
25
+
26
+
We recommend using the `readr` package to import data (see @sec-readr). These functions have an underscore in their name, e.g. `read_delim` in oppose to the base `R` functions, which have a period (e.g. `read.delim`).
Notice how `st_as_sf` takes the EPSG code for the `crs =` argument. You can find a lot of useful information on Coordinate Reference Systems (including EPSG Codes, etc.) under [epsg.io](http://epsg.io).
69
77
Let's compare our original `data.frame` with this new `sf` object:
70
78
@@ -77,6 +85,10 @@ wildschwein_BE
77
85
wildschwein_BE_sf
78
86
```
79
87
88
+
:::{.br}
89
+
:::
90
+
91
+
80
92
As you can see, `st_as_sf()` has added some metadata to our dataframe (`Geometry type`, `Dimension`, `Bounding box`, `Geodetic CRS`) and replaced the columns `Lat` and `Long` with a column named `geometry`. Other than that, the new `sf` object is very similar to our original dataframe. In fact, `sf` objects *are* essentially `dataframes`, as you can verify with the function `is.data.frame()`:
Instead of keeping the same data twice (once as a `data.frame`, and once as an `sf` object), we will overwrite the `data.frame` and continue working with the `sf` object from now on. This saves some memory space in `R` and avoids confusion.
102
118
103
119
```{r}
@@ -126,6 +142,7 @@ So what can we do with our new `sf` object that we couldn't before? One example
126
142
```
127
143
128
144
Here's the resulting `sf` object from the operation:
145
+
129
146
```{r}
130
147
#| echo: true
131
148
@@ -137,6 +154,9 @@ wildschwein_BE
137
154
138
155
Transforming from one Coordinate Reference System to another was one operation where we needed an object with a spatial nature. In this way, we were able to use an off-the-shelf function to project the coordinates from one CRS to another. In our next example, we again rely on a spatial function: We want to calculate a [convex hull](https://en.wikipedia.org/wiki/Convex_hull) per Wild boar. And guess what the function for calculating a convex hull is called in `sf`? If you guessed `st_convex_hull()`, you were right!
139
156
157
+
:::{.br}
158
+
:::
159
+
140
160
By default `st_convex_hull()` calculates the convex hull *per feature*, i.e. *per point* in our dataset. This of course makes little sense. In order to calculate the convex hull per animal, we need to convert our point- to multipoint-features where each feature contains all positions of one animal. This is achieved in two steps:
141
161
142
162
First: add a grouping variable to the `sf` object. Note the new grouping variable in the metadata of the `sf` object. Other than that, `group_by` has no effect on our `sf` object.
Second: use `summarise()` to "dissolve" all points into a multipoint object.
155
179
156
180
```{r}
@@ -179,7 +203,7 @@ Using base plot to visualize `sf` objects is easy enough, just try the following
179
203
plot(mcp)
180
204
```
181
205
182
-
But since we use `ggplot` extensively, try and plot the object `mcp` with `ggplot`. Hint: Use the layer`geom_sf()` to add an `sf` object.
206
+
One wildboar is seems to be hidden by one of the others. Try and plot the object `mcp` with `ggplot`, setting the layer's `alpha`
183
207
Note: `ggplot` refuses to use our specified CRS, so we need to force this by specifying `datum = ` in `coord_sf()`. Try it out.
184
208
185
209
```{r}
@@ -190,60 +214,28 @@ Note: `ggplot` refuses to use our specified CRS, so we need to force this by spe
190
214
```
191
215
192
216
193
-
### Input: Importing raster data {#w1-importing-raster}
194
-
195
-
In the next task, we would like to add a background map to our `mcp` object. Download the file here: [pk100_BE.tif](https://moodle.zhaw.ch/mod/folder/view.php?id=1192125)
196
-
To import the file into `R`, we use the package `terra` with the function `rast`.
197
-
198
-
```{r}
199
-
#| echo: true
200
-
201
-
library("terra")
202
-
203
-
pk100_BE <- terra::rast("datasets/pk100_BE.tif")
204
-
205
-
pk100_BE
206
-
```
207
-
208
-
`pk100_BE_2056.tif` is a three layered geotiff File. The above console output shows some metadata including the resolution, extent and the names of our layers (`pk1_1`, `pk1_2`etc). With the default `plot` method, each layer is displayed individually:
209
-
210
-
```{r}
211
-
#| echo: true
212
-
213
-
plot(pk100_BE)
214
-
```
217
+
### Task 5: Making maps with `tmap`
215
218
216
-
With `plotRGB` all three layers are combined into a single image:
217
-
218
-
```{r}
219
-
plotRGB(pk100_BE)
220
-
```
221
-
222
-
### Task 5: Adding a background map
223
-
224
-
There are multiple ways to add a background map in `ggplot`, many require additional packages. This is a good opportunity to get to know a completely different package for creating maps: `tmap` ("thematic map"). This package was developed with a syntax very similar to `ggplot2`, which makes it easy to learn.
219
+
Let's get to know a completely different package for creating maps: `tmap` ("thematic map"). This package was developed with a syntax very similar to `ggplot2`, which makes it easy to learn.
225
220
226
221
```{r}
227
222
#| echo: true
228
223
229
224
library("tmap")
230
225
231
-
tm_shape(pk100_BE) +
232
-
tm_rgb()
226
+
tm_shape(wildschwein_BE) +
227
+
tm_dots(fill = "TierName")
233
228
```
234
229
235
-
As you can see, plotting layers in `tmap` is combined with the `+` sign, just as in `ggplot2`. In `tmap` however, each layer consists of two objects: a `tm_shape()` in which the data is called, and a `tm_*` object in which we define how the data is visualized (`tm_rgb()` states that it is plotted as an RGB Raster Layer). Add the object `mcp` to the plot in this manner. Read [the vignette](https://cran.r-project.org/web/packages/tmap/vignettes/tmap-getstarted.html) if you are having trouble.
230
+
As you can see, plotting layers in `tmap` is combined with the `+` sign, just as in `ggplot2`. In `tmap` however, each layer consists of two objects: a `tm_shape()` in which the data is called, and a `tm_*` object in which we define how the data is visualized (`tm_dots()` states that it is plotted as a point layer).
236
231
232
+
Add the object `mcp` to the plot in this manner. Checkout the [tmap website](https://r-tmap.github.io/tmap/) if you are having trouble.
237
233
238
-
```{r}
239
-
#| file: "solutions/week1/task_5.R"
240
-
#| echo: false
241
234
242
-
```
243
235
244
236
### Task 6: Create an interactive map
245
237
246
-
Rerun the `tmap()...` command from the previous task, but switch the plotting mode to "view"" (`tmap_mode("view")`) beforehand. Omit the raster layer (`pk100_BE`), you won't be needing it.
238
+
Rerun the `tmap()...` command from the previous task, but switch the plotting mode to "view"" (`tmap_mode("view")`) beforehand.
Copy file name to clipboardExpand all lines: Week5/2_tasks_and_inputs.qmd
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@
6
6
# source("01_R_Files/helperfunctions.R")
7
7
```
8
8
9
-
Up to now, we have used a variety of different functions designed by other developers. Sometimes we need to execute an operation multiple times, and most often it is reasonable to write a function to do so. Whenever you have copied and pasted a block of code more than twice, you should consider writing a function [@wickham2017].
9
+
Up to now, we have used a variety of different functions designed by other developers. Sometimes we need to execute an operation multiple times, and most often it is reasonable to write a function to do so. Whenever you have copied and pasted a block of code more than twice, you should consider writing a function [@wickham2023].
10
10
11
11
The first step in writing a function, is picking a name and assigning `<- function(){}` to it.
0 commit comments