如何在ggplot中的geom_text标签中键入下标

问题描述 投票:0回答:1

我有时间点数据绘制并由我想用下标标记的时间点分隔。下面是我用来生成图形和时间点标签的代码。我希望-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个回答
0
投票

对于下标,您需要括在括号中:

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

然后在parse = TRUE中使用geom_text

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) 

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.