r 中绘图标题中的下标

问题描述 投票:0回答:2

我想添加标题“1999年至2008年美国PM2.5的排放”,它使用r中的基本绘图函数。在此,我希望 2.5 成为 PM 的下标。如果 PM2.5 恰好位于字符串的末尾,我这样做没有任何问题:

barplot(height = total.emissions$Emissions, names.arg=total.emissions$year,  
        xlab="Year", ylab= " Amount of emitted in tonsPM"2.5 ,   
        main = "Emissions from in the United States from 1999 to 2008PM"[2.5] )

但是如果它位于字符串的中间,我就不能这样做。如果我将其分成两部分,如下所示:

barplot(height = total.emissions$Emissions, names.arg=total.emissions$year,  
        xlab="Year", ylab= " Amount of PM_[2.5] emitted in tons",   
        main = expression("Emissions from PM"[2.5] "in the United States from 1999 to 2008"))

由于方括号,我收到一条错误消息,指出意外符号。

r plot subscript
2个回答
11
投票

尝试使用

paste
expression
功能(详细信息请参见
?plotmath
),例如:

plot(0, main = expression(paste("Emissions from ", PM[2.5], " in the United States from 1999 to 2008")))

0
投票

当您想在每个下标/上标后跟随文本时,您还可以添加乘号:

barplot(height = total.emissions$Emissions, names.arg=total.emissions$year,  
        xlab="Year", ylab= expression("Amount of PM"[2.5]*" emitted in tons",   
        main = expression("Emissions from PM"[2.5]*" in the United States from 1999 to 2008"))

[取自:https://statisticsglobe.com/add-subscript-and-superscript-to-plot-in-r]

© www.soinside.com 2019 - 2024. All rights reserved.