Removing data outside country map boundary in R

2019-05-15 02:48发布

I know this is simple but could get this working. I want to remove the excess data points on the map below. How do i do it? Below code gave me the results.

ggplot() +
geom_polygon(data = rwa2, aes(x = long, y = lat, group= group),
             colour = "black", size = 0.5, fill = "white") +
geom_tile(data = df, aes(x = Lon, y = Lat, z = z, fill = z), alpha = 0.8) +
ggtitle("State Data") +
xlab("Longitude") +
ylab("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()

enter image description here

I want to remove all data that are outside the state boundary. Boundary coordinates are obtained from a Rdata file using readRDS, with ID_2 for states and ID_3 for districts so are the names. Guide me here, please.

1条回答
小情绪 Triste *
2楼-- · 2019-05-15 03:41

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()
查看更多
登录 后发表回答