Crash course in R – plotting maps

Slides:



Advertisements
Similar presentations
Setting up Articles Throughout this slide show there will be hyperlinks (highlighted in blue) follow the hyperlinks to navigate to the specified Topic.
Advertisements

 Use the Left and Right arrow keys or the Page Up and Page Down keys to move between the pages. You can also click on the pages to move forward.  To.
© Paradigm Publishing, Inc Excel 2013 Level 1 Unit 1Preparing and Formatting a Worksheet Chapter 1Preparing an Excel Workbook.
Loading Excel Double click the Excel icon on the desktop (if you have this) OR Click on Start All Programs Microsoft Office Microsoft Office Excel 2003.
Click your mouse for next slide Dreamweaver – Merging, Coloring, Fonts Now it’s time to fill your page with some more interesting stuff The first thing.
With Alex Conger – President of Webmajik.com FrontPage 2002 Level I (Intro & Training) FrontPage 2002 Level I (Intro & Training)
Give it, Live it, Doing the Pivot Louise Cape, James Colombo, Katie McDonald, Tammy Rowland.
Introduction to VBA. This is not Introduction to Excel We’re going to assume you have a basic level of familiarity with Excel If you don’t, or you need.
Using Dreamweaver. Slide 1 Dreamweaver has 2 screens that do different things The Document window where you create your WebPages The Site window where.
Create your first presentation Proof, print, and prep for the show You’ve learned the essentials for creating a slide show. Now imagine that your presentation.
Lesson No:9 MS-Word Tools, Mail Merge and working with Tables CHBT-01 Basic Micro process & Computer Operation.
Using FrontPage Express. Slide 1 Standard toolbars and menus Time indicator: gives an approximation of download time of the page.
Mail merge I: Use mail merge for mass mailings Perform a complete mail merge Now you’ll walk through the process of performing a mail merge by using the.
Fall 2005 Using FrontPage to Enhance Blackboard - Darek Sady1 Using FrontPage to Enhance Blackboard 1.Introduction 2.Starting FrontPage 3.Creating Documents.
Software. Generic Software  e.g. word processing, spreadsheet and database. – This simply implies that any of the dozens of spreadsheet packages, for.
Downloading and Installing Autodesk Revit 2016
ISU Basic SAS commands Laboratory No. 1 Computer Techniques for Biological Research Animal Science 500 Ken Stalder, Professor Department of Animal Science.
Sports Website Creation. In this project you will design and produce your own website.
WINKS 7 Tutorial 7 – Advanced Topic: Labels and Formats Permission granted for use for instruction and for personal use. © Alan C. Elliott,
Downloading and Installing Autodesk Inventor Professional 2015 This is a 4 step process 1.Register with the Autodesk Student Community 2.Downloading the.
1 Data Manipulation (with SQL) HRP223 – 2010 October 13, 2010 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
11/25/2015Slide 1 Scripts are short programs that repeat sequences of SPSS commands. SPSS includes a computer language called Sax Basic for the creation.
1. 2 Word Processing Word Processing is writing words and sentences on the computer. It is easy to change or move text in a word document. People use.
Introduction to R Introductions What is R? RStudio Layout Summary Statistics Your First R Graph 17 September 2014 Sherubtse Training.
MySQL Getting Started BCIS 3680 Enterprise Programming.
Mapping local community assets online Read this if you want to learn how to: 1)Create online maps of local community assets using Google Maps 2)Allow other.
Mail merge I: Use mail merge for mass mailings Overview: Mailings en masse What if you need to send to each of your employees a letter containing personal.
If you don’t have Google Earth downloaded already, you can go to to get it.
1 Data Manipulation (with SQL) HRP223 – 2009 October 12, 2009 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
Downloading and Installing GRASP-AF Workshop Ian Robson Information Analyst, North of England Cardiovascular Network.
Perform a complete mail merge Lesson 14 By the end of this lesson you will be able to complete the following: Use the Mail Merge Wizard to perform a basic.
Welcome to the Basic Microsoft Word Guide. Before you start this Guide, you will need to complete “Basic Computer”; “Basic Windows” and know how to type.
THE GUIDE TO THE ‘NEW LOOK’ CLASSROOM PROFILING DATABASE
Mail Merge Introduction to Word Processing ITSW 1401 Instructor: Glenda H. Easter Introduction to Word Processing ITSW 1401 Instructor: Glenda H. Easter.
How to map your ringing data
A step-by-Step Guide For labels or merges
Setting up Categories, Grading Preferences and Entering Grades
Learning the Basics – Lesson 1
GO! with Microsoft Access 2016
GeoLyx.com Unleashing the Power of Geographic Analytics.
Using Excel with Google Maps
Tutorial 1 – Creating a Document
Saving, Modifying page, grammar & spell checking, and printing
An Introduction to Using
Microsoft Windows 2000 Professional
ECONOMETRICS ii – spring 2018
Macrosystems EDDIE: Getting Started + Troubleshooting Tips
Windows Internet Explorer 7-Illustrated Essentials
Chapter 1: Introduction to SAS
How to Run a Java Program
Using Charts in a Presentation
Crash course in R – short introduction
Key Applications Module Lesson 12 — Word Essentials
Code is on the Website Outline Comparison of Excel and R
This is where your title of your presentation will typically go.
Making Objects Move in Unison: Using Lists
Macrosystems EDDIE: Getting Started + Troubleshooting Tips
How to Start This PowerPoint® Tutorial
Stata Basic Course Lab 2.
CSCI N207 Data Analysis Using Spreadsheet
Windows Installation Tutorial
Using FrontPage Express
Macrosystems EDDIE: Getting Started + Troubleshooting Tips
Key Applications Module Lesson 12 — Word Essentials
creating a ecosystems model in net logo
Review of Previous Lesson
HIBBs is a program of the Global Health Informatics Partnership Learning the Basics of Microsoft Word 2019 and Microsoft office support TFN
Macrosystems EDDIE: Getting Started + Troubleshooting Tips
Hardware is… Software is…
Presentation transcript:

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