首页 > 解决方案 > 在 geom_text 中使用 bquote 后跟文本时的下标

问题描述

在过去的几个小时里,我一直在查看 stackoverflow,但找不到适合我的问题的解决方案。

问题:我正在尝试使用 geom_text 手动写入手动计算的值,当我使用 bquote 写入值及其单位时,这很好(例如,只写 4.4µm 就可以了)。但是,我想使用 geom_text 在左上角写出D[g]= 4.4µm (g 子集!)。但是,一旦我开始尝试为其添加下标,它就不起作用了,并且出现以下错误之一:

Error: Don't know how to add RHS to theme object 
Error: Aesthetics must be either length 1 or the same as the data (14): label
Error: unexpected string constant in:
" #geom_text(label = bquote("D" [g] "= 4.4µm"), x = -.3, y = 900, size = 10)  +
  geom_text(label = bquote(D[g]"=4.4µm"

这是我迄今为止一直在使用的代码:

A = structure(list(`Size (µm)` = c(0.85, 0.85, 1.6, 1.6, 2.7, 2.7, 
4, 4, 5.85, 5.85, 9, 9, 20, 20), `C/dlogd` = c(0, 70.7482842209313, 
70.7482842209313, 147.721992341133, 147.721992341133, 752.133343128365, 
752.133343128365, 296.076678012312, 296.076678012312, 226.648066580862, 
226.648066580862, 302.286593848111, 302.286593848111, 0), `+s` = c(0, 
175.47011068069, 175.47011068069, 243.15534458114, 243.15534458114, 
1007.91042406178, 1007.91042406178, 439.475898343651, 439.475898343651, 
366.578774657598, 366.578774657598, 385.065980566499, 385.065980566499, 
0), `-s` = c(0, -33.9735422388272, -33.9735422388272, 52.2886401011273, 
52.2886401011273, 496.356262194949, 496.356262194949, 152.677457680973, 
152.677457680973, 86.7173585041254, 86.7173585041254, 219.507207129723, 
219.507207129723, 0), Sampling = c("A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A"), Date = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("21st of February 2019", 
"19th of June 2019", "3rd of July 2019", "17th of July 2019"), class = "factor"), 
    Datenoyear = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L), .Label = c("21st of February", "19th of June", 
    "3rd of July", "17th of July"), class = "factor"), DateNumber = c(21.2, 
    21.2, 21.2, 21.2, 21.2, 21.2, 21.2, 21.2, 21.2, 21.2, 21.2, 
    21.2, 21.2, 21.2)), row.names = c(NA, -14L), class = c("tbl_df", 
"tbl", "data.frame"))



ggplot(A, aes(x=  `Size (µm)`)) + 
geom_line(aes(y = `C/dlogd`), size = 1.5) + 
  geom_line(aes(y =  `+s`), linetype = "twodash", color = "red") +
  geom_line(aes(y =  `-s`), linetype = "dashed" , color = "blue") +
  geom_point(aes(y =  `+s`), color = "red") +
  geom_point(aes(y = `C/dlogd`)) +
  geom_point(aes(y =  `-s`), color = "blue") +
  scale_x_log10(limits = c(0.4,21)) +
 #geom_text(label = bquote("D" [g] "= 4.4µm"), x = -.3, y = 900, size = 10)  + #gives the second error
  #geom_text(label = bquote(D[g]"=4.4µm"), x = -.1, y = 900, size = 10) + #gives the 3rd error
 # geom_text(label = bquote("4.4µm"), x = -0.3, y = 900, size = 10) +  #No error, but doesn't give me the "D[g] = " I need
  theme_bw() +
  annotation_logticks(side = "b") +
  facet_grid(~Datenoyear) +
  xlab(element_blank()) +
  ylab(element_blank())

我做错了什么?我试过看看这些解决方案: http ://rcaeology.blogspot.com/2012/11/combining-superscripts-subscripts-and.html

https://community.rstudio.com/t/use-bquote-in-ggplot2-for-subscript-text/40882

使用文本在 R 中为绘图编写标签,并使用 bquote、paste 或表达式编写下标

但不幸的是,它们都没有奏效。任何帮助,将不胜感激。

标签: ggplot2

解决方案


不幸的是,我不能告诉你你的代码有什么问题。但是bquote可以使用as.character(expression(paste(D[g], "=4.4µm"))). 来源:见这里。尝试这个:

ggplot(A, aes(x=  `Size (µm)`)) + 
  geom_line(aes(y = `C/dlogd`), size = 1.5) + 
  geom_line(aes(y =  `+s`), linetype = "twodash", color = "red") +
  geom_line(aes(y =  `-s`), linetype = "dashed" , color = "blue") +
  geom_point(aes(y =  `+s`), color = "red") +
  geom_point(aes(y = `C/dlogd`)) +
  geom_point(aes(y =  `-s`), color = "blue") +
  geom_text(label = as.character(expression(paste(D[g], "=4.4µm"))), parse = TRUE, x = -.1, y = 900, size = 10) + #gives the 3rd error
  scale_x_log10(limits = c(0.4, 21)) +
  theme_bw() +
  annotation_logticks(side = "b") +
  facet_grid(~Datenoyear) +
  xlab(element_blank()) +
  ylab(element_blank())

推荐阅读