0

I've plotted the result of medians from a different month in the year. I've used stat_summary to get the sample size of each month. I need the sample size of each month and put it on the side so far I'm only able to put it on the box and it looks bad

library(ggplot2)

give.n <- function(x){
  return(c(y = median(x), label = length(x)))
}

p_m <- ggplot(data=m, aes(as.factor(as.yearmon(date)), value, fill= depth))+ 
    geom_boxplot(position=position_dodge(1), outlier.shape=NA)+
    theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
          panel.background = element_blank(), axis.line = element_line(colour = "black"))+
    ggtitle("Rhytisma f. fulvum spermatogenesis")+
    labs(x="Sampling months",y="Spermaries size (µm)")+
    guides(fill=guide_legend(title="Sampling depth"))+
    annotate("text", x=3, y=450, label= "2016", size= 5)+
    annotate("text", x=10, y=450, label= "2017", size= 5)+
    scale_y_continuous(limits  = c(0, 500), breaks = seq(0, 500, by = 50))+
    scale_fill_manual(values = c("white", "green"), 
                      labels = c("MCE" = "40-45 m ", "shallow" = "< 12 m "))+
    theme(axis.text.x = element_text(angle=45, hjust=1))+
    stat_summary(fun.data = give.n, geom = "text", position=position_dodge(width=0.75))+
    scale_x_discrete(labels=c("Apr", "May", "Jun", "Jul"
                              ,"Aug", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug" )
                     )

plot

4

0 回答 0