r - 使用plotmath 在 ggplot2 中添加上标

标签 r ggplot2 axis-labels superscript plotmath

我正在尝试使用几篇文章中引用的绘图数学表达式来获取 y 轴单位的上标。我浏览了有关使用 expression 的线程还有here ,但它对我不起作用。这是我到目前为止所拥有的:

ggplot(data.frame, aes(Region, Zmar_sh)) + 
  geom_boxplot() + xlab("Region") +  
  ylab(expression(paste("eelgrass shoot density", " (shoots/", expression(m[2])))) +
  ggtitle(expression(paste(italic("Zostera marina")," shoot density in X Bay", sep = " ")))

ggtitle 表达式有效,但我仍然无法让 y 轴读取“鳗草芽密度(芽/平方米)”,并以“2”作为上标。我缺少什么?

最佳答案

您不需要第二个表达式()。另外,m[2] 给出一个下标。使用 m^2 作为上标。

以下是内置 mtcars 数据框的示例:

ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  ylab(expression(paste("eelgrass shoot density", " (shoots/", m^2,")")))

事实上,您甚至不需要使用paste。相反,你可以这样做:

ylab(expression(eelgrass~shoot~density~(shoots/m^2)))

其中 ~ 是“空格”字符。如果您在单词之间放置实际的空格(不使用 ~),您将收到错误。但是,为了便于阅读,您也可以这样做:

ylab(expression(eelgrass ~ shoot ~ density ~ (shoots/m^2)))

这相当于上面的代码,因为只要 ~ 存在,空格就会被忽略。

上面的任何 ylab 表达式都会给出以下图:

enter image description here

关于r - 使用plotmath 在 ggplot2 中添加上标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35880053/

相关文章:

css - 在 R 中的 DT 中悬停图像弹出窗口

r - 在 textInput 中选择文本/设置焦点

r - gvisMerge + gvisAnnotatedTimeLine 通用 RangeSelector

r - ggplot2 1.01 中不再有 geom_label() 了吗?

r - {ggdist }'s stat_interval levels calculate (I thought it was confidence intervals but they' re 不正确)是什么?

R ggplot2 : filling the space between 2 lines with varying x values (geom_ribbon) and formatting log axis labels

r - 存在重复项时生成唯一的组合

r - 如何手动将图例添加到 ggplot 对象

Python 子图为公共(public)轴标签留出空间

用自己的值替换 X 轴