Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
25 views12 pages

Usingrformapmaking Notes

This document provides an introduction to using R and RStudio for map making, explaining how to install and load packages, access geographic datasets, plot maps with base R functions and the ggplot2 package, and import vector and raster map data. Key packages discussed include rgdal, sf, sp, ggplot2, and raster, and the document demonstrates basic operations like subsetting data, converting between sp and sf objects, and adding layers to maps.

Uploaded by

aya996995
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views12 pages

Usingrformapmaking Notes

This document provides an introduction to using R and RStudio for map making, explaining how to install and load packages, access geographic datasets, plot maps with base R functions and the ggplot2 package, and import vector and raster map data. Key packages discussed include rgdal, sf, sp, ggplot2, and raster, and the document demonstrates basic operations like subsetting data, converting between sp and sf objects, and adding layers to maps.

Uploaded by

aya996995
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Using R for Map Making

Erica A. Metheney

2020-02-27

Intro to R
Getting to Know R and RStudio
R and RStudio are separate tools that work together to perform data analysis. You can think
of RStudio as your office workspace where you do your work and R as the computer in your
office that actually runs the computations. This means we will be working directly with
RStudio while R does all the heavy lifting (computing) in the background.
When you open RStudio you will something like Figure 1 (may varying depending on
version and operating system).

This is my caption.
See how the screen is divided into 4 parts:

Saving Your Work


If you want to save your work, either to share it with others or to review on your own later,
you must write your code in a script file. Code written and evaluted in the console will not
be saved. To start a new script file Click the button with the white cross on a green circle
over a piece of paper (See Figure 2 for reference)

Button to open new script file.

Basics of R
In R you can either write your own code or use pre-written code in a package. Packages are
collections of functions written by an R user. There are a number of default packages that
come with the installation of R, but there are numerous additional packages available for
download as well.
While there are many ways to install a package, the easiest way is to go to the Packages tab
in the lower right pane and click the Install button. Type in the name of the package you
want to install and click Install. When you do this you will see a line of code pop up in the
console. You can also use this command directly as shown below.
#install.packages("kSamples")

If the installation was successful you will see the package listed in the Package tab.
To use a package you installed use the command below
library(kSamples)

Note you must run the command after installing a package to use it the first time
Additionally, the command is important because two packages may contain functions with
the same name that perform different operations. The command tells R that you wish to
use the functions as defined in the chosen function. .

Finding Help
If you want to learn more about a package, you can type
??kSamples

See how the help references pop up in the Help tab (depending on your settings a web
browser with the documentation may pop up).

Helpful Packages
The first package we will use to create maps in R is
#if you need to install
#install.packages("rgdal")
#install.packages("sf")
#install.packages("sp")
#install.packages("ggplot2")
#install.packages("spData")
#install.packages("devtools)
#devtools::install_github("Nowosad/spDataLarge")

#import the package


library(rgdal)
library(sf)
library(sp)
library(ggplot2)
library(spData)
library(spDataLarge)
library(raster)

Geographic Data in R
We will begin to get to know the basics of geographic data in R by using the “world” dataset
in the package. First let’s see what information is in the “world” dataset.
#get the names of the variables in the world dataset
names(world)

[1] "iso_a2" "name_long" "continent" "region_un" "subregion"


[6] "type" "area_km2" "pop" "lifeExp" "gdpPercap"
[11] "geom"

If we want to get the summary of an attribute in our dataset we:


summary(world["lifeExp"])

lifeExp geom
Min. :50.62 MULTIPOLYGON :177
1st Qu.:64.96 epsg:4326 : 0
Median :72.87 +proj=long...: 0
Mean :70.85
3rd Qu.:76.78
Max. :83.59
NA's :10

summary(world[c("subregion","lifeExp")])

subregion lifeExp geom


Length:177 Min. :50.62 MULTIPOLYGON :177
Class :character 1st Qu.:64.96 epsg:4326 : 0
Mode :character Median :72.87 +proj=long...: 0
Mean :70.85
3rd Qu.:76.78
Max. :83.59
NA's :10

Note how the geometry variable says even if you do not select it.
If we want to subset the dataset we can use the same syntax as we would use for any
dataframe in R.
world.subset = world[1:4,1:2]

sp or sf Object?
What if I need a spatial oject (“sp”) object not an “sf” object. Luckily we can convert!
world.sp = as(world,Class = "Spatial")

Basic Plotting
plot(world[3:6])

plot(world["pop"])
#[0] will plot only the geometry
#add layers with thereset = FALSE and add = TRUE options
africa = world[world$region_un=="Africa",]
zambia = world[world$name_long == "Zambia",]
plot(africa[0],reset = FALSE)
plot(zambia[0],add=TRUE,col = "green")
Pretty Plotting
In order to make more sophistocated and professional looking maps we will use the
package , specifically the function . Let’s start with an example.
africa.plot = ggplot() +
geom_sf(data = africa, aes(fill = pop))

africa.plot
#compared to

plot(africa["pop"])
Adding Layers
#set the theme of the plots. basic visual. The black and white theme removes
the grey background.
theme_set(theme_bw())

egypt = world[world$name_long=="Egypt",]

ggplot(data = africa) +
geom_sf(color = "black",fill = "green")+
xlab("Longitude")+
ylab("Latitude")+
ggtitle("Map of Africa",subtitle = "Colored in Green")

ggplot(data = africa) +
geom_sf(aes(fill = lifeExp))+
xlab("Longitude")+
ylab("Latitude")+
ggtitle("Map of Africa",subtitle = "Life Expectency")
ggplot(data = africa) +
geom_sf(aes(fill = lifeExp))+
scale_fill_viridis_c(option = "plasma")+
xlab("Longitude")+
ylab("Latitude")+
ggtitle("Map of Africa",subtitle = "Life Expectency")
Importing Data
Vector Data
#read in a shapefile as an sf object

kenya.border = read_sf("//home.gu.gu.se/home-XM$/xmeter/Documents/KenyaBorder
SHP/KEN_adm0.shp")

kenya.bins = read_sf("//home.gu.gu.se/home-XM$/xmeter/Documents/KenyaPolygon
Data/Nairobi_sectors.shp")

Raster Data
library(raster)

kenya.pop = raster("C:/Users/xmeter/Desktop/GeoCourse_R/Session2_data/KenyaPo
pulation/population_ken_2018-10-01.tif")

Basic Plots
Vector
plot(kenya.border[0], reset = FALSE, main = "Plot of Kenya")
plot(kenya.bins[0], add = TRUE, col = "red")
Raster
library(sp)
spplot(kenya.pop)
Pretty Plots
Raster
#kenya.pop.spdf <- as(kenya.pop, "SpatialPixelsDataFrame")
#kenya.pop.df <- as.data.frame(kenya.pop.spdf)

#this takes a long time to render


#ggplot() +
#geom_raster(data = kenya.pop.df , aes(x = x, y = y, fill = #population_ken_
2018.10.01)) +
#coord_quickmap()

References
Good tutorials of the basics:

You might also like