问题
I have some questions about maps/shapefiles. I'm not a expert in R, so, to make it easier do understand what I'm going for, I'll enumerate:
1- Identify each grid on the map, and maybe omit some of these grids. 2- Color each grid by values from a data frame
I just made what i'm trying to make using Photoshop to help to illustrate my goal here
I made this map using the 'intersect' function with a shapefile I got from internet and a grid i made using 'rastertoPolygons' function, but I'm not sure if using a .shp is the best way to get what I want, altough it has been the only way I found to do this, since I got lost trying with ggplot2 options (and I'm very familiar with the package)
Any help or suggestion will be awesome.
Sorry if I made a stupid question and sorry for my bad english.
回答1:
If you are asking how you can do this in ggplot, you can pretty easily. If not, can you clarify what you are asking?
You can draw the map of Brazil easily, and use your shapefile either directly, or with some adjustments. Since I don't have your shapefile, I'll use one of my own and you can adjust for yourself. I just made two arbitrary boxes, and labelled them with a field called id
. Your grouping name may be different.
library(ggplot2)
library(maps)
library(rgdal)
brasilia <- borders("world", regions = "Brazil")
brazil <- ggplot() + brasilia + theme_bw() + xlab("Longitude (decimals)") + ylab("Latitude (decimals)") +
theme(panel.border = element_blank(), panel.grid.major = element_line(colour = "grey80"), panel.grid.minor = element_blank()) +
coord_fixed(1.0)
brazil # You can see just the map of Brazil
Next, import your shapefile using rgdal
, which should read all the metadata so you don't have to tell it what projection, etc. Just tell it where it is, and what the shape file name is. See ?readOGR
for help.
shapes <- readOGR(dsn = "C:/foo/GIS/Brazil", layer = "brazil_grid")
brazil_shapes <- brazil + geom_path(data = shapes, aes(x = long, y = lat, group = id), color = "red")
brazil_shapes
Filling them with the colors you want may take the most work, creating a table to map your fill levels to the grids. It looks like this answer can point you in the right direction though. R ggplot2 merge with shapefile and csv data to fill polygons
Here's a good overview of mapping in R. http://eriqande.github.io/rep-res-web/lectures/making-maps-with-R.html
来源:https://stackoverflow.com/questions/50028994/identifying-grids-into-a-map-shapefile