0

I am trying to plot my study area in R. So far I have downloaded census OA boundary data and I have merged attribute data to the OA I wish to look at.
So lets say from a total of 1,000 OAs. I want to look at only 500 of these OAs, for which I have external dataset. Land is my spatial polygons data frame containing the 1,000s OAs. It has one column which is the OA name "geocode". However, it has its coordinates because is you run:

coordinates(land)

it produces all the coordinates.

Houseprice is a data frame containing house prices and a column with the geocode.

Code for far:

library("sp")
library("GISTools")
library("maptools")
library("spatial")
library("ggplot2")
library("rgdal")

land@data = merge(land@data,houseprice,by.x="geocode", 
by.y="geocode",sort=FALSE)

View(land)

this shows that I have only 500 entries (which is what I want).

plot(land)

this ends up plotting the whole 1,000 OAs instead of just the 500 I want.

What must I do for only the 500 OAs with data to just be plotted? Thanks for help

Sébastien Rochette
  • 6,536
  • 2
  • 22
  • 43
wilga
  • 103
  • 2
  • 7

1 Answers1

0

You can use merge directly on your spatial data as there is a method for that. Then you select only rows for which there are no NA in your added column (called my_col in the example below).
Here you only modified the attributes table but not the geometry

# Merge with external dataset
land2 <- merge(land, houseprice,by.x="geocode", by.y="geocode",sort=FALSE, all.x = TRUE)
# Select only rows of interest
land500 <- land2[which(!is.na(land2$my_col)),]
plot(land500)
Sébastien Rochette
  • 6,536
  • 2
  • 22
  • 43