Showing posts with label R Codes. Show all posts
Showing posts with label R Codes. Show all posts

Wednesday, March 04, 2020

How to remove columns in SpatialPolygonsDataFrame in R?

If you have a spatial polygon data frame (SPDF) and you would want to remove some columns, how do you do it?  You can use R to remove columns in SpatialPolygonsDataFrame .

Use the syntax object_ name[,-(1:10)] to remove columns 1 to 10 or object_name[,-c(1,10)] to drop columns 1 and 10.

require(maptools)

#load shapefile from maptools package
 
w <- readShapeSpatial(system.file("shapes/sample.shp", package="maptools")[1],
                   IDvar="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))

class(xx) #check the object class
 
#[1] "SpatialPolygonsDataFrame"
#attr(,"package")
#[1] "sp"
#remove columns 1 to 10 
ss <- w[,-(1:10)]
head(ss@data) 
#or use column names to remove them 
rem <- c("name1","name2") # list of column names
final <- rem[,!(names(rem) %in% drops)] #remove columns "name1" and "name2"
 

Thursday, February 27, 2020

How to Add Pie Charts on Top of GIS Maps Using R Package

Question: How do you add pie charts on top of GIS shapefile maps using an R code or package? 

Below I list a few of the R code options depending on the package you want to use.

1. mapplots package

Create simple maps; add sub-plots like pie plots to a map or any other plot; format, plot and export gridded data. The package was developed for displaying fisheries data but most functions can be used for more generic data visualization.  Check out this example.


2. rworldmap package

Enables mapping of country level and gridded user datasets.  Check out this example.

3. plotGoogleMaps package 

Plot htm output with Google Maps API and a plot of spatial data as a combination of users' data and Google Maps layers. Ready to use as local htm file or into your own website.  Check out this tutorial.


Wednesday, February 19, 2020

R Code to process vector to raster data fast

Use this R Code to convert and process vector to raster data fast.

Option 1 
# Load libraries
library('raster')
library('rgdal')
library('parallel') 

# Load a SpatialPolygonsDataFrame 
# Load shapefile
Data_sample <- system.file("external/lux.shp")
Data_sample2 <- shapefile(Data_sample) 

# Define RasterLayer object
r.raster <- raster()

# Define raster extent
extent(r.raster) <- extent(Data_sample2)

# Define pixel size
res(r.raster) <- 0.1
# Rasterize
system.time(Data_sample2.r <- rasterize(Data_sample2, r.raster)) 
Option 2
# Calculate the number of cores
num_cores <- detectCores() - 1

# Number of polygons features
features <- 1:nrow(Data_sample2[,])

# Split features in n parts
n <- 20
parts <- split(features, cut(features, n))

# Initiate cluster (after loading all the necessary object to R environment: Data_sample2, parts, r.raster, n)
cl <- makeCluster(num_cores, type = "FORK")
print(cl)

# Parallelize rasterize function
system.time(rParts <- parLapply(cl = cl, X = 1:n, fun = function(x) rasterize(Data_sample2[parts[[x]],], r.raster)))

# Finish
stopCluster(cl)

# Merge all raster parts and plot
Merge_parts <- do.call(merge, rParts)
plot(Merge_parts)

Friday, July 15, 2016

Species Distribution Model SDM Package Tool in R Software

I used the Species Distribution Model SDM Package Tool in R software all the time.

sdm is an object-oriented, reproducible and extensible R platform for species distribution modelling. The sdm package is designed to create a comprehensive modelling and simulation framework that: 

1) provides a standardised and unified structure for handling species distributions data and modelling techniques (e.g. a unified interface is used to fit different models offered by different packages);

2) is able to support markedly different modelling approaches;

3) enables scientists to modify the existing methods, extend the framework by developing new methods or procedures, and share them to be reproduced by the other scientists;

4) handles spatial as well as temporal data for single or multiple species;

5) employs high performance computing solutions to speed up modelling and simulations, and finally;

6) uses flexible and easy-to-use GUI interface. For more information, check the published paper by Naimi and Araujo (2016) in the journal of Ecography.

Download the package here.