Skip to content

Legend frame error (PR#9236)

2 messages · tgibson at augustcouncil.com, Peter Dalgaard

#
Hello,
The frame drawn around the legend appears to miscalculate when the
x-axis decreases rather than increases.  Pasted below is a simple
testcase.  When the x-axis decreases, the width of the legend frame
appears to be calculated based on the length of the first item, rather
than the maximum length of all items.

Thanks,
-TAG
Todd A. Gibson

line1=dpois(seq(0,50), 10)

par(mfcol=c(2,1))

plot(line1, col="blue", type="l")
legend(x=30,y=0.10, legend=c("binding","transcription","Random Genes"), col=c("red", "blue","black"),lty=c(1,1,2),lwd=c(2,2,1))

plot(line1, col="blue", type="l", xlim=c(50,0))
legend(x=30,y=0.10, legend=c("binding","transcription","Random Genes"), col=c("red", "blue","black"),lty=c(1,1,2),lwd=c(2,2,1))
#
tgibson at augustcouncil.com writes:
More likely, it is calculating the maximum of the endpoints, which
will of course be the one furthest to the left if the x axis is
reversed. (As reordering the legends would have shown you.)

The construction

    if (is.null(text.width))
        text.width <- max(strwidth(legend, units = "user", cex = cex))
    else if (!is.numeric(text.width) || text.width < 0)
        stop("'text.width' must be numeric, >= 0")

seems to be the culprit. max() should be min() in the reversed case.

And if I'm not mistaken, there's an additional issue: If the x axis is
reversed, an explicit text.width setting should in fact be negative.