在geom_text中使用bquote后跟文本的下标

时间:2020-03-20 13:49:02

标签: ggplot2

在过去的几个小时中,我一直在研究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://rchaeology.blogspot.com/2012/11/combining-superscripts-subscripts-and.html

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

writing a label in R for a plot using text, and subscripts using either bquote, paste or expression

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

1 个答案:

答案 0 :(得分:1)

很遗憾,我无法告诉您代码的问题是什么。但是,使用bquote可以达到期望的结果,而不用使用as.character(expression(paste(D[g], "=4.4µm")))。资料来源:见here。试试这个:

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())