Removing data outside country map boundary in R

眉间皱痕 提交于 2019-12-05 20:17:09

Since we do not have your data, it is hard to work on your case. But, I'd like to leave a method for you. As long as I can see from your code, you have a data frame called df. You want to create a temporary SpatialPointsDataFrame. Let's call it spdf. You also have polygon data called rwa2, which is also a data frame. If rwa2 comes from a spatial class object (i.e., SpatialPolygonsDataFrame), you want to use it to subset data points staying within the polygons. Let's imagine you have the spatial object called sp.rwa2.

library(sp)
library(ggplot2)

Step1 : Create a SpatialPointsDataFrame using df Make sure that you assign proj4string of sp.rwa2 to spdf. I just have a sample proj4string in this code.

spdf <- SpatialPointsDataFrame(coords = df[, c("Lon", "Lat")], data = df,
                               proj4string = CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))

Step 2: Subset data points that are staying within sf.rwa2.

whatever <- spdf[!is.na(over(spdf, as(sp.rwa2, "SpatialPolygons"))), ]

Step 3: Convert spdf to a data frame

whatever <- as.data.frame(whatever)

Then, you would run your code for ggplot. rwa2 is a data frame.

ggplot() +
geom_polygon(data = rwa2, aes(x = long, y = lat, group = group),
             colour = "black", size = 0.5, fill = "white") +
geom_tile(data = whatever, aes(x = Lon, y = Lat, fill = z), alpha = 0.8) +
labs(title = "State Data", x = "Longitude", y = "Latitude") +
scale_fill_distiller(type = "div", palette = "Spectral") +
theme_bw() +
theme(plot.title = element_text(size = 25, face = "bold"),
      legend.title = element_text(size = 15),
      axis.text = element_text(size = 15),
      axis.title.x = element_text(size = 20, vjust = -0.5),
      axis.title.y = element_text(size = 20, vjust = 0.2),
      legend.text = element_text(size = 10)) +
coord_map()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!