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:
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!
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.
To set the legend on top-right side we can use legend. position="top" and legend. justification="right".
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.
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.
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)))
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)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With