0

I'm using plot() to create a map with a legend and because of the shape of the map, it overlaps with the legend. I'm still learning R, but how can I move the map slightly to the left to reduce overlap? I'm sure there's a simple fix, but I was not able to find the right parameter.

Thanks for your help! I'm new to R (and stackoverflow) so I cannot post an image unfortunately.

EDIT: Here's the code that I'm running:

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
)
steve_teb
  • 3
  • 1
  • 4

2 Answers2

1

Use the mar (for margin) options in par. From ?par

mar A numerical vector of the form c(bottom, left, top, right) which gives the number of lines of margin to be specified on the four sides of the plot. The default is c(5, 4, 4, 2) + 0.1.

So, if your legend is on the right, make your right margin bigger by entering

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

Some trial and error should be able to get it right.


This question about resetting par values may also be helpful. In general, you can always do dev.off() to close the device, and a new device will start with the default par settings.


EDIT: Adapting @Hugh's example

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.

Adjusting the margins results in

Extra margin area

Adding white space to the graph, as in @Hugh's answer, looks like this:

White space inside plot


Edit 2

Trying to adapt new code from question. You're still using base graphics' plot function, so nothing should be special about having a map. However, we don't have your data, so we can't really test anything. (If this doesn't work---and regardless before posting another question---you should look at tips for making 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
)
Community
  • 1
  • 1
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Is this right? The legend is contained within the plot area which wouldn't be affected by changes to mar (beyond the plot area). – Hugh Jan 30 '14 at 00:24
  • @Hugh We don't have any code, so there's no reason to believe the legend is inside the plot area. The question is "how to move a plot side to side", and adjusting the margins does that. I often like to put the legend outside the plotting region (negative inset and setting `xpd` if necessary works well). – Gregor Thomas Jan 30 '14 at 05:36
  • @Hugh and @shujaa thanks for the reply! your ideas seem good, but the map and legend appear to move in tandem/not create a margin. I've tried putting `par()` before the `plot()` and `legend()`, but both seemed to shrink the map with the legend. Is there a different issue because my plot is a map? – steve_teb Jan 31 '14 at 00:38
  • @shujaa those suggestions worked perfectly. Thanks for all your help! – steve_teb Jan 31 '14 at 02:24
0

As a one off, you can change the lim arguments of plot to create more space.

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")
Hugh
  • 15,521
  • 12
  • 57
  • 100