Fitting Data into a Map in R
Introduction
Fitting data into a map is an essential step in many geographic information systems (GIS) and spatial analysis applications. In this article, we will explore how to fit data into a map using R and the maptools package.
Background
The maptools package provides functions for working with shapefiles, which are a common format for storing geographic data. Shapefiles can be used to represent features such as boundaries, polygons, and lines on a map. To fit data into a map, we need to merge our data with the shapefile of the region that contains our data.
Reading Shape Files
To get started, we need to read in our shapefile using the readShapePoly() function from the maptools package.
# Packages
library(maptools)
# Reading shape files
estados <- readShapePoly(fn = 'estados_2010.shp')
In this example, we are reading in a shapefile called “estados_2010.shp” into a data frame called estados.
Adding an Order Vector
To ensure that our data is ordered correctly when merging it with the shapefile, we need to add an order vector to the data frame.
# Adding an order vector in the data frame
estados@data$order <- 1:nrow(estados@data)
In this example, we are creating a vector of increasing numbers from 1 to the number of rows in the data frame. This will ensure that our data is ordered correctly when merging it with the shapefile.
Data Preparation
Before merging our data with the shapefile, we need to prepare our data by converting it into a format that can be used with the merge() function.
# Data to fit in the map
teste <- c(0.567072774466371,
-0.144934811865636,
0.595967865326944,
0.479904421295722,
0.251495518688696,
0.195815073242536,
0.548223329774688,
0.463845521774005,
0.272145251828454,
0.580334048486482,
0.649672015127772,
0.584054575484519,
0.671373957229477,
0.436391947145788,
0.522605768431274,
0.641067907529059,
0.570339843565797,
0.381297082036819,
0.474587396032187,
0.462810571821925,
0.558684078070584,
-0.197881941278306,
0.720551728476599,
0.489189630257170,
-0.538560893510226,
0.732280520987057,
0.555364159479124)
# Adding the data vector in the data frame
estados@data$dados <- teste
In this example, we are creating a new column called dados and filling it with our prepared data.
Merging the Data
Now that our data is prepared, we can merge it with the shapefile using the merge() function.
# Merging the data
colorOfdados2 <- data.frame(dados2=levels(estados@data$dados2),
colors=unique(myColorsBlue))
estados@data <- merge(estados@data, colorOfdados2)
In this example, we are creating a new data frame called colorOfdados2 that contains the unique values of our dados2 column and their corresponding colors. We then merge this data frame with the original shapefile using the merge() function.
Plotting the Data
Now that our data is merged with the shapefile, we can plot it using the plot() function.
# Plot
plot(estados, col=as.character(estados@data$colors),
lty=0)
In this example, we are plotting the shapefile with a color gradient based on our prepared data.
Adding Additional Data
To add additional data to the plot, we can use the add() function.
# Add
plot(estados2, add=T) # Destaca as linhas dos estados no mapa
In this example, we are adding a new layer to the plot by plotting the shapefile again with the add=T argument.
Creating a Legend
To create a legend for our plot, we can use the legend() function.
# Legend
color.legend(xl=-70, xr=-60,yb=-25,yt=-26,
legend=c('-0.53','','0.73'),
rect.col=myColorsBlue, gradient='x',
cex=.8, pos=c(1,1,1))
In this example, we are creating a legend that highlights the values of -0.53 and 0.73 on our plot.
Conclusion
Fitting data into a map is an essential step in many GIS and spatial analysis applications. In this article, we explored how to fit data into a map using R and the maptools package. We covered topics such as reading shape files, adding order vectors, preparing data for merging, and creating legends. By following these steps, you can easily merge your data with a shapefile and create a color gradient plot that highlights your data.
Additional Tips
- Make sure to explore the documentation of the
maptoolspackage to learn more about its functions and features. - Use the
ggplot2package to create plots with additional features such as faceting, layering, and customization. - Experiment with different color palettes and gradients to find one that suits your data best.
- Consider using other libraries such as
leafletorshinyto create interactive maps.
References
- “maptools: A package for reading and manipulating map data” by Robin Lovelace
- “ggplot2: Elegant Statistical Graphics in R” by Hadley Wickham
Last modified on 2023-10-26