【问题标题】:How to type subscripts in geom_text labels in ggplot如何在ggplot的geom_text标签中键入下标
【发布时间】:2019-08-11 07:21:35
【问题描述】:

我有时间序列数据绘制并按时间点分隔,我想用下标标记。下面是我用来生成图形和时间点标签的代码。我希望 -1、3 和 6 是下标。提前致谢!

timepoints=data.frame(date=as_datetime(c("2016-08-15" ,"2016-11-22", 
"2017-02-25")), timepoint=c("T-1", "T3", "T6"))

TimeseriespH = ggplot(FinalSeaphox, aes(x=DTTM)) +
geom_line(aes(y=MpH, color = "Outer Bay", group = grp), size = 0.5) +
geom_line(aes(y=CpH, color = "Inner Bay", group = grp), size = 0.5) +
scale_x_datetime(labels = date_format("%b '%y"), date_breaks = "1 
month", limits = as_datetime(c("2016-07-01","2017-04-19"))) +
labs(x = "", y = "pH") +
scale_y_continuous(limits = c(7.4,8.2)) +
geom_vline(xintercept = as_datetime("2016-12-01"), linetype = 2, color 
= "black") +
geom_vline(xintercept = as_datetime("2016-08-26"), linetype = 2, color 
= "black") +
geom_vline(xintercept = as_datetime("2017-03-06"), linetype = 2, color 
= "black") +
geom_text(data=timepoints, mapping=aes(x=date, y=c(8.18, 8.18, 8.18), 
label=timepoint), size=5, vjust=-0.4, hjust=0, inherit.aes = FALSE, 
color = "black")  

【问题讨论】:

    标签: r ggplot2 subscript geom-text


    【解决方案1】:

    下标需要用括号括起来:

    timepoint = c("T[-1]", "T[3]", "T[6]")
    

    然后在geom_text中使用parse = TRUE

    library(ggplot2)
    library(lubridate)
    
    timepoints=data.frame(
      date = as_datetime(c("2016-08-15" ,"2016-11-22", "2017-02-25")), 
      Y = c(8, 8.1, 8)
      timepoint = c("T[-1]", "T[3]", "T[6]")
    )
    ggplot(timepoints) +
      geom_point(aes(x = date, y=Y), size = 3) +
      geom_text(data=timepoints, 
                mapping=aes(x=date, y=c(8.18, 8.18, 8.18), 
                            label = timepoint), 
                size=5, vjust=0.4, hjust=0, inherit.aes = FALSE, 
                color = "black", parse = TRUE) 
    

    【讨论】:

    • 啊,很容易解决。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2022-11-25
    • 2018-01-08
    • 1970-01-01
    • 2020-12-27
    • 2017-05-22
    • 2018-07-26
    • 1970-01-01
    • 2013-08-22
    相关资源
    最近更新 更多