如何仅在R基础图中将一个图例名称改为斜体?(How to only make one legend name italic in base R plot?)

编程入门 行业动态 更新时间:2024-10-12 03:23:52
如何仅在R基础图中将一个图例名称改为斜体?(How to only make one legend name italic in base R plot?)

我想添加一个图例,其中只有一个图例名称被斜体显示 。 我在base R中使用plot() 。

但是,我需要斜体行来包含可变数字,所以我使用bquote 。

我尝试过的东西不起作用:

a <- 2 ; b <- 5 plot(a,b) l1 <- bquote(Italic ~ .(a) + .(b)) l2 <- bquote(a + b) legend(x='topright',legend = bquote(italic(.(l1)))) legend(x='topleft',legend = c(bquote(italic(.(l1))),l2)) 请注意,当我自己离开表达式(右图)时,它会正确表示斜体。 但是,当我为其他图例行添加文本时,它会混淆(LEFT LEGEND)。

我该如何正确地做到这一点?

注意:我宁愿有一个不需要多次调用legend()的解决方案。

I want to add a legend to a plot in which only one of the legend names is italicized. I'm using plot() in base R.

However, I need that italicized line to contain variable numbers, so I use bquote.

What I've tried does not work:

a <- 2 ; b <- 5 plot(a,b) l1 <- bquote(Italic ~ .(a) + .(b)) l2 <- bquote(a + b) legend(x='topright',legend = bquote(italic(.(l1)))) legend(x='topleft',legend = c(bquote(italic(.(l1))),l2)) Notice that when I leave the expression on its own (RIGHT LEGEND), it italicizes correctly. However, when I add the text for other legend lines, it messes up (LEFT LEGEND).

How do I properly do this?

Note: I'd rather have a solution that doesn't require multiple calls to legend().

最满意答案

你可以使用text.font = 3 ,说:

legend(x='topleft',legend = c(bquote((.(l1))),l2), text.font=c(3,1))

You could use text.font = 3, say:

legend(x='topleft',legend = c(bquote((.(l1))),l2), text.font=c(3,1))

更多推荐

本文发布于:2023-08-07 12:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464522.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斜体   图中   图例   名称   基础

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!