plot multiple shp file on a graph using spplot in R
ggplot2, gis, plot, r
Solution
You can use the `sp.layout` argument in `spplot`. Alternatively, you can use ggplot2. Some example code (untested):
library(ggplot2)
shp1_data.frame = fortify(shp1)
shp1_data.frame$id = "shp1"
shp2_data.frame = fortify(shp2)
shp2_data.frame$id = "shp2"
shp = rbind(shp1_data.frame, shp2_data.frame)
ggplot(aes(x = x, y = y, group = group, col = id), data = shp) + geom_path()
In `ggplot2`, columns in the data are linked to graphical scales in the plot. In this case `x` is the x-coordinate, `y` is the y-coordinate, `group` is a column in the data.frame shp which specifies to which polygon a point belongs, and `col` is the color of the polygon. The geometry I used is `geom_path`, which draws a series of lines based on the polygon input data.frame. An alternative is to use `geom_poly`, which also supports filling the polygon.
Problem
I have 3 shp files representing the house, room, and beds of a house respectively. I need to plot them on a graph using R so that they all overlap with each other. I know that in `plot` function, I can use `line` to plot new lines on top of the existing plot, is there anything equivalent in `spplot`? Thanks.