简体   繁体   中英

Special symbols in legend in R

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)

在此处输入图像描述

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM