[英]How to add degree symbol to multiple plot legend entries in R?
I know I can add a degree symbol to a plot legend by using expression
. 我知道我可以使用
expression
图例添加度数符号。
plot(1:5,1:5)
legend('topleft', fill = 'white', legend = expression(5~degree~C))
But How can I do this for multiple legend entries? 但是如何为多个图例条目执行此操作?
I tried the following, but it didn't work: 我尝试了以下,但它不起作用:
plot(1:5,1:5)
points(1:5,(1:5) + 0.1, col = 2)
legend('topleft', fill = c('white','red'), legend = paste0(c(5,10), expression(~degree~C)))
Edit: I am more interested in how to do this for many values, not just 2. 编辑:我更感兴趣的是如何为许多值执行此操作,而不仅仅是2。
Any suggestions on how to do this succinctly and neatly would be greatly appreciated! 如何简洁明了地提出任何建议将不胜感激! Thanks!
谢谢!
I think you need the help of substitute
and expression
. 我认为你需要
substitute
和expression
的帮助。 You do a sapply of the vector for which you need to write the expression, here i am putting c(5,10)
as input of sapply. 你需要写一个表达式的向量,这里我把
c(5,10)
作为sapply的输入。 For the function part, you need to use as.expression with substitute. 对于函数部分,您需要使用as.expression和替换。
plot(1:5,1:5)
points(1:5,(1:5) + 0.1, col = 2)
legend('topleft', fill = c('white','red'), legend = sapply(c(5,10), function(x) as.expression(substitute(A~degree~"C",list(A = as.name(x))))))
Output : 输出 :
您可以在表达式中包含这两个值
legend('topleft', fill = c('white','red'), expression(5~degree~C,10~degree~C))
对于ggplot:
ylab(expression(paste("SST (",degree~C,")"))) +
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.