[R-sig-Geo] plot raster: how to eliminate surrounding white space?

Agustin Lobo alobolistas at gmail.com
Wed Aug 28 10:10:07 CEST 2013


Interesting thread, but spplot() does not solve the problem of the
excessive white space in multiple plots.
The following
require(lattice)
par(mar = c(0.1, 0.1, 0.1, 0.1))
p1 <- spplot(a[[1]], col=colort,colorkey=FALSE)
p2 <- spplot(a[[2]], col=colort,colorkey=FALSE)
p3 <- spplot(a[[3]], col=colort,colorkey=FALSE)
p4 <- spplot(a[[4]], col=colort,colorkey=FALSE)
print(p1, position = c(0,.5,.5,1),more=T)
print(p2, position = c(.5,.5,1,1),more = T)
print(p3, position = c(0,0,.5,.5),more=T)
print(p4, position = c(0.5,0,1,.5))

is quite the same as
par(mar = c(0.1, 0.1, 0.1, 0.1), mfrow = c(2,2))
for (i in 1:4) plot(a[[i]], col=colort,axes=FALSE,legend=FALSE, asp = 1)

and

print(p1, position = c(0,0,.25,1),more=T)
print(p2, position = c(0.25,0,0.5,1),more = T)
print(p3, position = c(0.5,0,.75,1),more=T)
print(p4, position = c(0.75,0,1,1))

is quite the same as

par(mar = c(0.1, 0.1, 0.1, 0.1), mfrow = c(1,4))
for (i in 1:4) plot(a[[i]], col=colort,axes=FALSE,legend=FALSE, asp = 1)

(and the the two last options provide acceptable results in my case).

I think that the trick is to design a plotting area with the appropriate shape,
and the white space is reduced proportionally. Or being able to get
the plotting area
proportional to the shape of the multiple plot.

But this is getting more into graphics than into spatial issues.

Thanks


On Wed, Aug 28, 2013 at 1:08 AM, Oscar Perpiñan
<oscar.perpinan at gmail.com> wrote:
> Hello,
>
> I think this question asked for the same problem:
> http://stackoverflow.com/questions/17214469/r-crop-raster-data-and-set-axis-limits
>
> My proposal was to use raster::spplot or rasterVis::levelplot.
>
> Best,
>
> Oscar
> Oscar Perpiñán Lamigueiro
> Grupo de Sistemas Fotovoltaicos (IES-UPM)
> Dpto. Ingeniería Eléctrica (EUITI-UPM)
> URL: http://procomun.wordpress.com
> Twitter: @oscarperpinan
>
>
> 2013/8/27 Agustin Lobo <alobolistas at gmail.com>:
>> Michael,
>>
>> On Tue, Aug 27, 2013 at 3:37 PM, Michael Sumner <mdsumner at gmail.com> wrote:
>>> I don't get what you mean by "rectangles are closer", but maybe modify
>>> the aspect ratio?
>>> plot(a,axes=FALSE,legend=FALSE, asp = 0.2)
>> No, that distorts the image. The plot of the actual image is correct.
>> What I want is to reduce the extent of the large white area around the
>> image. The shape of the white area
>> is not proportional to the shape of the image, it is a much wider rectangle.
>>
>>> Otherwise, you can control the margins around each plot directly with
>>> a bit of mixing old-school and new:
>>> par(mar = c(0.1, 0.1, 0.1, 0.1), mfrow = c(2,2))
>>> for (i in 1:4) plot(a[[i]], axes=FALSE,legend=FALSE, asp = 0.2)
>>> I don't think you can control the par run by raster's plot function
>>> directly, but if you stop it setting up the whole page like this it
>>> respects each panel it seems.
>>>
>> Reducing the margins with par() is not enough, the big issue is the
>> large white space surrounding each image.
>> The trick seems to be making the plotting area so that it is more
>> proportional to the shape of the image.
>> Actually, based on your suggestion, this is quite close to what I need:
>> par(mar = c(0.1, 0.1, 0.1, 0.1), mfrow = c(1,4))
>> for (i in 1:4) plot(a[[i]], axes=FALSE,legend=FALSE, asp = 1)
>>
>> But a more general solution would be better.
>>
>> Thanks
>> Agus
>>
>>
>>
>>> On Tue, Aug 27, 2013 at 11:15 PM, Agustin Lobo <alobolistas at gmail.com> wrote:
>>>> I have raster images with many more rows than columns, i.e
>>>> require(raster)
>>>> a <- raster(matrix(200,ncol=256,nrow=3171))
>>>> extent(a) <- c(0,256,0,3177)
>>>>
>>>> and get too much blank space at plotting:
>>>>
>>>> plot(a,axes=FALSE,legend=FALSE)
>>>>
>>>> in particular if I want to display several bands in the same plot:
>>>> a <- stack(a,a*2,a/2,a*3)
>>>> plot(a,axes=FALSE,legend=FALSE)
>>>>
>>>> Is there a way to optimize the plot so that rectangles are closer?
>>>>
>>>> Thanks
>>>>
>>>> Agus
>>>>
>>>> _______________________________________________
>>>> R-sig-Geo mailing list
>>>> R-sig-Geo at r-project.org
>>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>>>
>>>
>>> --
>>> Michael Sumner
>>> Hobart, Australia
>>> e-mail: mdsumner at gmail.com
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list