Home > other >  Special symbols in legend in R
Special symbols in legend in R

Time:06-06

I want to write special symbols in the legend in a plot, like 5<# of events<=20. So far I've tried multiple codes but didn't work.

I don't know why this doesn't work bquote(5<~"# of events"~<=20)

I finally made this work (in a clumsy way): legend_txt <- c(bquote('5 < # of counties' <= 20), bquote('20 < # of counties' <= 60), bquote('60 < # of counties' <= 100), bquote('# of counties' >= 100)) But as you can see, the < is different from <= in the legend. Can someone help me with this? Thanks!

example code:

x = c(0, 5, 10, 15)
y = c(2.2, 3.8, 4.6, 7.6)
z = c(4.5, 6.8, 9.3, 10.5)
plot(x, y, type="l", lwd=2, col=1, ylim=c(0, 12))
lines(x,z, lwd=2, col=2)
legend_txt <- c(bquote('5 < # events' <= 20),bquote('20 < # of events' <= 60))
legend('topright', legend = legend_txt, col = 1:2, lty = 1)

enter image description here

CodePudding user response:

I am not sure that this is what you are looking for. Put two tildes before the 5 and the legends align.

x = c(0, 5, 10, 15)
y = c(2.2, 3.8, 4.6, 7.6)
z = c(4.5, 6.8, 9.3, 10.5)
plot(x, y, type="l", lwd=2, col=1, ylim=c(0, 12))
lines(x,z, lwd=2, col=2)
legend_txt <- c(bquote(~~5 <~ '# of events' <= 20),
                bquote(20 <~ '# of events' <= 60))
legend('topright', legend = legend_txt, col = 1:2, lty = 1)

Created on 2022-06-04 by the reprex package (v2.0.1)

  • Related