[R] How to center a horizontal legend with specified 'text.width'

Sascha Wolfer sascha at cognition.uni-freiburg.de
Sun May 12 13:47:44 CEST 2013

Hello dear list,

I just cannot figure out how to do this and didn't find a solution for my specific problem.

I want to place a centered horizontal legend between 4 plots. That's no problem.

The problem is: As soon as I specify the "text.width" parameter to separate the two legend entries, the legend is not centered anymore. I thought the 'xjust' parameter would do what I want, but it doesn't change a thing :(

Please see this minimal example:

# Setting up the layout of plots
layout(matrix(c(1,2,3,3,4,5), nrow = 3, byrow = T), heights = c(1,0.1,1))
par(mar = c(4.3,4,2,1.5))
# plot 1
# plot 2
# legend as plot no.3
par(mar = c(0,0,0,0))
legend(x = "center", lty = c("solid", "dashed"), horiz = T,
      legend = c("Beginning", "End"), lwd = 2, bty = "n", text.width = 0.2)
par(mar = c(4.3,4,2,1.5))
# plot 4
# plot 5

Thanks for your help!
Sascha W.

Sascha Wolfer
Center for Cognitive Science
University of Freiburg

More information about the R-help mailing list