R legend pch mix of character and numeric

前端 未结 5 1422
忘掉有多难
忘掉有多难 2020-12-31 04:28

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=\"+\")
l         


        
5条回答
  •  不知归路
    2020-12-31 04:48

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

    `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:

    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)
    
    • This uses strrep to concatenate spaces in order to shift the text over from the "symbols", and it uses rect to retroactively fit a box around the printed legend text.

提交回复
热议问题