将R图左右移动

时间:2014-01-29 23:51:40

标签: r plot

我正在使用plot()创建带有图例的地图,并且由于地图的形状,它与图例重叠。我还在学习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
)

2 个答案:

答案 0 :(得分: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设置开始。


编辑:改编@ Hugh的例子

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

在@ Hugh的答案中,为图表添加空格如下所示:

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
)

答案 1 :(得分:0)

作为一个关闭,您可以更改lim的{​​{1}}参数以创建更多空间。

plot