无论< - spdf [!is.na(over(spdf,as(sp.rwa2,SpatialPolygons))),]
$ c $步骤3:将spdf转换为数据框 >无论< - 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 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()
这篇关于在R中删除国家地图边界外的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!