【问题标题】:Subscripts when using bquote in geom_text followed by text在 geom_text 中使用 bquote 后跟文本时的下标
【发布时间】:2020-03-20 13:49:02
【问题描述】:

过去几个小时我一直在查看 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

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

【问题讨论】:

    标签: ggplot2


    【解决方案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())
    

    【讨论】:

    • 你是个巫师!感谢您的帮助!
    猜你喜欢
    • 2017-03-12
    • 2019-08-11
    • 2021-02-01
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多