How to avoid margins with wide spplot?

I am generating graphics with spplot and I have margin problems.

library(sp)
library(grid)
library(gridExtra)

demo(meuse, ask = FALSE, echo = FALSE)
p = spplot(meuse, c("zinc"), aspect=0.1)

tg <- textGrob('title', gp = gpar(fontsize = 14, fontface = "bold"))
sg <- textGrob('subtitle', gp = gpar(fontsize = 10))

grid.newpage()
grid.arrange(tg, sg, p, ncol = 1, heights = unit.c(3*grobHeight(tg), grobHeight(sg), unit(1, "grobheight", data= grob(p))))

When aspect ratio is near 1 it looks almost fine, but the side margins are still annoying: enter image description here

However, in my main use case, ratio is closer to 0.1 and in that case the top and bottom margins are very obvious: enter image description here

How can I take control of that, remove all margins around the spplot and handle it myself with grid.

Upvotes: 2

Views: 85

Answers (1)

see24
see24

Reputation: 1210

The height of the resulting image is determined by the size of the device you are plotting it on so you will have extra white space if your device is taller that needed for example. In this case setting height to and width I can get to a reasonable output

png(width = 10, height = 3, units = "in", res = 100)
grid.arrange(tg, sg, p, ncol = 1, 
             heights = unit.c(3*grobHeight(tg), grobHeight(sg), 
                              unit(1, "grobheight", data= grob(p))))

dev.off()

enter image description here

Upvotes: 1

Related Questions