错误:当我想在 R 中的 facet_wrap 中注释时,美学必须是长度 1 或与数据 (1) 相同

问题描述

我正在尝试在 facte_wrap 函数中注释第一个图,但它不起作用!我进行了搜索,但找不到导致我出错的答案。提前致谢:)

这是剧情:

enter image description here

这是我的代码

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',size=18,hjust = 0.5))+theme(axis.text = font)+
theme(legend.title = element_blank(),legend.position="top",legend.text = element_text(color =  "black",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

这是我的数据:

 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

解决方法

仅标记第一个面板的一种方法:

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',size=18,legend.title = element_blank(),legend.position = "top",legend.text = element_text(color = "black",size=12),strip.background = element_rect(fill = "transparent",color = "transparent"),strip.text = element_text(size = 16,hjust = 0,face = "bold"))

example_2.png

如果要分别标记每个分面面板:

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

B <- vroom("example.txt")
B$Month <- gsub(pattern = "\\.",Hudson = "(b)",Shirud = "(c)"
)

ggplot(B,face = "bold"))

example_1.png