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 itspdf
. You also have polygon data calledrwa2
, which is also a data frame. Ifrwa2
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 calledsp.rwa2
.Step1 : Create a SpatialPointsDataFrame using
df
Make sure that you assign proj4string ofsp.rwa2
tospdf
. I just have a sample proj4string in this code.Step 2: Subset data points that are staying within sf.rwa2.
Step 3: Convert spdf to a data frame
Then, you would run your code for ggplot.
rwa2
is a data frame.