1)
Merge ESRI shape file with external CSV or data frame to plot the map with CSV/data frame variables
library(maptools)
library(sp)
library(shapefiles)
library(RColorBrewer) # creates nice color schemes
library(classInt) # finds class intervals for continuous variables
#Read files
csvvalues=read.csv(“c:/csv_path ")
shapefile= readShapePoly("c:/shape.shp")
#Merge data by unique ID
shapefile@data <-data.frame(shapefile@data, csvvalues, by="ID")
attach(shapefile@data)
# Define the number of classes to be mapped
nclass <- 5
# Set the color ramp that will be used to plot these classes
cols <- brewer.pal(nclass,"YlGnBu")
# Set the class breakpoints using equal intervals
# Can also use quantiles or natural breaks - see help(classIntervals)
breaks <- classIntervals(Column_name_to_be_mapped, nclass, style="quantile")
# Based on the breakpoints and color ramp, specify a color to plot for each polygon
plotcols <- findColours(breaks, cols)
# Generate the map
plot (shapefile, col=plotcols)
2)
Combine multiple data frames with similar names into a single data frame using matched column names.
Ex. Use gtools library and smart bind function.
library (“gtools”)
df_result <- smartbind(df1,df2)