使用“表达”使用来自数据框的值创建具有某些斜体的标签列表
我正在尝试创建一个包含斜体的标签列表。我可以用这样的“表达”来做到这一点,当我将它们放在情节上时(通过添加传说为例,但我会以不同的方式使用它们),这一切都很好。
sp.names=c(expression(paste("species ",italic("one")," sp.")),
expression(paste("species ",italic("two")," sp.")))
plot(1:10)
legend("topleft",legend=sp.names)
但是,我想在代码中直接指定标签中的单词,而是想从数据帧中的单元格中调用它们(因为它们有很多,并且根据我的基础数据而更改)。但是,当我尝试指定我想要的数据框单元时,它不会正确打印标签(请参见下文)。也许我想称“表达”功能可以识别的单元格有另一种方式?
df=data.frame(V1=c("species","species","species"),V2=c("one","two","three"))
sp.names=c(expression(paste(df$V1[1],italic(df$V2[1])," sp.")),
expression(paste(df$V1[2],italic(df$V2[2])," sp.")))
plot(1:10)
legend("topleft",legend=sp.names)
I'm trying to create a list of labels that contain italics. I can do it with "expression" like this, and when I put them on a plot (by adding a legend as an example, but I'll use them different ways), it all works nicely.
sp.names=c(expression(paste("species ",italic("one")," sp.")),
expression(paste("species ",italic("two")," sp.")))
plot(1:10)
legend("topleft",legend=sp.names)
But instead of specifying the words in the label directly in the code, I want to call them from cells in a dataframe (because there are a lot of them and they change depending on my underlying data). But when I try and specify which dataframe cell I want, it doesn't print the labels correctly (see below). Perhaps there is a different way for me to call the cell that I want that the "expression" function will recognise?
df=data.frame(V1=c("species","species","species"),V2=c("one","two","three"))
sp.names=c(expression(paste(df$V1[1],italic(df$V2[1])," sp.")),
expression(paste(df$V1[2],italic(df$V2[2])," sp.")))
plot(1:10)
legend("topleft",legend=sp.names)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
替换
,它在表达式中替换变量。我还删除了不需要的
粘贴
(在platmath中具有不同的含义),并用〜
代替了它,以提高可读性。Use
substitute
, it substitutes variables in expressions.I also removed the unneeded
paste
(which has a different meaning within plotmath) and replaced it with~
for increased readability.给出
bquote
镜头。Give
bquote
a shot.