0

I'm trying to annotate just first plot in facte_wrap function but it doesn't work!I did search but couldn't find an answer that why I get error.Thanks in advance :)

This is the plot: enter image description here

Here is my code:

B<-read.csv(choose.files(),header=T)
site<-B[,1]
month<-B[,2]
density<-B[,3]
library(ggplot2)
library(lubridate)
date<-dmy(  month,  quiet = FALSE,  tz = NULL,  locale =  Sys.getlocale("LC_TIME"),  truncated = 0)
A<-ggplot(data = B, aes(x=date, y=density,group=date))+geom_boxplot(aes(fill=site))+theme_classic()+
theme(aspect.ratio=0.75)+facet_wrap(~site)+
theme(strip.background = element_blank(),strip.text.x=element_blank())+
stat_summary(fun=median, geom="point", size = 2.5 ,aes(shape = site))+
scale_x_date(date_labels = " %b")+labs(x = "\nMonth", y = bquote('Density'~(mg)))+
theme(axis.title.x = element_text(color = 'black',face = 'bold',size = 16, hjust = 0.5))+
theme(axis.title.y = element_text(color = 'black',face = 'bold',size=18, 
hjust = 0.5))+theme(axis.text = font)+
theme(legend.title = element_blank(),legend.position="top",
legend.text = element_text(color =  "black",face = 'bold', size=12))

A+annotate("text",x=as.Date("2016-04-01"),y = 4.5,label="(a)",fontface="bold",size=5)###
This is what  works but annotate all plots
ann_text<-data.frame(x=as.Date("2016-04-01"),
y = 4,  lab="(a)",site=factor("Berke",levels=c("Hudson","Shirud","Berke")))
A+geom_text(data=ann_text,label="(a)")### this one gives me error

And here is my data:

 site     Month       density
Hudson  1.Apr.2016  0.586165468
Hudson  1.Apr.2016  0.226195982
Hudson  1.Apr.2016  3.614281059
Hudson  1.May.2016  0.527332108
Hudson  1.May.2016  3.029478971
Hudson  1.July.2016 0.563950688
Hudson  1.July.2016 0.261450446
Hudson  1.July.2016 0.418152365
Hudson  1.July.2016 0.15314951
Hudson  1.Aug.2016  0.203491674
Hudson  1.Aug.2016  0.085789449
Hudson  1.Aug.2016  0.377520862
Hudson  1.Aug.2016  0.255077678
Hudson  1.July.2016 0.180572753
Hudson  1.July.2016 0.120347364
Hudson  1.Sep.2016  0.111758245
Hudson  1.Sep.2016  0.174609039
Hudson  1.Sep.2016  0.10579666
Shirud  1.Apr.2016  2.886127158
Shirud  1.Apr.2016  1.22283898
Shirud  1.Apr.2016  1.005639192
Shirud  1.May.2016  0.371793341
Shirud  1.May.2016  1.333296278
Shirud  1.May.2016  2.040265903
Shirud  1.June.2016 0.584321457
Shirud  1.June.2016 4.65204552
Shirud  1.June.2016 2.629888197
Shirud  1.Aug.2016  0.57249892
Shirud  1.Aug.2016  1.541114492
Shirud  1.Aug.2016  0.833501986
Shirud  1.Sep.2016  0.853316575
Shirud  1.Sep.2016  0.148069366
Shirud  1.Sep.2016  0.173536413
Berke   1.June.2016 1.356025618
Berke   1.June.2016 0.391884409
Berke   1.June.2016 1.03214444
Berke   1.June.2016 0.235074894
Berke   1.July.2016 0.167735789
Berke   1.Aug.2016  3.782709228
Berke   1.Aug.2016  0.978743319
Berke   1.Aug.2016  4.558905703
Berke   1.Sep.2016  3.484025326
jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
  • Thank you so much for your help,I would like to annotate inside of the plots, if there is a method that can help in that way would be appreciated – fahimeh rashidabadi May 11 '21 at 05:36

1 Answers1

2

One method to label just the first panel:

library(tidyverse)
library(lubridate)
library(vroom)

B <- vroom("test.txt")
B$Month <- gsub(pattern = "\\.", replacement = " ", x = B$Month) %>% 
  dmy(., quiet = FALSE, tz = NULL, truncated = 0)

site_code <- c(
  Berke = "(a)",
  Hudson = "",
  Shirud = ""
)

ggplot(B, aes(x = date, y = density, group = date)) +
  geom_boxplot(aes(fill = site)) +
  facet_wrap(~ site, labeller = labeller(site = site_code)) +
  theme_classic() +
  stat_summary(aes(shape = site), fun = median, geom = "point", size = 2.5) +
  labs(x = "\nMonth", y = bquote('Density'~(mg))) +
  theme(aspect.ratio=0.75,
        axis.title.x = element_text(color = 'black',
                                    face = 'bold',
                                    size = 16,
                                    hjust = 0.5),
        axis.title.y = element_text(color = 'black',
                                    face = 'bold',
                                    size=18, 
                                    hjust = 0.5),
        legend.title = element_blank(),
        legend.position = "top",
        legend.text = element_text(color = "black",
                                   face = 'bold',
                                   size=12),
        strip.background = element_rect(fill = "transparent",
                                        color = "transparent"),
        strip.text = element_text(size = 16, hjust = 0,
                                  face = "bold"))

example_2.png

If you want to label each facet panel separately:

library(tidyverse)
library(lubridate)
library(vroom)

B <- vroom("example.txt")
B$Month <- gsub(pattern = "\\.", replacement = " ", x = B$Month) %>% 
  dmy(., quiet = FALSE, tz = NULL, truncated = 0)

site_code <- c(
  Berke = "(a)",
  Hudson = "(b)",
  Shirud = "(c)"
)

ggplot(B, aes(x = date, y = density, group = date)) +
  geom_boxplot(aes(fill = site)) +
  facet_wrap(~ site, labeller = labeller(site = site_code)) +
  theme_classic() +
  stat_summary(aes(shape = site), fun = median, geom = "point", size = 2.5) +
  labs(x = "\nMonth", y = bquote('Density'~(mg))) +
  theme(aspect.ratio=0.75,
        axis.title.x = element_text(color = 'black',
                                    face = 'bold',
                                    size = 16,
                                    hjust = 0.5),
        axis.title.y = element_text(color = 'black',
                                    face = 'bold',
                                    size=18, 
                                    hjust = 0.5),
        legend.title = element_blank(),
        legend.position = "top",
        legend.text = element_text(color = "black",
                                   face = 'bold',
                                   size=12),
        strip.background = element_rect(fill = "transparent",
                                        color = "transparent"),
        strip.text = element_text(size = 16, hjust = 0,
                                  face = "bold"))

example_1.png

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46