Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top