如何减少世界地图的空白边距

Ger*_*rit 5 r ggplot2 knitr r-maptools

library(maptools)
data(wrld_simpl)
plot(wrld_simpl)
box()
Run Code Online (Sandbox Code Playgroud)

这给了我一张世界地图,并显示了地图顶部和底部的丰富空白区域.我希望这个空间消失,因为我需要打印许多(~60)地图.我正在使用knitr嵌入地图的报告.例如

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

但我认为这不是一个knitr问题.所以:

  1. 我怎样才能摆脱白色空间?
  2. 如何确保地图从左到右填充我的页面?

我已经试过mai,mar,par,但没有这样的运气!我猜测out.widthchunk选项会给出我的问题2的结果,但由于我在问题1上如此严重,我觉得很难说.

非常感谢你!这里有很多值得学习的地方!

Pau*_*tra 4

我用于ggplot2这些类型的地图:

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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这还包括您抱怨的空白。问题在于 x 轴和 y 轴之间的纵横比是固定的。因此,如果您选择方形图形设备,则会在上方和下方留下白色边框。解决方案是使您的图形设备具有与绘图大致相同的比例。使用fig.widthfig.height来执行此操作。请参阅此链接了解更多信息。作为说明,当使用正确的比例保存上面的图时:

ggsave("/tmp/plt.png", width = 16, height = 9)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

问题不再存在。