Show two characters for each legend label

Using pch , I can build any character and assign any label in the legend. But how can I build two characters for each legend label? For example, in the chart below, I would like to have β–  β–² ● paired with red versions, so I only have three labels β€œa”, β€œb”, β€œc” displayed in the legend for these six characters. At the moment, it seems that the main plot legend allows me to make only one character for each label:

 plot(rnorm(50),pch=c(15:17),col=1:2) legend('topleft',pch=c(15:17),col=1:2,legend=c("a","b","c"),cex=1.5) 

enter image description here

+6
source share
2 answers

This is not too complicated with lattice , as its key = argument accepts an arbitrary number of columns to be included in the legend.

 library(lattice) myPCH <- 15:17 Data <- rnorm(50) Index <- seq(length(Data)) xyplot(Data ~ Index, pch = myPCH, col=1:2, key = list(space = "right", adj=1, text = list(c("a", "b", "c"), cex=1.5), points = list(pch = myPCH), points = list(pch = myPCH,col=2))) 

I don’t know how to include a legend in the plotting area, but with this kind of plot it seems better to have it outside. ( Edit: @chl in the comments indicates several ways to do this. For example, to draw a key in the lower left corner of the picture, replace space = "right" in the above corner = c(0,0) or x = 0, y=0.2 )

enter image description here

+8
source

As indicated in chl, you can also create an individual legend. The "legend" function invisibly returns the borders of the legend window, as well as the coordinates of the legend text. You can write legend text without characters, and then add characters manually using "dots" in the returned coordinates. This will not require additional graphics packages:

 plot(rnorm(50), pch=c(15:17), col=1:2) # Plot legend text, inset could be used to shift legend text to the right pos <- legend('topleft', legend=c("a","b","c"), cex=1.5) # Plot symbols in two columns, shifted to the left by 3 and 1 respectively points(x=rep(pos$text$x, times=2) - c(3,1), y=rep(pos$text$y, times=2), pch=rep(c(15:17), times=2), col=rep(1:2, times=3)) 

The image produced by the code

+5
source

Source: https://habr.com/ru/post/908975/


All Articles