Unfold day to see schedule
9:00-10:30 Handling and plotting vector data in R
10:30-10:45 Coffee break
10:45-12:00 Handling and plotting cont.
12:00-13:00 Lunch break
13:00-14:30 Spatial operations (intersection, clipping, conversions etc)
14:30-14:45 Coffee break
14:45-16:00 Spatial operations cont.
9:00-10:30 Spatial analysis of vector data (clustering, density surfaces, autocorrelation)
10:30-10:45 Coffee break
10:45-12:00 Spatial analysis cont.
12:00-13:00 Lunch break
13:00-14:15 Visualizing spatial data
14:15-14:30 Coffee break
14:30-16:00 Spatial analysis with R in Puhti supercluster, inc running R code in parallel
9:00-10:30 Raster basics with R
10:30-10:45 Coffee break
10:45-12:00 Raster data manipulation
12:00-13:00 Lunch break
13:00-14:30 Map algebra
14:30-14:45 Coffee break
14:45-16:00 Spatial modelling with raster data
13:00-15:00 Using Puhti supercomputer with R for spatial analysis and code parallization hands-on workshop. Intro to the new Puhti web interface. Optional for course participants, open to everybody.
Zoom link
For exercises we will use RStudio in CSCs Notebook environment, for which you will only need an updated webrowser (Firefox, Chrome or Safari are recommended, others may or may not work)
Launch new
under 'Spatial data analysis with R course'Open in Browser
link shows up and follow that link.rstudio
and password from clipboard.git clone https://github.com/csc-training/r-spatial-course.git
If you want, you can also follow the course in your own RStudio on your computer.
raster, lubridate, paletteer, sf, tidyverse, rmapshaper, spdep, spatstat, mapview, mapedit, fpc, GWmodel, NbClust, osmdata, terra, patchwork, exactextractr, fasterize
.Is there a way to see this r markdown “visualized” in rstudio like we read normal markdown in eg. github?
Permission issues, paths not found that are clearly there?
Warning in CPL_write_ogr(obj, dsn, layer, driver, as.character(dataset_options), : GDAL Error 4: sqlite3_open(../Data/villages.gpkg) failed: unable to open database file Creating dataset ../Data/villages.gpkg failed. Error in CPL_write_ogr(obj, dsn, layer, driver, as.character(dataset_options), : Creation failed
git clone https://github.com/csc-training/r-spatial-course.git
Instead of maps I got plots. What is wrong?
library(sf)
in the consoleNote: if you want to run all code in an Rmd you can go on the triangle button next to 'Run' in the upper right corner of the Rmd script window within RStudio and choose 'Run all', or use the keyboard shortcut
Control + Alt + R
so feature is like an observation and field is like a variable?
I had a weirdish issue: i managed to load the nicely, but now i ran the code again and it says the file doesn't exist. the same code worked 10min ago. :)
st_sf can not be used directly, st_sfc needs to be used first?
When is it necessary to use the package before the tool you are using?
library
command you will need to use library::function
Solutions to the exercises you can now find in https://github.com/csc-training/r-spatial-course/tree/main/Day 1
read_sf("../Data/villages.gpkg") %>%
dplyr::select(vil_id, HumanFootprint, IncomeDependency) %>%
dplyr::filter(IncomeDependency > 50)
villages%>%
mutate(HFP = ifelse( HumanFootprint > mean(HumanFootprint),1,0)) %>%
group_by(HFP) %>%
summarise(mean(HumanFootprint)))
five_villages <- select(villages, vil_id) %>%
dplyr::slice(1:5)
new_point <- st_pint(c(1,1)) %>%
sf_sfc() %>%
st_sf() %>%
mutate(vil_id = 1) %>%
st_set_crs(st_ers(five_villages))
r_bind(five_villages, new_point)
It is good practise to clean workspace (broom symbol in environment panel)
Also helps to restart R (in tabs: Session > restart R) before starting new project/session to make sure everything is removed
I actually got the following error when running code in RMarkdown document: ”Error in plot.xy(xy.coords(x, y), type = type, …) : plot.new has not been called yet”
Error in plot.new() : figure margins too large
par("mar")
par(mar=c(1,1,1,1))
If you want you can also see the plot in separate window (where the plot shows up in the Rmd, right upper corner left most button 'show in new window')
difference between $ and []?
If someone else has problems with knitting second part .Rmd to html: it seems to be out out memory error https://stackoverflow.com/questions/51410248/r-markdown-to-html-via-knitr-pandoc-error-137
I re-launched the notebook, but I don’t remember what was the command to get the material from the git repository.
git clone https://github.com/csc-training/r-spatial-course.git
for plotting multiple plots with ggplot use
|
, egp1 |p2
to have p1 and p2 side by side
guides= "collect"
to get a legend with information from all plots
Solutions to the exercises you can now find in https://github.com/csc-training/r-spatial-course/tree/main/Day 1
# for CSC notebooks replace ".." with "/home/rstudio/r-spatial-course"
villages <- read_sf("../Data/villages.gpkg")
admin <- read_sf("../Data/LAO_adm1.shp")
rivers <- read_sf("../Data/rivers.gpkg")
ggplot() +
geom_sf(data = admin, fill = "transparent") +
geom_sf(data = rivers, lty = 2, col = "darkblue") +
geom_sf(data = villages, aes(size = IncomeDependency))
2
bbox <- admin %>%
dplyr::filter(NAME_1 == "Vientiane_Capital") %>%
st_bbox()
bbox
ggplot() +
geom_sf(data = admin, fill = "transparent") +
geom_sf(data = rivers, lty = 2) +
geom_sf(data = villages, size = 0.1) +
coord_sf(xlim = c(bbox[1],bbox[3]), ylim = c(bbox[2],bbox[4]))
Some general info about CRS (Coordinate Reference System): https://rspatial.org/raster/spatial/6-crs.html
R does not provide any 'on-the-fly' CRS transformation (like QGIS etc)!
You can take a look at the metadata of any variable by typing the variable name in the chunk to be executed
class(variablename)
to get the class information of a variable
Note: library rmapshaper does not currently work in the notebook environment because it is relying on some external library which we could not make work. Sorry for the inconvenience. In normal Desktop RStudio on your computer this should work fine. (Let us know if you encounter any problems)
"Creating regular grids" part was skipped in class
what does reset=FALSE do in the plot? line 265 in D1S3
after villages <- sf::read_sf("/home/rstudio/r-spatial-course/Data/villages.gpkg") %>% sf::st_transform(32648) it gives Error in UseMethod("st_transform") : no applicable method for 'st_transform' applied to an object of class "c('tbl_df', 'tbl', 'data.frame')"
running this: ( vil_sel <- dplyr::filter(basins, HYBAS_ID == 4061080120) %>% sf::st_intersection(villages) ) ends up in the error: Error in geos_op2_geom("intersection", x, y, …) : st_crs(x) == st_crs(y) is not TRUE and I cannot run the rest
Note: https://proj.org/operations/projections/aea.html for exercise
Solutions to the exercises you can now find in https://github.com/csc-training/r-spatial-course/tree/main/Day 1
Note for exercise 1: Use
*"+proj=aea +lat_1=13 +lat_2=22"*
instead of*"+proj=aea"*
read_sf("../Data/LAO_adm1.shp") %>%
st_transform("+proj=aea +lat_1=13 +lat_2=22") %>%
plot()
admin <- read_sf("../Data/LAO_adm1.shp") %>%
select(NAME_1) %>%
st_transform(32648)
intersections <- major_basins %>%
select(MAIN_BAS) %>%
st_intersection(admin) %>%
mutate(area = sf::st_area(geom))
intersections
intersections %>%
group_by(NAME_1) %>%
summarise(dplyr::n())
help(st_area)
help(st_length)
intersections %>%
st_cast("LINESTRING") %>%
mutate(length = st_length(geom))
Spatial operations do not result in new geometries!
Skipped some spatial queries chunks in the course
Solutions to the exercises you can now find in https://github.com/csc-training/r-spatial-course/tree/main/Day 1
Yesterday there was some issue with point sizes in ggplot, today we will learn how to scale the points (use
scale_size_continuous(range=c(0.2,2))
)
Spatial autocorrelation for random points
Why unclass?
How to interpret Moran's?
I got error on plot(vil_voronoi[,"Morans_p"])
Error in h(simpleError(msg, call)) :
error in evaluating the argument 'x' in selecting a method for function 'plot': undefined columns selected
vil_voronoi <- vil_voronoi %>% dplyr::mutate(Morans_I = local_moran$Ii, Morans_p = dplyr::pull(local_moran,5))
My session on the Rstudio Server seems to be jammed because I tried to run something too large. Is there a way to stop a process instead of closing the whole session?
line 68 needed to be adjusted during course: (remove scalefillmanual because of library issues in notebooks) :
# 4 clusters, with a maximum of 100 iterations
clusters <- kmeans(data, 4, iter.max = 100)
voronoi$cluster <- clusters$cluster
plot(voronoi[,"cluster"])
Why do I get different clusters from teacher?
When reasonable to scale coordinates?
can the number of initial values be controlled? to not get so different values when running kmeans multiple times.
ctrl + alt + i to create new chunk in Rmd
gather function creates a so-called long format
for errors regarding cluster must not be duplictaed: code chunks should only be run once!
Spatial regression, do all variables need to be converted to spatial format?
If there is no distance matrix given, what happens?
how do SAR and GWR differ from "simple" linear models (e.g. gls) with a spatial correlation structure (correlation=…)?
Do we get any statistical inference if change in coefficients is significant?
literature suggestions?
arange(vil_id) in beginning of file, to keep the order of villages
For those interested, Jaakko Madetoja's dissertation is dealing with GWR and uncertainty in GWR models. It has a nice introduction to GWR in general, too (page 39 -> ). https://aaltodoc.aalto.fi/bitstream/handle/123456789/29575/isbn9789526078052.pdf?sequence=1&isAllowed=y
find all colorscales:
palettes_c_names
scico
: scientific colorscale that do not hamper interpretation
How you can set same temperature scale for all plots in the stack?
Benefit of using paleteer_c instead of directly using scico?
project can be used with a projection (eg EPSG code) or using other raster as target (project will then reproject the raster to same extent and crs of the raster given)
always try to rather reproject vectors if you have a choice (vectors can be reprojected back, rasters not)
will missing values be problem with mean?
na.rm=TRUE
helpsHow would you make a smoothing window that is determinend by the central cell?
how does the self-made weights and NAs behave with mean?
What was the command for finding help when having cursor over some function?
help("function_name")
or ?function_name
Some information about the scientific colour schemes, like the Turku one we used earlier: https://www.fabiocrameri.ch/colourmaps/
khroma::smooth_rainbow
-a rainbow scheme that is optimized also for colorblind people, not recommended for continuous data, but ok for discrete
A list for additional reading about R and GIS: https://docs.csc.fi/apps/r-env-for-gis/#references
does cv refer to your fuction or of R?
merge instead of left_join?
does here -package work with these notebooks?
How about puting building and testing 50:50?
Raster data and ggplot2; a threat or a possibility?
Do not write below this line!