Sf: plot(y, add = TRUE) does not work when previous plot(x) has multiple facets

Created on 18 Feb 2017  路  14Comments  路  Source: r-spatial/sf

Example, building on #221:

image

Most helpful comment

Thanks. add=TRUE does work, try

library(sf)
demo(nc)
plot(st_geometry(nc))
plot(st_geometry(st_centroid(nc)), add = TRUE)

If the first plot concerns an sf object and a legend is printed, do set reset=FALSE in its call.

All 14 comments

I think an informative warning at the very least is needed here. How hard would it be to make harrogate_oac be plotted 3 times in the above example?

Note, this also seems to fail silently:

plot(harrogate_oac[1:3], add = T)

The case of adding to plots that have already a 3x3 or 2x5 layout is more complicated; I see this plot mostly as an exploratory tool, just like printing (the first x records of) a data.frame, or plot(mtcrars): it gives you an idea of what's there. If you want to make such a composite of maps and add something to each of them, best is to do it yourself, i.e. (not tested):

par(mfrow = c(1,3))
plot(harrowgate_oac[1])
# add something to this plot now
plot(harrowgate_oac[2])
# add something else to this plot
# etc

I don't think base plot lets you revisit a particular subplot and add to it after they all have been plotted.

Also, plot.sf now sets mfrow back to its original value, so that a new plot, e.g. hist(x) will fill the full device and not just the first one third.

I think that a bit understanding of how to make composite plots with base plot is needed.

I think asking R to somehow remember mfrow from the previous plot would be useful. In the mean-time I suggest adding a message such as the following if add = TRUE is used:

 message("Note that plots will only be added if a single layer or max.plot = 1 was used in the original plot.")

Nothing is easier than that: if you leave the plotting device in the mfrow=c(3,3) state, it simply keeps that, but it means that plot.sf has a stronger side effect. I find it not only very annoying, but also beginner unfriendly that plotting a 3x3 map matrix would _require_ you to do a par(mfrow=c(1,1)) before the next _normal_ plot. Also, as I said, you can't modify the plots in the 3x3 grid after they're plotted (but only _while_ your plotting them).

Or just

message("See ?sf::plot for plotting multiple layers")

Incidentally, ?sf::plot does not seem to do anything - is there documentation yet for this (I finding this difficult when learning how sp::plot differed from graphics::plot - finding the docs was not easy!):

image

How about using ?plot?

That's what comes up for me when I enter ?plot: 3 help options, one of which (the sf one) does not open anything when I click on it.

When I install normally with R CMD INSTALL, no problem; when I install with devtools::github_install, I get

library(sf)
?plot # select sf method
Error in fetch(key) : 
  lazy-load database '/home/edzer/R/x86_64-pc-linux-gnu-library/3.3/sf/help/sf.rdb' is corrupt
In addition: Warning message:
In fetch(key) : internal error -3 in R_decompress1
> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.1 LTS

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] sf_0.3-5

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.9        digest_0.6.12      withr_1.0.2        grid_3.3.2        
 [5] R6_2.2.0           DBI_0.5-15         magrittr_1.5       units_0.4-3       
 [9] httr_1.2.1         curl_2.3           devtools_1.12.0    tools_3.3.2       
[13] udunits2_0.13      tcltk_3.3.2        memoise_1.0.0.9001 knitr_1.15  

possibly an issue with devtools, any idea @hadley ?

@edzer you sometimes need to restart R after a devtools install because of the way that the documentation is cached

Works indeed! Thanks,

So the final answer is that add=T isn't supported in sf::plot to create multiple layers on the same plot? Sorry if this question is really obvious, I'm having a hard time finding the answer right now either on github or elsewhere. I'm currently trying:

plot(st_geometry(district_bound))
plot(st_geometry(temp.sf[[92]]), cex= .2, col=124 , add=T)

where temp.sf is a sf object from st_as_sf that plots fine on its own. Add=T seems to fail regardless of which plot is attempted to be plotted first. Is it that base R just can't handle this kind of thing and I need to use ggplot2?

Also thanks a ton for developing this package, I've only been using it for a couple hours and it already seems leaps and bounds better than the original sp package!

Thanks. add=TRUE does work, try

library(sf)
demo(nc)
plot(st_geometry(nc))
plot(st_geometry(st_centroid(nc)), add = TRUE)

If the first plot concerns an sf object and a legend is printed, do set reset=FALSE in its call.

Thank you so much! I think in the end I was getting some other strange error, but thanks for clarifying that add=TRUE does work!

Was this page helpful?
0 / 5 - 0 ratings