So, I'm working with a dataset of polygons generated at different levels of sensitivity to error from a citizen science project. I have been trying to visualize how my sensitivity threshold changes results, but in plotting have noticed odd behaviour in ggplot2, inserting polygons where there should be none. The comparison between plotting the native
SpatialPolygonsDataSet and the
tidy generated data frame is particularly telling. What's going on here, and how can I fix it?
I start with reading in the data and doing some light processing
library(tidyverse) library(broom) library(sp) library(spdplyr) library(ggplot2) library(rgdal) library(raster) library(rgeos) library(lubridate) #read in spatialPolygonsDataFrame (shapefile) #from https://www.nceas.ucsb.edu/~byrnes/floating_forests/ff_polys_proj.rds classifications <- readRDS("./ff_polys_proj.rds") classifications <- classifications %>% mutate(scene_timestamp = parse_date_time(scene_timestamp, orders="ymdHMS"), quarter = quarter(scene_timestamp, with_year=TRUE, fiscal_start=11))
(note, this all takes some time as the file is big-ish)
Here's a simple plot of the result focusing on one area filtering down to those polygons with a threshold of 6.
#plot the north coast at a threshold of 6 plot(classifications %>% filter(threshold==6), xlim=c(-124.001673, -122.772273), ylim=c(38.115699,39.176660) )
OK, now, here's a data frame instead
#convert to data frame classifications_df <- classifications %>% tidy classifications@data$id <- rownames(classifications@data) classifications_df <- left_join(classifications_df, classifications@data)
which can then be ggplotted
##ggplot the north coast ggplot() + geom_polygon(data=classifications_df %>% filter(threshold==6), mapping=aes(x=long, y=lat, group=group)) + theme_bw(base_size=14) + xlab("") + ylab("") + xlim(-124.001673, -122.772273) + ylim(38.115699,39.176660)
And now... why are there extra polygons where formerly there were none?