[R] Σχετ: geom_text only in the first panel with facet_wrap in ggplot2

Maria Lathouri m|@thour| @end|ng |rom y@hoo@gr
Mon Jun 25 16:46:02 CEST 2018


Dear Ulrik and all,
Thank you all so much; well, Ulrik's suggestion worked better. Very much appreciated.
Best,Maria 

    Στις 11:17 π.μ. Δευτέρα, 25 Ιουνίου 2018, ο/η Ulrik Stervbo <Ulrik.Stervbo using ruhr-uni-bochum.de> έγραψε:
 

 Hi Maria,

you are on the right way. The data.frame with the text must have the 
same columns as you use in the clobal aesthetics, that is 'Q', 'fit', 
'ASB', and the facet variable ('asb_1') and the label you want shown.

ann_text <- data.frame(Q = 20, fit = 1.03, ASB = 1, plot_lab =  
c("1","2","3"), asb_1 = c("ASB1", ASB2", "ASB3"))

p + geom_text(data = ann_text, aes(label = plot_lab)

should do the trick

HTH
Ulrik

On 2018-06-25 09:50, Maria Lathouri via R-help wrote:
> 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
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


   
	[[alternative HTML version deleted]]




More information about the R-help mailing list