在R中没有边距的情况下绘制地图 [英] Drawing maps without margins in R

查看:56
本文介绍了在R中没有边距的情况下绘制地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图摆脱使用R中的"maps"包生成的地图的边距.我可以通过设置 par(mar = c(0,0,0,0))并在 map()函数中使用border = 0选项.但是相比具有 mar = c(0,0,0,0)的散点图,仍然有很多额外的空间.这是一些用于生成示例地图的代码,以及用于比较的常规散点图.

I'm trying to get rid of the margins of maps generated using the 'maps' package in R. I get some of the way there by setting par(mar=c(0,0,0,0)) and using the option border=0 in the map() function. But compared to e.g. a scatterplot with mar=c(0,0,0,0) there's still a lot of extra space. Here's some code to generate an example map, as well as a regular scatterplot for comparison.

library(maps)
x <- sample(360, 10)-180
y <- sample(160, 10)-80
x.boundary <- c(-180, 180, 0, 0)
y.boundary <- c(0, 0, -80, 80)

pdf("map.tmp.pdf", width=9, height=4)
par(mar=rep(0,4))
map("world", border=0, ylim=c(-80, 80), fill=TRUE, bg="gray", col="white")
points(x, y, pch=19, col="blue")
points(x.boundary, y.boundary, pch=19, col="red")
# map.axes()
dev.off()

pdf("scatter.tmp.pdf", width=9, height=4)
par(mar=rep(0,4))
plot(x, y, xlim=c(-180, 180), ylim=c(-80, 80), pch=19, col="blue")
points(x.boundary, y.boundary, pch=19, col="red")
dev.off()

如果取消对 map.axes()函数的注释,则可以看到,即使在名义上抑制了边距的情况下,仍为轴保留了空间.

If you uncomment the map.axes() function you can see that even with margins notionally suppressed, space has been reserved for the axes.

任何受到赞赏的想法,多年来一直困扰着我.

Any ideas much appreciated, this has been annoying me for ages.

推荐答案

在map函数中,mar再次被重置(因此不遵循常规设置).您可以在地图功能内设置边距(请参见?map ).这给出了您想要的:

In the map function, mar is reset again (and hence does not follow the general settings). You can set the margins within the map function (see ?map). This gives what you want :

map("world", border=0, ylim=c(-80, 80), fill=TRUE, 
     bg="gray", col="white",mar=rep(0,4))

在旁注中,如果您更改par的常规设置,则可以执行类似的操作

on a sidenote, if you change the general settings of par, you can do something like

oldpar <- par(mar=rep(0,4)
... some plotting ...
par(oldpar)

将参数设置回原始值.如果您编写自己的自定义绘图功能,这将特别有用.

to set the parameters back to the original. This is especially useful if you write your own custom plot functions.

这篇关于在R中没有边距的情况下绘制地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆