rlegend-propertiesr-base-graphics

Vertical adjustment of symbols relative to text in legend


Is there a way to vertical align the symbols in a legend relative to the first line of the corresponding text?

plot(table(iris$species))
legend('right',c('A','B','long\ntext'), fill = colors(3))

The labels A and B are vertically aligned to the corresponding symbols, but the symbol corresponding to the the third label (long text with a line-break between the words) is aligned to the middle of this label.

I would prefer if the symbol would be aligned to the first line of the label (i.e. long).


Solution

  • The easiest way is to put a blank line before the text. That way, the first line of text will be aligned with the box:

    pie(table(iris$Species), col = palette.colors(3, "Pastel 1"), cex = 2)
    legend('right',c('A','B',' \nlong\ntext'), fill = palette.colors(3, "Pastel 1"), 
           cex = 2, box.lty = 0, y.intersp = 0)
    

    enter image description here