15
Prepared by Volkan OBAN Spatial data and Maps in R: Using R as a GIS Reference: https://pakillo.github.io/R-GIS-tutorial/ Basic packages library ( sp ) # classes for spatial data library ( raster ) # grids, rasters library ( rasterVis ) # raster visualization library ( maptools ) library ( rgeos ) library ( dismo ) library(googleVis) library(rworldmap) library (RgoogleMaps)

R Data Visualization-Spatial data and Maps in R: Using R as a GIS

Embed Size (px)

Citation preview

Page 1: R Data Visualization-Spatial data and Maps in R: Using R as a GIS

Prepared by Volkan OBAN

Spatial data and Maps in R: Using R as a GIS

Reference: https://pakillo.github.io/R-GIS-tutorial/

Basic packages

library(sp) # classes for spatial datalibrary(raster) # grids, rasterslibrary(rasterVis) # raster visualizationlibrary(maptools)library(rgeos)library(dismo)library(googleVis)library(rworldmap)library(RgoogleMaps)

library(dismo)

Page 2: R Data Visualization-Spatial data and Maps in R: Using R as a GIS

mycountry <- gmap("Turkey")

plot(mycountry)

>mycountry <- gmap("Turkey", type = "satellite")> plot(mycountry)

Page 3: R Data Visualization-Spatial data and Maps in R: Using R as a GIS

>mycountry <- gmap("Turkey", type = "satellite",exp=3)

Page 4: R Data Visualization-Spatial data and Maps in R: Using R as a GIS

> plot(mycountry)

Page 5: R Data Visualization-Spatial data and Maps in R: Using R as a GIS

library(RgoogleMaps)> newmap <- GetMap(center = c(41.112185,29.019965), zoom = 10, destfile = "newmap.png", + maptype = "satellite")

Page 6: R Data Visualization-Spatial data and Maps in R: Using R as a GIS

Izmir

library(RgoogleMaps)newmap <- GetMap(center = c(38.423734,27.142826), zoom = 10, destfile = "newmap.png", + maptype = "satellite")

Page 7: R Data Visualization-Spatial data and Maps in R: Using R as a GIS

İstanbul Boğazı-Bosphorus:

newmap1 <- GetMap(center = c(41.046018,29.033891), zoom = 10, destfile = "newmap1.png", maptype = "satellite")

Page 8: R Data Visualization-Spatial data and Maps in R: Using R as a GIS

> tmin <- getData("worldclim", var = "tmin", res = 10) # this will download > # global data on minimum temperature at 10' resolution> tmin1 <- raster(paste(getwd(), "/wc10/tmin1.bil", sep = "")) # Tmin for January> fromDisk(tmin1)[1] TRUE> tmin1 <- tmin1/10 # Worldclim temperature data come in decimal degrees > tmin1 class : RasterLayer dimensions : 900, 2160, 1944000 (nrow, ncol, ncell)resolution : 0.1666667, 0.1666667 (x, y)extent : -180, 180, -60, 90 (xmin, xmax, ymin, ymax)coord. ref. : +proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs data source : in memorynames : tmin1 values : -54.7, 26.6 (min, max)

> plot(tmin1)

> library(gtools)

Page 9: R Data Visualization-Spatial data and Maps in R: Using R as a GIS

> file.remove(paste(getwd(), "/wc10/", "tmin_10m_bil.zip", sep = ""))[1] FALSE> list.ras <- mixedsort(list.files(paste(getwd(), "/wc10/", sep = ""), full.names = T, + pattern = ".bil"))> list.ras # I have just collected a list of the files containing monthly temperature valueslist.ras <- mixedsort(list.files(paste(getwd(), "/wc10/", sep = ""), full.names = T, pattern = ".bil")) list.ras # I have just collected a list of the files containing monthly temperature values

>tmin.all <- stack(list.ras)> tmin.allclass : RasterStack dimensions : 900, 2160, 1944000, 12 (nrow, ncol, ncell, nlayers)resolution : 0.1666667, 0.1666667 (x, y)extent : -180, 180, -60, 90 (xmin, xmax, ymin, ymax)coord. ref. : +proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs names : tmin1, tmin2, tmin3, tmin4, tmin5, tmin6, tmin7, tmin8, tmin9, tmin10, tmin11, tmin12 min values : -547, -525, -468, -379, -225, -170, -171, -178, -192, -302, -449, -522 max values : 266, 273, 277, 283, 295, 312, 311, 312, 300, 268, 267, 268

> tmin.all <- tmin.all/10> plot(tmin.all)

Page 10: R Data Visualization-Spatial data and Maps in R: Using R as a GIS
Page 11: R Data Visualization-Spatial data and Maps in R: Using R as a GIS

> elevation <- getData("alt", country = "Turkey")> x <- terrain(elevation, opt = c("slope", "aspect"), unit = "degrees")> plot(x)

slope <- terrain(elevation, opt = "slope")

Page 12: R Data Visualization-Spatial data and Maps in R: Using R as a GIS

aspect <- terrain(elevation, opt = "aspect") hill <- hillShade(slope, aspect, 40, 270)

plot(hill, col = grey(0:100/100), legend = FALSE, main = "Türkiye")

plot(elevation, col = rainbow(25, alpha = 0.35), add = TRUE)

> library(ggmap)> library(RgoogleMaps)> mapImageData1 <- get_map(location = c(lon =29.019442, lat =41.103783),color = "color",source = "google",maptype = "satellite",zoom = 17)> > ggmap(mapImageData1,extent = "device", ylab = "Latitude",xlab = "Longitude")

Page 13: R Data Visualization-Spatial data and Maps in R: Using R as a GIS

İTÜ (Istanbul Technical University)