How to reduce white space margins of world map

北城余情 提交于 2020-04-10 09:20:20

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/13343726/how-to-reduce-white-space-margins-of-world-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!