如何用文本和上标ggplot2编写x轴标题

时间:2022-11-30 09:35:46

I would like to write the below title at an x-axis using the following code:

我想使用以下代码在x轴上写下面的标题:

Title: Grain yield (ton. ha-1)

标题:粮食产量(吨.ha-1)

labs(x=expression(bold(paste("Grain yield","  ","(ton.", ha^-1,")", sep=""))))

-1 should be superscripted and the entire title should be in bold. I am getting everything right excepted that the superscripted part is not in bold.

-1应该是上标的,整个标题应该是粗体。除了上标部分不是粗体外,我得到了正确的一切。

I appreciate any help.

我感谢任何帮助。

Thanks!

谢谢!

1 个解决方案

#1


15  

It's rather unusual to get incorrect advice from @BenBolker, but here is the solution to what he was offering as an example of a problem without solution:

从@BenBolker获得不正确的建议是相当不寻常的,但这里是他提供的解决方案,作为没有解决方案的问题的示例:

barplot(height=c(1,1), ylab=expression(bold(paste("org.", cm^bold("-2")))))

The trick here is not to use numeric but rather text arguments. In your case you are under the common misconception that paste in plotmath has a 'sep' argument. It doesn't. (Furthermore it is generally not needed if you learn to use "~" and "*" properly.) This is a paste()-less solution:

这里的诀窍是不使用数字而是使用文本参数。在你的情况下,你有一个普遍的误解,即plotmath中的粘贴有一个'sep'参数。它没有。 (此外,如果你学会正确使用“〜”和“*”,通常不需要它。)这是一个paste() - 更少的解决方案:

plot(1,1, xlab=expression(bold(Grain~yield~~"(ton."*ha^"-1"*")")))

(I tested it with a base graphic because you didn't offer a complete example. There is no lab function in base R.)

(我用基础图形测试它,因为你没有提供完整的例子。基础R没有实验室功能。)

#1


15  

It's rather unusual to get incorrect advice from @BenBolker, but here is the solution to what he was offering as an example of a problem without solution:

从@BenBolker获得不正确的建议是相当不寻常的,但这里是他提供的解决方案,作为没有解决方案的问题的示例:

barplot(height=c(1,1), ylab=expression(bold(paste("org.", cm^bold("-2")))))

The trick here is not to use numeric but rather text arguments. In your case you are under the common misconception that paste in plotmath has a 'sep' argument. It doesn't. (Furthermore it is generally not needed if you learn to use "~" and "*" properly.) This is a paste()-less solution:

这里的诀窍是不使用数字而是使用文本参数。在你的情况下,你有一个普遍的误解,即plotmath中的粘贴有一个'sep'参数。它没有。 (此外,如果你学会正确使用“〜”和“*”,通常不需要它。)这是一个paste() - 更少的解决方案:

plot(1,1, xlab=expression(bold(Grain~yield~~"(ton."*ha^"-1"*")")))

(I tested it with a base graphic because you didn't offer a complete example. There is no lab function in base R.)

(我用基础图形测试它,因为你没有提供完整的例子。基础R没有实验室功能。)