在绘图轴标题中使用特殊字符和上标

41

我想制作一个带特殊字符和上标的y轴标题。 我已经能够做到这一点,但是我希望右括号不要上标。 这就是我遇到的问题。 我认为这只是我的括号放置问题,但是我已经尝试了(看起来)所有方法。

plot(WatexCl, ConcuM, col = as.numeric(1), pch = as.numeric(Depth), 
   xlab = expression(paste("Concentration Cl  ( ", mu, "moles/g dry wt)")), 
   ylab = expression(paste("Average Conc of S- on plates ( ", mu, "Moles/cm"^"2"),)), 
   data = plates)
2个回答

73

很多用户经常无法理解的一件事是,当在一个绘图标签的表达式中使用字符串时,你通常不需要将它们加引号并粘贴在一起。直接使用布局工具(例如~*)通常更简单。例如:

df <- data.frame(y = rnorm(100), x = rnorm(100))

plot(y ~ x, data = df,
     ylab = expression(Average ~ Conc ~ of ~ S- ~ on ~ plates ~ 
                       (mu ~ Moles ~ cm^{-2} ~ dry ~ wt)),
     xlab = expression(Concentration ~ Cl ~ (mu ~ moles ~ g^{-1} ~ dry ~ wt)))

或者,您可以包含长文本段的字符串; 在这种情况下,这样做可能更容易:

plot(y ~ x, data = df,
     ylab = expression("Average Conc of S- on plates" ~
                         (mu ~ moles ~ cm^{-2} ~ "dry wt")),
     xlab = expression("Concentration Cl" ~ (mu ~ moles ~ g^{-1} ~ "dry wt")))

但请注意这里没有必要将字符串和其他特性进行粘贴

两者都会生成:

enter image description here

请注意plotmath与上标2的问题。您可能希望添加一些额外的空间作为y轴边距以适应它:

op <- par(mar = c(5,4.5,4,2) + 0.1)
plot(y ~ x, data = df,
     ylab = expression("Average Conc of S- on plates" ~
                          (mu ~ moles ~ cm^{-2} ~ "dry wt")),
     xlab = expression("Concentration Cl" ~ (mu ~ moles ~ g^{-1} ~ "dry wt")))
par(op)

生产

enter image description here


7
这可以解决上标闭括号的问题:
# reproducible data
plates <- data.frame(WatexCl = rnorm(100), ConcuM = rnorm(100), Depth = rnorm(100))

# alter the default plot margins so the 
# superscript in the y-axis label is completely displayed
par(mar=c(5,5,4,2))

# draw the plot
plot(WatexCl ~ ConcuM, data = plates,
     col = as.numeric(1), 
     pch = as.numeric(Depth), 
     xlab = bquote("Concentration Cl ("*mu~"moles/g dry wt)"), 
     ylab = bquote("Average Conc of S- on plates ("~mu~"Moles/cm"^"2"*")"))

enter image description here


2
bquote在这里有点过头了。使用expression应该可以很好地工作。当想要用值替换表达式中的对象时,bquote非常有用。 - Gavin Simpson
@GavinSimpson,谢谢,这很好知道!看起来expression在处理空格字符方面比bquote做得更好,我的图表括号附近有一些奇怪的空格... - Ben
6
这是因为你使用 ~ mu ~ 来分隔两个引用的字符串。如果你将其设置为 * mu ~,就会得到正确的间距。~ 是一个间距运算符,* 将参数并置(即不带空格)。 - Gavin Simpson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接