本文介绍了在R中删除国家地图边界外的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这很简单,但可以得到这个工作。我想删除下面地图上的多余数据点。我该怎么做?
下面的代码给了我结果。

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


我想删除状态边界之外的所有数据。
使用 readRDS 从Rdata文件获得边界坐标,其中ID_2表示状态,ID_3表示区域名称。 解决方案

由于我们没有数据,因此很难处理您的案例。但是,我想给你留个办法。只要我能从你的代码中看到,你就有一个名为 df 的数据框。你想创建一个临时的SpatialPointsDataFrame。我们称之为 spdf 。您还有称为 rwa2 的多边形数据,它也是一个数据框。如果 rwa2 来自空间类对象(即SpatialPolygonsDataFrame),您希望将其用于子集中保留多边形的数据点。假设您有一个名为 sp.rwa2 的空间对象。

  library (sp)
library(ggplot2)

第1步:使用<$ c $创建SpatialPointsDataFrame c> df
请确保您将 sp.rwa2 的proj4string分配给 spdf

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

步骤2:保留在sf.rwa2中的子集数据点。

 无论<  -  spdf [!is.na(over(spdf,as(sp.rwa2,SpatialPolygons))),] 
 >无论<  -  as.data.frame(无论)

然后,您会运行ggplot的代码。 rwa2 是数据框。

  ggplot()+ 
geom_polygon(data = rwa2,aes(x = long,y = lat,group = group),
color =black,size = 0.5,fill =white)+
geom_tile = any,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()


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()

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.

解决方案

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 dfMake 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()

这篇关于在R中删除国家地图边界外的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 19:53