6
library(maptools)
data(wrld_simpl)
plot(wrld_simpl)
box()

This gives me a world map and shows the plentiful white space top and bottom of the map. I would like this space to be gone as I need to print many (~60) maps. I am using knitr for the report in which the maps will be embedded. E.g.

Here is some text.
<<chunk.maps, eval = TRUE>>=
library(maptools)
plot(wrld_simpl)
box()
@
And some more text.

But I don't think this is a knitr question. So:

  1. How can I get rid of the white space?
  2. How can I make sure that the map fills my page from left to right?

I have tried mai, mar, par, but no such luck! I am guessing out.width for the chunk option will give the result for my question 2, but since I am so badly stuck on question 1 I find it hard to tell.

Thank you very much! There is so much to learn out here!

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
Gerit
  • 195
  • 2
  • 14
  • have you tried inserting `par(mar=rep(0,4))` before the call to `plot`? I can't check it with knitr/latex at the moment, but it gets rid of the white space in many R graphics devices. – BenBarnes Nov 12 '12 at 14:42
  • Calling `par(...)` before or after calling `plot` doesn't do the trick, unfortunately. Thank you though. It was worth a try. – Gerit Nov 12 '12 at 20:03

1 Answers1

4

I use ggplot2 for these kinds of maps:

require(ggplot2); theme_set(theme_bw())
wrld_simpl_df = fortify(wrld_simpl)
ggplot(wrld_simpl_df, aes(x = long, y = lat, group = group)) + 
   geom_path() + coord_equal()

enter image description here

This also includes the whitespace you where complaining about. The problem is that the aspect ratio between the x- and y-axis is fixed. So if you choose a square graphics device, that will leave white borders above and below. The solution is to make your graphics device have roughly the same proportions as your plot. Use fig.width and fig.height to do this. See this link for more info. As an illustration, when saving the plot above with the correct proportions:

ggsave("/tmp/plt.png", width = 16, height = 9)

enter image description here

the problem is no longer present.

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
  • With `fig.height` and `fig.width` alone in the chunk options I could change the shape of the graphs. This is what I want. Great! At first my title and legend had been too small, but I fixed that with `par(cex=2)`. Thank you! (And I didn't need to dive into yet another new package for me, i.e. `ggplot2` -phew!) – Gerit Nov 12 '12 at 20:34
  • yes, you need to be careful with the dimension of the plot when you draw maps (e.g. need to set appropriate `fig.height` and `fig.width` so the map fills in the white space); the other approach is to simply leave it alone and crop the plots afterwards (see the [graphics manual](https://github.com/downloads/yihui/knitr/knitr-graphics.pdf) for cropping images) – Yihui Xie Nov 12 '12 at 22:23
  • @Yihui Thank you, I appreciate your contribution. Now that I checked the graphics manual again I remember that I actually tried to follow your instructions, but failed. (I agree that I should have mentioned that, too.) I could try again, if it helps the community when I post the error message. I would open up a new post for it though. Let me know. – Gerit Nov 13 '12 at 13:49