Discussion:
geom_text only in the first panel with facet_wrap in ggplot2
'Maria Lathouri' via ggplot2
2018-06-19 14:26:12 UTC
Permalink
Dear all,

I am trying to add text only in the first panel of a faceted ggplot; I have been trying to find a solution online but unfortunately none of it is working. I get the text in all the three panels.


Here is a reproducible example. I hope it helps:

head(example)
# Q index ASB Year WB_ID S_ID score_1 score_2 works
#1 100 1.02 1 2011 CL102021072830 157166 0 2.83 0
#2 100 1.03 1 2014 CL102021072830 157166 0 2.83 0
#3 80 1.02 1 2013 CL102021072860 1636 0 10.39 0
#4 80 1.06 2 2006 CL102021072860 1636 0 10.39 0
#5 80 1.06 2 2003 CL102021072860 1636 0 10.39 0
#6 98 1.07 3 2002 CL102021072900 1635 0 7.57 0

str(example)
#'data.frame': 249 obs. of 9 variables:
#$ Q : int 100 100 80 80 80 98 105 105 105 105 ...
#$ index : num 1.02 1.03 1.02 1.06 1.06 1.07 1.14 1.05 1.1 1.08 ...
#$ ASB : int 1 1 1 2 2 3 1 1 3 3 ...
#$ Year : int 2011 2014 2013 2006 2003 2002 2013 2005 2013 2006 ...
#$ WB_ID : Factor w/ 44 levels "CL102021072830",..: 1 1 2 2 2 3 3 3 4 4 ...
#$ S_ID : int 157166 157166 1636 1636 1636 1635 1635 1635 134261 1631 ...
#$ score_1: int 0 0 0 0 0 0 0 0 0 0 ...
#$ score_2: num 2.83 2.83 10.39 10.39 10.39 ...
#$ works : num 0 0 0 0 0 0 0 0 0 0 2800 2800 0 600 600 600 600 600 ...


Actually, at first I need to run a mixed-effect model:

model<-gamm4(index~s(Q, by=factor(ASB))+Year+score_1+score_2+works, data=example, random=~(1|WB_ID/S_ID))

newDat <- expand.grid(ASB = factor(example$ASB),
Q = seq(from = min(example$Q, na.rm = TRUE),
to = max(example$Q, na.rm = TRUE),
length = 100),
Year = 2002,
score_1 = mean(example$score_1),
score_2 = mean(example$score_2),
works = mean(example$works),
WB_ID = "CL102021072830",
S_ID = "157166")

datM <- predict(model$gam, type = "response",
se.fit = TRUE, newdata = newDat)

newDat$fit <- datM$fit
newDat$upr <- datM$fit + (1.96 * datM$se.fit)
newDat$lwr <- datM$fit - (1.96 * datM$se.fit)

#I create a new variable for ASB so I can change the panel text
newDat$asb<-factor(newDat$ASB, levels=c(1, 2, 3), labels=c("ASB1", "ASB2", "ASB3"))


#I plot it with ggplot
p<-ggplot(newDat, aes(x = Q, y = fit, group = ASB)) +
theme_bw() +
geom_rug(data = example, aes(x = Q, y = 0.98), sides = "b") +
ylim(0.98, 1.04) +
geom_line(size = 1) +
facet_wrap(~ asb, labeller = label_parsed)


#When I try to add the text through annotate, I get the text to all the three panels
ann_text <- data.frame(Q = 20, fit = 1.03, lab = "Text",
ASB = factor(1,levels = c("1","2","3")))

p + geom_text(data = ann_text,label = "Text")


#I try to use instead the asb which I use on the facet_wrap function but I get an error.
ann_text <- data.frame(Q = 20, fit = 1.03, lab = "Text",
+ asb = factor("ASB1",levels = c("1","2","3")))
#Error in FUN(X[[i]], ...) : object 'ASB' not found

I would very much appreciate if you could help me on this.


Thank you very much in advance.

Kind regards,
Maria
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...