I am trying to include text into the upper right corner of my faceted plots. I have 5 survival curve plots and I would like to include text into each one, which will be the month at which the probability of survival equals 50. My code would ideally plot the survival curve for the species and include a text line: "Average Life X Months", where X is the month where the species is has a 50% prob of survival. My code almost does this, but for each plot, it overlaps each text line. So instead of having 1 text line for each plot I have 5 text lines overlapping each other for each plot. I think my problem has to do with how the strata is factored.
all<-survfit(Surv(MonthDifference,Survival)~Spec,data=ad,conf.type='log-log')
sum<-summary(all)
allquant<-quantile(all,.5)
stratalabs<-c("Species A","Species B","Species C","Species D","Species E")
names(stratalabs)<-c("A","B","C","D","E")
a<-ggplot(data = all, aes(x = time, y = surv))+
geom_line() +
facet_wrap( .~strata, ncol = 2,labeller =
labeller(strata = stratalabs))
dat_tex<-data.frame(label=c("Average Life 90 Months","Average Life 76 Months","Average Life 73 Months","Average Life 51 Months","Average Life 50 Months"),
f=factor(c(1:5)))
a+ geom_text(data=dat_tex,aes(x=800,y=1,label=label),vjust=2)
Answers to similar posts like mine on SO mention using the egg package's tag_facet function. Using this function actually actually worked and put the text into the correct plots, but the problem is that it deleted the plot titles.
mytag<-c("Average Life 90 Months","Average Life 76 Months","Average Life 73 Months","Average Life 51 Months","Average Life 50 Months")
a<-ggplot(data = all, aes(x = time, y = surv))+
geom_line() +
facet_wrap( .~strata, ncol = 2,labeller =
labeller(strata = stratalabs))
tag_facet(a, x = Inf, y = Inf,
hjust = 1.0,
tag_pool = mytag)
My attempts to troubleshoot this problem come from:
Annotating text on individual facet in ggplot2 ,
Annotating text on individual facet in ggplot2 using geom_text,