Crash course in R – plotting maps Dag Hjermann & Susi Schneider, NIVA Forfatternavn 13.11.2018
Installing R Hopefully already done Otherwise: cran.r-project.org find …. You may in addition install Rstudio Nice window layout Many practical features Is also based on coding Forfatternavn 13.11.2018
Starting R 3 types of windows Console window - Write commands, run each line by ENTER button - Shows results of commands (analyses, tables) Script window(s) - Write commands, run one/several lines by Ctrl-R - Can be saved for later Plot window(s) - For plots Forfatternavn 13.11.2018
Run your first command (in two ways) Run a single command In the command window, write 2+2 and push enter - To change/correct command, push «up arrow» key Run command from script window In the menu: File > Open script In the script window, write 2+2 and push Control-R (or use Edit menu) - Save the script (Menu: File > Save) Forfatternavn 13.11.2018
Storing variables and data In the script window, write x <- 1 y <- 2 x+y Mark all lines and push Control-R R uses the arrow sign <- for storing variables and data sets Forfatternavn 13.11.2018
Installing packages R has built-in commands for simple statistics and graphs Additional functions are in packages Each package contains (usually) several functions (and often example data) Ca. 10 000 packages can be found on cran.r-project.org Each package only has to be installed once (are stored on your computer) Forfatternavn 13.11.2018
Packages we will use here readxl – for reading Excel files ggplot2 – for nice plots ggmap – for plotting on maps 2 ways to install: In the menu: Packages > Install package(s) In the console or script window: install.packages(c("readxl", "ggplot2", "ggmap")) and run it using Control-R In both cases, you will be asked to select the country of the server Forfatternavn 13.11.2018
Make Excel data ready Look at the data set in Excel Make sure of this: First row has names of columns Data starts in row number 2 No empty columns to the left of the data Column names can not contain hyphen (-) or start with a number Column names can contain spaces and ‘foreign’ letters (such as ä, ñ, æ, ø, å, …) - but makes things more difficult Forfatternavn 13.11.2018
Set the working directory Make sure that R “is” in the folder as the data: Either using the menu: File > Change dir… Or using the command: setwd(“C:/Work/R course”) Note that we must use “forward slash” (/) Using backslashes (“C:\Work\R course”) will not work Check - this command should show the data file: dir() Forfatternavn 13.11.2018
Read Excel data First, load the library of the read_excel command into memory… library(readxl) Then, we read the data and store it as “mydata”: mydata <- read_excel("Macrophyte index.xls") Loads the read_excel command into memory What you want to call your data in R (choose freely!) Command The name of your file (if you have set the working directory, you don’t have to say in which folder it is) Forfatternavn 13.11.2018
Read Excel data Note that «mydata» (the «R version» of your data) has now only been loaded into the memory, not saved in an R file The next time you open R, «mydata» will have disappeared …but if you have saved the script, reading the file again is very fast And then you need only one copy of the data Forfatternavn 13.11.2018
Read Excel data – possible errors Error in read_fun(path = path, sheet = sheet, limits = limits, shim = shim, : path[1]="Macrophyte index.xls": The system cannot find the file specified - R often gives confusing error messages, with a lot of extra information you don’t need - In this case, either 1) The name of the file is spelled incorrectly, or 2) You have not set the work directory (R is in the wrong folder) This is the useful information Forfatternavn 13.11.2018
Look at the data, in four ways str(mydata) head(mydata) View(mydata) mydata List of variables, the type of each variable The first rows of the data “Excel-like” window (may not show all columns!) Shows all the data on the screen Forfatternavn 13.11.2018
How to get help in R ? + the command name (with no space between) ?read_excel ?str ?head Often not very well written Alternatives on the web Forfatternavn 13.11.2018
Plot a kind of ‘map’ of the data Loads commands we need (ggplot, geom_point, etc.) library(ggplot2) ggplot(mydata, aes(Elongitude, Nlatitude)) + geom_point() Name of data X and Y variables How to show the data Forfatternavn 13.11.2018
Plot a kind of ‘map’ of the data Forfatternavn 13.11.2018
Kind of ‘map’, with names ggplot(mydata, aes(Elongitude, Nlatitude)) + geom_point() + geom_text(aes(x = Elongitude + 0.005, label=Station), hjust = "left") Add text to the plot Put the text a bit to the East of the points This is the variable we want as tee text Make the text left-adjusted (so it starts to the East of the point) Forfatternavn 13.11.2018
Plot a kind of ‘map’ of the data Forfatternavn 13.11.2018
Plot a real map of the data Loads the qmap command (and others) library(ggmap) map_ohrid <- qmap(c(20.733, 41.031), zoom=11) map_ohrid Center of map (longitude, latitude) Zoom level (3-21) Show the map Forfatternavn 13.11.2018
Plot a real map of the data Forfatternavn 13.11.2018
Satellite map without names map_ohrid <- qmap(location = c(20.733, 41.031), zoom=11, maptype = "satellite") map_ohrid Forfatternavn 13.11.2018
Different map variants Location can also be a place name (or even address) qmap(location = "Bay of Kotor") qmap(location = "Boka Kotorska") qmap(location = "Downing Street 10, London", zoom = 17) Can select different map variants: qmap(location = "Downing Street 10, London", zoom = 17, source = “stamen”) qmap(location = "Lake Ohrid", maptype = "toner-lite", source = "stamen", zoom = 11) map_ohrid Forfatternavn 13.11.2018
Show stations on the map map_ohrid + geom_point(data = mydata, aes(Elongitude, Nlatitude), color = "red") + geom_text(data = mydata, aes(x = Elongitude + 0.007, y = Nlatitude, label=Station), hjust = "left", color = "red") Add points to the map Add station names to the map Forfatternavn 13.11.2018
Show index data on the map map_ohrid + geom_point(data = mydata, aes(Elongitude, Nlatitude, color = Index), size = 4) + geom_text(data = mydata, aes(x = Elongitude + 0.007, y = Nlatitude, label=Station), hjust = "left", color = "red") Show values of the variable ‘Index’ as colours Forfatternavn 13.11.2018
Show index data, better colours map_ohrid + geom_point(data = mydata, aes(Elongitude, Nlatitude, color = Index), size = 4) + geom_text(data = mydata, aes(x = Elongitude + 0.007, y = Nlatitude, label=Station), hjust = "left", color = "red") + scale_color_distiller(palette = "Spectral") Forfatternavn 13.11.2018
Show index data, better colours map_ohrid + geom_point(data = mydata, aes(Elongitude, Nlatitude, color = Index), size = 4) + geom_text(data = mydata, aes(x = Elongitude + 0.007, y = Nlatitude, label=Station), hjust = "left", color = "red") + scale_color_distiller(palette = "Spectral“, direction = 1) Forfatternavn 13.11.2018
Turn color scale around (red = bad) map_ohrid + geom_point(data = mydata, aes(Elongitude, Nlatitude, color = Index), size = 4) + geom_text(data = mydata, aes(x = Elongitude + 0.007, y = Nlatitude, label=Station), hjust = "left", color = "red") + scale_color_distiller(palette = "Spectral“, direction = 1) Forfatternavn 13.11.2018
Scale following eutrophy categories Define categories and their colours Add variable for categories to the data Plot the new variable Macrophyte index Category 1.00 - 2.40 Oligotrophic 2.40 – 2.70 Oligo-mesotrophic 2.70 – 2.95 Mesotrophic 1 2.95 – 3.30 Mesotrophic 2 3.30 – 3.55 Eutrophic 1 3.55 – 3.90 Eutrophic 2 3.90 – 5.00 Eutrophic 3 Forfatternavn 13.11.2018
Scale following eutrophy categories Define categories and their colours categories <- c("oligotrophic", "oligo-mesotrophic", "mesotrophic 1", "mesotrophic 2", "eutrophic 1", "eutrophic 2", "eutrophic 3") colors <- c("darkblue","blue","darkgreen","green", "yellow","orange","red") names(colors) <- categories # the 'link' between categories and colors Test: write “colors” and run (Ctrl + R) Forfatternavn 13.11.2018
Scale following eutrophy categories 2. Add variable for categories to the data mydata$Index_cat <- cut(mydata$Index, c(1, 2.4, 2.7, 2.95, 3.3, 3.55, 3.9, 5), labels = categories), include.lowest = TRUE) 1. In our data set ‘mydata’ we will make a new variable called ‘Index_cat’ 2. This variable is based on the existing variable called ‘Index’ 3. We will make categories (function cut) based on ‘Index’ 4. This is the 8 limits we will use for making the categories 5. This is the 7 names we will give to the categories (saved in the previous slide) Forfatternavn 13.11.2018
Scale following eutrophy categories 3. Make the plot – almost as before map_ohrid + geom_point(data = mydata, aes(Elongitude, Nlatitude, color = Index_cat), size = 4) + geom_text(data = mydata, aes(x = Elongitude + 0.007, y = Nlatitude, label=Station), color = "yellow", hjust = "left") + scale_color_manual(values = colors) 1. We will use our new variable ‘Index_cat’ for colors 2. We will use the colours we saved under the name ‘colors’ Forfatternavn 13.11.2018
Scale following eutrophy categories We are almost happy now, but… …we want all the categories in the legend
Scale following eutrophy categories 3. Make the plot – almost as before map_ohrid + geom_point(data = mydata, aes(Elongitude, Nlatitude, color = Index_cat), size = 4) + geom_text(data = mydata, aes(x = Elongitude + 0.007, y = Nlatitude, label=Station), color = "yellow", hjust = "left") + scale_color_manual(values = colors, drop = FALSE) drop = FALSE means that we will show all categories in the legend – not only those we have in the map Forfatternavn 13.11.2018
Scale following eutrophy categories Result from our hard work
Show index data on the map map_ohrid + geom_point(data = mydata, aes(Elongitude, Nlatitude, color = Index), size = 4) + geom_text(data = mydata, aes(x = Elongitude + 0.007, y = Nlatitude, label=Station), hjust = "left", color = "red") map_ohrid Show values of the variable ‘Index’ as colours Forfatternavn 13.11.2018
For your own data Change the things in red categories <- c("oligotrophic", "oligo-mesotrophic", "mesotrophic 1", "mesotrophic 2", "eutrophic 1", "eutrophic 2", "eutrophic 3") colors <- c("darkblue","blue","darkgreen","green", "yellow","orange","red") names(colors) <- categories mydata$Index_cat <- cut(mydata$Index, c(1, 2.4, 2.7, 2.95, 3.3, 3.55, 3.9, 5), labels = categories)) Forfatternavn 13.11.2018
Show species abundance data on map Read data (and look at the data in R) species_data <- read_excel("Species data.xls") str(species_data) Variable names without spaces (e.g. Chara_aspera) One line with variable names Also includes variables Elongitude, Nlatitude
Pick selection of rows in data Example: pick depth = II subset(species_data, depth %in% "II")
Plotting data subset(species_data, depth == "II") map_both <- qmap(location = c(20.85, 40.97), zoom=10, maptype = "satellite") map_both + geom_point(data = subset(species_data, depth %in% "II"), aes(Elongitude, Nlatitude, size = Cladophora_spec., color = Cladophora_spec.)) + scale_color_distiller(palette = "Spectral") subset(species_data, depth == "II") subset(species_data, depth %in% "II") Select which rows to pick from the data Forfatternavn 13.11.2018
Plotting species data Forfatternavn 13.11.2018