Maria Lathouri
2018-Jun-25 07:50 UTC
[R] geom_text only in the first panel with facet_wrap in ggplot2
Dear all, I am trying to add text only in the first panel of a faceted ggplot; I have been struggling to find a solution on this online but unfortunately none of what I found is working. Here it is a reproducible example. I hope it helps: library(gamm4) library(ggplot2) example<-read.csv("example.csv") 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 ... # I need first 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)) #I had to create a new dataset so I can use this with the ggplot2 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_1<-factor(newDat$ASB, levels=c(1, 2, 3), labels=c("ASB1", "ASB2", "ASB3")) #I plot this with ggplot2 p<-ggplot(newDat, aes(x = Q, y = fit, group = ASB)) + theme_bw() + geom_rug(data = example, aes(x = Q, y = 0.96), sides = "b") + ylim(0.96, 1.04) + geom_ribbon(aes(ymin = lwr, ymax = upr), col = NA, fill = "grey", alpha = 0.3) + geom_line(size = 1) + facet_wrap(~ asb_1, labeller = label_parsed) #When I try to add the text through geom_text, I get the text to all the three panels dat_text <- data.frame(label = c("Text", " ", " "), ASB = c(1, 2, 3)) p + geom_text(x=20, y=1.03, data = dat_text, label = label) # or p+geom_text(x=20, y=1.03 , aes(label=label), data=dat_text) # I tried another way 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") # When I tried to use asb_1 instead of ASB, I got an errorann_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 for your help. Thank you very much in advance. Kind regards, Maria