【问题标题】:How to subscript a letter or numeric digit in text of ggplot2 [duplicate]如何在ggplot2的文本中下标字母或数字[重复]
【发布时间】:2019-01-23 10:02:12
【问题描述】:

我希望 y 轴标记为:甲状腺素血清水平 (T4),下标为 T4 中的 4。在我的代码中,我尝试了 labs(y = expression(Serum Levels of Thyroxine ([T4]))。但是它不起作用。以前的问题只涉及要下标的字母,但是,当字母或否时出现问题。下标是在一些单词/文本之后。 我的数据框和代码如下

df <- data.frame(time = c("1 h","6 h","12 h","24 h","3 days"),
    values = c(1,2,3,4,5))
  df %>% 
      ggplot(aes(time,values))+
      geom_point()+
      labs(y = "Serum Thyroxine T4 levels", 
    x = "Exposure Period")

【问题讨论】:

  • 抱歉错误。我编辑了问题
  • ggplot(df, aes(time,values))+ geom_point()+ labs(x = expression(Exposure~Period) , y = expression(Serum~Thyroxine~T["4"]~水平~(ng/ml)))

标签: r ggplot2


【解决方案1】:

问题似乎是由于表达式中使用的空格造成的。解决这个问题的方法是将每个空格替换为~

library(ggplot2)
library(magrittr)
df <- data.frame(time = c("1 h","6 h","12 h","24 h","3 days"),
                 values = c(1,2,3,4,5))
ggplot(df, aes(time,values)) +
  geom_point() + 
  labs(y = expression(Serum~Thyroxine~T[4]~levels~(ng/ml)), x = "Exposure Period")

编辑:将 T 移出 []

【讨论】:

  • 当我使用你的代码时,得到这个错误:Error: unexpected '[' in: " geom_point() + labs(y = expression(Serum~Thyroxine~[" >
  • 这个有效:labs(y = expression(Serum~Thyroxine~T[4]~levels~(ng/ml)), x = "Exposure Period")
  • 谢谢,从我的本地会话中复制过来
  • 非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-15
  • 2021-02-11
  • 1970-01-01
  • 2014-02-19
  • 2016-04-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多