Reputation: 39
I have a sf polygon dataframe with multiple series (T1, T2, T3, all on the same scale: they're observations at different time points). I can plot say T1 with
ggplot(map)+geom_sf(aes(fill=T1))
what I'd like to do is plot all three (T1, T2 and T3) as facets (separate maps) on the same drawing. I'm sure there's a way to do this, but I can't find it. Can anyone tell me how? Thanks!
ADDED: Two additional notes on this question. First, the data structure described above is one that could be plotted using spplot, with the T's being the arguments to spplot's zcol argument. So in this connection, my question amounts to asking how to convert an spplot structure to be usable by geom_sf.
Second, suppose I use sf to read in a shp file for say 20 polygons. I also have a data frame consisting of stacked observations for these same polygons, say for 3 periods, so the dataframe has 60 rows. How do I merge these in order to be usable? Can I just stack 3 copies of the sf structure, and than cbind the dataframe (assuming the rows match up correctly)?
Upvotes: 1
Views: 486
Reputation: 39
At least in one sense this turns out to be very simple. Given a data structure (ds_sp) that can be plotted with spplot, you can just do the following:
ds_sf <- st_as_sf(ds_sp) # convert to sf form
plot(ds_sf[c("T1","T2")]) # plot the desired series
This isn't quite the same as using facet_wrap with ggplot, but at least it gives you something to work with.
ANOTHER LATER ADDITION : As to the longitudinal + facet_wrap issue, the following seems to work:
If necessary, create a data frame (df1) with the longitudinal data (longit), an area indicator (fips) and a time indicator (date) which will be used for faceting, and anything else you may need.
If necessary, create an sf-compatible version of the spatial geometry via st_to_sf, as new_poly . This will be of classes "sf" and "data.frame" and should have a spatial indicator matching fips in df1.
Merge the two: data_new<-df1<-dplyr::inner_join(df1,new_poly,by="fips",all.x=TRUE)
Now produce the plot ggplot(data_new)+geom_sf(aes(fill=longit,geometry=geometry))+facet_wrap(~date) and make adjustments from there.
Upvotes: 1