如何用ggplot2中的数学表达式制作多行x轴标题[复制]

问题描述 投票:1回答:1

这个问题在这里已有答案:

xlab(expression(paste("CO"^"2", " concentration", "\n Lolium perenne")))

这是我目前的编码。使用我的xlab,我希望CO ^ 2浓度在一条线上,然后在那之下使用“Lolium perenne”,但它也需要用斜体字表示。目前,该代码将“浓度”置于“CO ^ 2”和“Lolium Perenne”之上的线上。请帮忙!

r ggplot2 plotmath
1个回答
1
投票

让我们把它变成一个可重复的例子,然后用@Brian建议的方法回答。

以下不起作用。 x轴标题全部在一行:

library(ggplot2)
set.seed(124)
d <- data.frame(x = rnorm(50),
                y = rnorm(50))

ggplot(d, aes(x, y)) + geom_point() +
  xlab(expression(paste("CO"^"2", " concentration", "\n Lolium perenne")))

enter image description here

但是,使用atop()函数,我们可以得到这个结果:

ggplot(d, aes(x, y)) + geom_point() +
  xlab(expression(atop(CO[2]*" concentration", italic("Lolium perenne"))))

enter image description here

我们使用italic()函数以斜体字排版“Lolium perenne”。我们还用下标写了二氧化碳,这可能就是这里需要的。

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