R legend pch mix of character and numeric


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




5 Answers
5



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 ...


pdf()


bmp()


png()



enter image description here





Hi, Thanks for the reply. Yes This was actually my current solution. But the problem here is that I use smartlegend() from gplots to plot the legends so I dont have to specify coordinates just indicate top/bottom, left or right. In that case, if I plot two legends, they overplot. But thanks :-)
– rmf
Nov 19 '12 at 13:53





@Roy First of all, legend() also takes coordinates as "bottomleft", "bottomright", ... No need to use smartlegend() for that. Second, this solution also overplots two legends. That's what you need to do, as c("+",16) is always converted to a character vector before it's passed to the legend() function.
– Joris Meys
Nov 19 '12 at 14:05



c("+",16)


legend()





If overplotting is an issue: legend(7,4.5,pch=c(NA,16),legend=c("A","B")); legend(7,4.5,pch=c("+",NA),legend=c(" "," "), bty="n") you can just blank all the elements that would overplot in the second call to legend.
– plannapus
Nov 19 '12 at 15:12


legend(7,4.5,pch=c(NA,16),legend=c("A","B")); legend(7,4.5,pch=c("+",NA),legend=c(" "," "), bty="n")


legend



Use the numerical equivalent of the "+" character:


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(43,16),legend=c("A","B"))





For anyone interested, the numerical equivalent of the alphabet starts at pch=65 for A.
– SnowFrog
May 8 '14 at 10:18


pch=65





For completeness... The documentation (?points or [stat.ethz.ch/R-manual/R-devel/library/graphics/html/… ) states: ‘32:127’: ASCII characters. For a list of these, see e.g. [hesa.ac.uk/component/content/article?id=1531]
– bug313
May 30 '15 at 14:13


?points



There are actually numerical equivalents for all symbols!



enter image description here



Source: Dave Roberts



The pch code is the concatenation of the Y and X coordinates of the above plot.


+



Example:


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(43,16),legend=c("A","B"))



I bumped to this issue several time, so I wrote a tiny function below. You can use to specify the pch value, e.g.


pch=c(15:17,s2n("|"))



String to Numeric



As noted in previous answers, you can simply add the numerical equivalent of the numeric and character symbols you want to plot.



However, just a related aside: if you want to plot larger numbers (e.g., > 100) or strings (e.g., 'ABC') as symbols, you need to use a totally different approach based on using text().


text()


`Plot(x,y,dat,type='n') ; text(x,y,labels = c(100,'ABC')



Creating a legend in this case is more complicated, and the best approach I've ever come up with is to stack legends on top of each other and using the legend argument for both the pch symbol and the description:


legend


pch


pchs <- c(100,'ABC','540',sum(13+200),'SO77')

plot(1:5,1:5,type='n',xlim=c(1,5.1))
text(1:5,1:5,labels = pchs)
legend(3.5,3,legend = pchs,bty='n',title = '')
legend(3.5,3,legend = paste(strrep(' ',12),'ID#',pchs),bty='n',title='Legend')
rect(xleft = 3.7, ybottom = 1.5, xright = 5.1, ytop = 3)


strrep


rect



enter image description here






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

java.lang.IllegalArgumentException: Failed to create query method in a JpaRepository