2014-01-29 128 views
0

我正在使用plot()来创建带有图例的地图,并且由于地图的形状,它与图例重叠。我仍然在学习R,但是如何将地图稍微向左移动以减少重叠?我确信有一个简单的解决方法,但我无法找到正确的参数。将右侧的R图移动到侧

感谢您的帮助!我是R(和stackoverflow)的新手,所以我不能很遗憾地发布图片。

编辑:下面是我运行代码:

plot(spdfCounties, bg="gray90", col=findColours(ciFisher, colRamp)) 
title("Fisher-Jenks") 
strLegend = paste(
    "$", format(round(ciFisher$brks[-(intClasses + 1)]), big.mark=","), " - ", 
    "$", format(round(ciFisher$brks[-1]), big.mark=","), sep="" 
) 
legMain = legend(
    "topright", legend=strLegend, 
    title="Median Income, 2010", bg="gray", inset=0.02, cex=0.6, 
    fill=colRamp 
) 
+3

你可以发布你用来生成图像的代码吗? –

回答

1

使用mar(保证金)选项par。从?par

mar在所述四个边的情节 要指定的形式c(bottom, left, top, right)的数值载体,其 给出余量的行数。默认值是c(5, 4, 4, 2) + 0.1

所以,如果你的传奇是在右边,让你的右边距较大输入

par(mar = c(5, 4, 4, 8) + 0.1)

一些试验和错误应该能够得到它的权利。


This question有关重置参数值也可能有所帮助。通常,您总是可以使用dev.off()关闭设备,并且新设备将以默认的par设置启动。


编辑:适应@休的例子

x <- runif(1000) 
y <- runif(1000) 
plot(x, y) 
legend('topright', legend = "points") # overlaps points 

par(mar = c(5, 4, 4, 8) + 0.2) 
plot(x, y) 
legend('right', legend = "points", inset = -.3, xpd = T) 
# The correct right margin and inset value will depend 
# on the size of your graphic device. 

调整页边距结果

Extra margin area

添加空白的图形,如@休的答案,长相像这样:

White space inside plot


编辑2

试图从问题适应新的代码。你仍然在使用基本图形'plot函数,所以没有什么特别的地图。但是,我们没有您的数据,所以我们无法真正测试任何内容。 (如果这不起作用---且不论张贴另一个问题之前---你应该看看建议,让reproducible examples。)

dev.off() # to reset par 
par(mar = c(5, 4, 4, 8)) 
plot(spdfCounties, bg="gray90", col=findColours(ciFisher, colRamp)) 
# the margins are set as soon as you call plot() 
title("Fisher-Jenks") 
strLegend = paste(
    "$", format(round(ciFisher$brks[-(intClasses + 1)]), big.mark=","), " - ", 
    "$", format(round(ciFisher$brks[-1]), big.mark=","), sep="" 
) 
legMain = legend(
    "right", # changed the legend to right 
    legend=strLegend, 
    title="Median Income, 2010", 
    bg="gray", 
    inset= -0.3, # negative inset to put it outside of the plotting region 
    xpd = T,  # xpd set to allow plotting outside of the plot region 
    cex=0.6, 
    fill=colRamp 
) 
+0

这是正确的吗?该图例包含在绘图区域内,不会受mar的变化(超出绘图区域)的影响。 – Hugh

+0

@Hugh我们没有任何代码,所以没有理由相信这个传说是在剧情区域内。问题是“如何左右移动情节”,并调整边距。我经常喜欢把这个传说放在绘图区域之外(如果需要,负插入和设置“xpd”的效果很好)。 – Gregor

+0

@Hugh和@shujaa感谢您的回复!你的想法看起来不错,但地图和传说似乎是前后移动/不创造保证金。我已经尝试在'plot()'和'legend()'之前放置'par()',但两者似乎都缩小了图例的范围。有没有不同的问题,因为我的情节是一张地图? –

0

作为一次性的,你可以改变的plotlim参数创造更多空间。

x <- runif(1000) 
y <- runif(1000) 
plot(x,y) 
legend('topright', legend = "points") # overlaps points 

plot(x,y, xlim = c(0, 1.5), ylim = c(0, 1.5) # adds white space 
legend('topright', legend = "points")