Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot legend with two character pch symbols

Tags:

r

legend

I have a map of shapes that are labelled with numbers 1:200. I want to create a legend that decodes these numbers. So I tried (keeping it to 20 for easy reading).

plot(c(1,20), c(0,3), type="n")
xx <- c(0,1,1,0)
thelabels <- paste(LETTERS[1:20], LETTERS[1:20], sep="")
for (i in 1:20){
  polygon(xx, c(0,0,1,1))
  text(mean(xx), 0.5, i)
  xx <- xx + 1
}
legend("topleft", "groups", 
  legend = thelabels, pch=as.character(c(1:20)),
  ncol=4
)

Attempted legend with numbers

However, this doesn't work because pch only admits 1-length strings. How can I create a legend where the key is based on the numbers 1:200 not just the first digit of each? or do something equivalent to forcing pch into accepting multilength strings? (Note that thelabels contains longer text so I can't directly label the shapes with thelabels.)

like image 969
Hugh Avatar asked Nov 15 '25 22:11

Hugh


1 Answers

Here's a total kludge workaround:

plot(c(1,20), c(0,3), type="n")
xx <- c(0,1,1,0)
thelabels <- paste(1:20, " ", LETTERS[1:20], LETTERS[1:20], sep="")
for (i in 1:20){
  polygon(xx, c(0,0,1,1))
  text(mean(xx), 0.5, i)
  xx <- xx + 1
}
legend("topleft", "groups", 
       legend = thelabels, pch="",
       ncol=4)
like image 196
gung - Reinstate Monica Avatar answered Nov 17 '25 17:11

gung - Reinstate Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!