Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center boxes on top of lines in the legend of a plot?

Tags:

plot

r

legend

I would like to superimpose transparent boxes on top of the lines in the legend.

A small example:

xdata <- 1:7
y1 <- c(1,4,9,16,25,36,49)
y2 <- c(1, 5, 12, 21, 34, 51, 72)
y3 <- c(1, 6, 14, 28, 47, 73, 106 )
y4 <- c(1, 7, 17, 35, 60, 95, 140 )

plot(xdata, y1, type = 'l', col = "red")
lines(xdata, y2, type = 'l', col = "red", lty = 3)
lines(xdata, y3, type = 'l', col = "blue") 
lines(xdata, y4, type = 'l', col = "blue",  lty = 3)

# add legend with lines
legend("topleft", legend = c("Plot 1", "Plot 2", "Plot 3", "Plot 4"),
      lty = c(1,3,1,3), lwd = rep(1.3 ,4),
      col = c("blue", "blue", "red", "red") ,
      pch = rep(NA, 4), cex = 0.8, 
      x.intersp = 0.7, y.intersp = 1.2, bty = 'n')

# add boxes
legend("topleft", legend = c("", "", "", ""), lty = rep(0, 4), 
       col = c(adjustcolor(blues9[3], alpha.f = 0.6), 
               adjustcolor(blues9[3], alpha.f = 0.6), 
               adjustcolor("red", alpha.f = 0.1), 
               adjustcolor("red", alpha.f = 0.1)),
        pch = rep(15, 4), cex = 0.8, pt.cex = rep(2, 4),
        x.intersp = 0.7, y.intersp = 1.2, bty = 'n')

which produces:

enter image description here

As you can see, the boxes are shifted to the left along the lines in the legend.

How can I set the alignment of the boxes, so that they become horizontally centered on top of the line symbols in the legend? Thanks!

like image 940
user321627 Avatar asked Jun 21 '18 04:06

user321627


People also ask

How do I change the position of my legend in R?

Control legend position with legend. You can place the legend literally anywhere. To put it around the chart, use the legend. position option and specify top , right , bottom , or left . To put it inside the plot area, specify a vector of length 2, both values going between 0 and 1 and giving the x and y coordinates.

How do you put a legend in the top right corner in R?

To set the legend on top-right side we can use legend. position="top" and legend. justification="right".

What is PCH in legend in R?

Share. Plot character or pch is the standard argument to set the character that will be plotted in a number of R functions. Explanatory text can be added to a plot in several different forms, including axis labels, titles, legends, or a text added to the plot itself.

What is Cex in legend in R?

legend: Text of the legend. fill: Colors to use for filling the boxes with legend text. col: Colors of lines. bg: It defines background color for the legend box. cex: Used for scaling.


1 Answers

The clue here is to specify the same lwd in both legend calls, i.e. also in the call for the boxes where lty = 0.

Here's a simpler example, with only the arguments relevant for your actual issue:

plot(1)

# lines
legend(x = "topleft",
       legend = c("Plot 1", "Plot 2", "Plot 3", "Plot 4"), bty = 'n',
       lty = c(1, 3), lwd = 1,
       pch = NA,
       col = rep(c("blue", "red"), each = 2))

# boxes
legend(x = "topleft", 
       legend = rep("", 4), bty = "n",
       lty = 0, lwd = 1, # <- lwd = 1  
       pch = 15, pt.cex = 2,
       col = c(rep(adjustcolor(blues9[3], alpha.f = 0.6), 2),
               rep(adjustcolor("red", alpha.f = 0.1), 2)))

enter image description here


If you're fine with a coloured outline of the boxes, one legend call is enough. Just use a pch which accepts pt.bg (here: 22):

plot(1)
legend(x = "topleft",
       legend = c("Plot 1", "Plot 2", "Plot 3", "Plot 4"), bty = 'n',
       lty = c(1, 3), col = rep(c("blue", "red"), each = 2),
       pch = 22, pt.cex = 2, 
       pt.bg = c(rep(adjustcolor(blues9[3], alpha.f = 0.6), 2),
                 rep(adjustcolor("red", alpha.f = 0.1), 2)))

enter image description here

like image 144
Henrik Avatar answered Oct 16 '22 01:10

Henrik