rmf rmf - 3 months ago 33
R Question

R legend pch mix of character and numeric

Is it possible to use a mix of character and number as plotting symbols in R legend?

plot(x=c(2,4,8),y=c(5,4,2),pch=16)
points(x=c(3,5),y=c(2,4),pch="+")
legend(7,4.5,pch=c("+",16),legend=c("A","B")) #This is the problem

Answer

My first thought is to plot the legend twice, once to print the character symbols and once to print the numeric ones:

plot(x=c(2,4,8),y=c(5,4,2),pch=16)
points(x=c(3,5),y=c(2,4),pch="+")
legend(7,4.5,pch=c(NA,16),legend=c("A","B")) # NA means don't plot pt. character 
legend(7,4.5,pch=c("+",NA),legend=c("A","B"))

NOTE: Oddly, this works in R's native graphical device (on Windows) and in pdf(), but not in bmp() or png() devices ...

enter image description here