在固定位置添加几行可变文本到 ggplot facet

Add several lines of variable text in fixed positions to a ggplot facet

我正在尝试向此方面添加几行文本。抱歉代码乱七八糟

从对象 means1 我想为每个方面添加变量 "pCensCom" "pCensEx" 和 "pCensReg" 的值,如下图所示

这是用于构建图表的对象'censTot1'

        censo     censTot           tipoAni     censAn       año     pCensAn
1: 2010-01-01          42     Hembra adulta         27      2010   64.285714
2: 2010-01-01          42             Joven          4      2010    9.523810
3: 2010-01-01          42      Macho adulto          1      2010    2.380952
4: 2010-01-01          42           Ternero         10      2010   23.809524
5: 2010-01-02          42     Hembra adulta         27      2010   64.285714

---                                                       
7300: 2014-12-30       57            Ternero         16      2014   28.070175
7301: 2014-12-31       57      Hembra adulta         32      2014   56.140351
7302: 2014-12-31       57              Joven          7      2014   12.280702
7303: 2014-12-31       57       Macho adulto          2      2014    3.508772
7304: 2014-12-31       57            Ternero         16      2014   28.070175

下面介绍设计图时使用的代码

# Plot color background 
# %%%%%%%%%%%%%%%%%%%%%%

bg0<-data.table()
for(i in 1:5){
bg<-data.table(xstart=c(as.Date(paste0(años[i],"-01-01"), format="%Y-%m-%d"),as.Date(paste0(años[i],"-03-21"), format="%Y-%m-%d"), as.Date(paste0(años[i],"-06-21"),format = "%Y-%m-%d"),as.Date(paste0(años[i],"-09-21"),format = "%Y-%m-%d"),
                      as.Date(paste0(años[i],"-12-21"),format = "%Y-%m-%d")),xend=c(as.Date(paste0(años[i],"-03-21"), format="%Y-%m-%d"),
                      as.Date(paste0(años[i],"-06-21"),format = "%Y-%m-%d"), as.Date(paste0(años[i],"-09-21"),format = "%Y-%m-%d"),
                      as.Date(paste0(años[i],"-12-21"),format = "%Y-%m-%d"),as.Date(paste0(años[i],"-12-31"),format = "%Y-%m-%d")),
             Estacion=c("Invierno","Primavera","Verano","Otoño","Invierno"))
l=list(bg0,bg); bg0<-rbindlist(l, fill=TRUE)
}

bg0<-bg0[,Estacion:=factor(ordered(Estacion,levels=c("Invierno","Primavera", "Verano", "Otoño")))]
cbPalette<-c("#FF3300","#006633","#FFFF00","#0000FF")
plotbg<-ggplot()+ geom_rect(data = bg0, aes(xmin = xstart, xmax = xend, ymin = 0, ymax = Inf, fill = Estacion), alpha = 0.10)+ scale_fill_manual(values=cbPalette)+ guides(fill=FALSE)+theme_bw()


means1<-data.table(tipoAni=c("Hembra adulta","Joven","Macho adulto","Ternero"),pCensCom=c(62.3,17.8,0.9,19.37),pCensEx=c(61.4,16.1,1.9,20.6),pCensReg=c(63.0,17.9,1.6,24.7))


# Plot
# %%%%
plotbg + geom_line(data=censTot1,aes(x=censo,y=pCensAn))+ facet_grid(tipoAni ~ .)+ xlab("Censos diarios") + ylab("Animales (%)") +theme_bw()+ theme(strip.text.x = element_text(size=8),strip.text.y = element_text(size=10, face="bold"),strip.background = element_rect(colour="red", fill="#CCCCFF"))

我需要帮助,我尝试了几次使用函数 annotation_customgrobTreetextGrob 但我无法实现

这是一个简化的答案。首先,我模拟了一些数据 dat,然后是第二个 data.table backgr,其中包含背景信息,最后是 textdt,其中包含有关文本元素的信息。

代码如下所示:

library(data.table)
library(ggplot2)
library(scales)

dat <- data.table(x = rep(1:100, 2),
                  group = rep(LETTERS[1:2], each = 100),
                  val = rnorm(200))

dat[, price := 100 + cumsum(val), by = group]

# plot empty

ggplot(dat, aes(x = x, y = price)) + 
  geom_line() + 
  facet_grid(group~.)

# plot with added polygons
# for the background colors
backgr <- data.table(minval = c(10, 40, 60, 90), 
                     maxval = c(20, 60, 80, 100),
                     backgroup = LETTERS[1:4])
# for the text elements
textdt <- data.table(xval = c(10, 50, 70),
                     yval = c(105, 100, 95),
                     textlabel = c("foo", "bar", "lorum"),
                     group = c("A", "A", "B"))

# plot
ggplot() + 
  geom_rect(data = backgr, aes(xmin = minval, xmax = maxval, ymin = -Inf, 
                               ymax = Inf, fill = backgroup)) +
  geom_line(data = dat, aes(x = x, y = price)) + 
  geom_text(data = textdt, aes(x = xval, y = yval, label = textlabel, 
                               group = group)) + 
  facet_grid(group~.) + 
  scale_fill_manual(values = alpha(c("red", "green", "blue", "yellow"), 0.5))

这会产生这样的图,您可以对其进行调整以适合您的数据: