[R-sig-Geo] layout and plot.stars

Edzer Pebesma edzer@pebe@m@ @end|ng |rom un|-muen@ter@de
Tue Mar 15 21:06:57 CET 2022



On 15/03/2022 20:02, Patrick Giraudoux wrote:
> Coming back to the example which initiated the issue, I have found the 
> trick following your hints Edzer, but it makes a strange rational. Here 
> we go:
> 
> nf <- layout(matrix(c(1,2),2,1,byrow = TRUE), c(3,3), c(3,1), TRUE)
> layout.show(nf)
> 
> plot(st_geometry(mydept),col="grey",border="grey90")
> plot(st_geometry(fzone1),col=mypal(12)[1],border=NA,add=TRUE)
> plot(st_geometry(fzone2),col=mypal(12)[2],border=NA,add=TRUE)
> plot(st_rasterize(ztot),col=mypal(12),main="",breaks="equal",key.pos=NULL,reset=FALSE,add=TRUE)
> plot(st_geometry(mydept),col=NA,border="grey90",add=TRUE)
> 
> mybox<-bbox2sf(n=0.5,s=0,w=0,e=10,crs=2154) # crs is just crap here, I 
> do not need it thenafter
> mypoly<-st_make_grid(mybox,n=c(12,1),what="polygons")
> plot(mypoly,col=mypal(12),border=mypal(12))
> plot(mybox,add=TRUE)
> 
> Makes exactly what I was intending to make. The critical point was to 
> explicitely keep reset=FALSE in the 4th plot (actually a plot.stars). If 
> not, the 6th plot is plotted in region #1 erasing the previous plots...
> 
> Some personal remarks:
> - in the first plot, I ommited the argument reset=FALSE, however this 
> does not make a problem (maybe because I just plot a geometry ?)

Yes, see ?plot.sf : reset is only an argument for plot.sf, not plot.sfc_XXXX

> - the 4th plot is definitely strange with reset=FALSE and add=TRUE 
> together, isn't it ?

Can't see it - if you want me to look at it, please provide a reprex.

> 
> Thanks Roger and Ezder for bailing me out once again... :-)
> Patrick
> 
> 
> 
> Le 15/03/2022 à 16:02, Edzer Pebesma a écrit :
>> In addition to what Roger wrote, you can use e.g.
>>
>> layout(matrix(1:4,2))
>> library(stars)
>> s = st_as_stars(L7_ETMs)
>> image(s[,,,1])
>> plot(s[,,,1], key.pos = NULL, reset = FALSE)
>> plot(s[,,,1], key.pos = NULL, reset = FALSE, main = NULL)
>>
>> to fill the sub-plots of layout() incrementally. Note the key.pos and 
>> reset arguments to plot.stars(): they make sure plot.stars doesn't 
>> mess with the layout settings.
>>
>> On 15/03/2022 11:45, Patrick Giraudoux wrote:
>>> Great ! Thanks Roger.  On this basis, I have a way to explore the issue
>>> now. Will give a feed-back on the list once done.
>>> Best,
>>> Patrick
>>>
>>> Le 15/03/2022 à 11:27, Roger Bivand a écrit :
>>>> On Tue, 15 Mar 2022, Patrick Giraudoux wrote:
>>>>
>>>>> Hi,
>>>>>
>>>>> I have a trouble with the combination of layout and plot.stars. e.g.
>>>>>
>>>>> nf <- layout(matrix(c(1,2),2,1,byrow = TRUE), c(3,3), c(3,1), TRUE)
>>>>> layout.show(nf)
>>>>>
>>>>> plot(st_geometry(mydept),col="grey",border="grey90")
>>>>> plot(st_rasterize(ztot),col=mypal(12),main="",breaks="equal",add=TRUE)
>>>>
>>>> You have already noticed that sf and stars, like raster and terra,
>>>> modify the assumptions of base plot methods, as
>>>> graphics::filled.contour(), unless some ordering and argument
>>>> conditions are met, crucially the non-base reset= argument. I do not
>>>> think that you can use layout() at all.
>>>>
>>>> library(stars)
>>>> nc <- st_read(system.file("gpkg/nc.gpkg", package="sf"))
>>>> bir74_rast <- st_rasterize(nc["BIR74"])
>>>> plot(bir74_rast, reset=FALSE)
>>>> plot(st_geometry(nc),border="grey90", add=TRUE)
>>>> gridGraphics::grid.echo()
>>>> library(grid)
>>>> g <- grid.grab()
>>>> plot(nc["BIR74"])
>>>> gridGraphics::grid.echo()
>>>> gv <- grid.grab()
>>>> grid.newpage()
>>>> gridExtra::grid.arrange(g, gv, ncol=2)
>>>>
>>>> Grabbing the base graphics device state lets you use
>>>> gridExtra::grid.arrange() to place multiple graphics objects; here I
>>>> haven't tried to constrain aspect or relative sizes. I don't think
>>>> that the plot methods in sf and stars play well with layout, because
>>>> they use it themselves internally.
>>>>
>>>> Hope this helps,
>>>>
>>>> Roger
>>>>
>>>>>
>>>>> plot(mypoly,col=mypal(12),border=mypal(12))
>>>>>
>>>>> I expect that the first two plots display in region #1, the second
>>>>> added to the first, and the third plot in region #2. However, this is
>>>>> not what happens: actually, the third plot displays in region 1
>>>>> erasing the others. I understand that plot.stars when not "added"
>>>>> does not respect the layout definition (and displays its own
>>>>> regions), and that my problem comes from the way plot.stars deals
>>>>> with that.
>>>>>
>>>>> Has anyone an idea about a workaround ?
>>>>>
>>>>> _______________________________________________
>>>>> R-sig-Geo mailing list
>>>>> R-sig-Geo using r-project.org
>>>>> https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-sig-geo&data=04%7C01%7CRoger.Bivand%40nhh.no%7Cc67e7b9e4d6b43cb453c08da065345c7%7C33a15b2f849941998d56f20b5aa91af2%7C0%7C0%7C637829252131764014%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=IDZZmUAOMmrU8cwbsARqkHTry%2BA5v%2FWpB2JzEmEcc7U%3D&reserved=0 
>>>>>
>>>>>
>>>>>
>>>>
>>>
>>>     [[alternative HTML version deleted]]
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo using r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
> 

-- 
Edzer Pebesma
Institute for Geoinformatics
Heisenbergstrasse 2, 48151 Muenster, Germany
Phone: +49 251 8333081



More information about the R-sig-Geo mailing list