ggplot2 faceting and adding unique labels for each facet in a for loop in R

I am trying to add a unique label to each faceted panel in my plots. Below is the code which I annotated and will describe in detail below:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>ebird <- left_join(ebird, cor,
by = c("Species", "region")) %>%
dplyr::select(-Date1.y, -survey.y) %>%
rename(Date1 = Date1.x) %>%
mutate(rho = factor(rho)) # added rho, which is what I'm trying to label each plot with
species <- unique(ebird$Species) # 30 species
for (i in 1:length(species)){ # loop over species
ggplot(ebird[(ebird$Species == species[i]),],
aes(x = Date1,
y = scaled_ebird,
)) +
facet_wrap(~region, # facet by region
scales = "free",
drop = TRUE) +
geom_line(aes(x = Date1, y = scaled_ebird),
#color = Species),
linewidth = 1) +
geom_ribbon(aes(ymin = scaled_ebird_lower, ymax = scaled_ebird_upper), alpha = 0.2) +
geom_line(aes(x = Date1, y = scaled_survey), linewidth = 1, linetype = "dashed") +
geom_point(aes(x = Date1, y = scaled_survey), pch = 21, fill = "black", size = 3) +
geom_text(aes(label = rho)) + # attempting to annotate each plot!!!
scale_x_date(expand = c(0,0.5),
limits = c(as.Date("2022-09-01"),
as.Date("2023-01-31")
),
breaks = seq.Date(as.Date("2022-09-01"),
as.Date("2023-01-31"),
by ="1 month"),
date_labels ="%b",
minor_breaks = seq.Date(as.Date("2022-09-01"),
as.Date("2023-01-31"),
by ="1 week")
) +
scale_color_brewer(palette = "Dark2") +
labs(x = NULL,
y = "Scaled Relative Abundance",
title = "") +
theme_bw(base_size = 16) +
theme(legend.position = "bottom") +
ggtitle(label = paste(species[i],
"Migration Chronology in Michigan",
sep = " ")) +
theme(plot.title = element_text(face = "bold", hjust = 0.5)) +
guides(color = "none")
ggsave(filename = paste("plots/mi/MI_migchron_",species[i],".png", sep = ""),
height = 10,
width = 10,
dpi = 600)
}
</code>
<code>ebird <- left_join(ebird, cor, by = c("Species", "region")) %>% dplyr::select(-Date1.y, -survey.y) %>% rename(Date1 = Date1.x) %>% mutate(rho = factor(rho)) # added rho, which is what I'm trying to label each plot with species <- unique(ebird$Species) # 30 species for (i in 1:length(species)){ # loop over species ggplot(ebird[(ebird$Species == species[i]),], aes(x = Date1, y = scaled_ebird, )) + facet_wrap(~region, # facet by region scales = "free", drop = TRUE) + geom_line(aes(x = Date1, y = scaled_ebird), #color = Species), linewidth = 1) + geom_ribbon(aes(ymin = scaled_ebird_lower, ymax = scaled_ebird_upper), alpha = 0.2) + geom_line(aes(x = Date1, y = scaled_survey), linewidth = 1, linetype = "dashed") + geom_point(aes(x = Date1, y = scaled_survey), pch = 21, fill = "black", size = 3) + geom_text(aes(label = rho)) + # attempting to annotate each plot!!! scale_x_date(expand = c(0,0.5), limits = c(as.Date("2022-09-01"), as.Date("2023-01-31") ), breaks = seq.Date(as.Date("2022-09-01"), as.Date("2023-01-31"), by ="1 month"), date_labels ="%b", minor_breaks = seq.Date(as.Date("2022-09-01"), as.Date("2023-01-31"), by ="1 week") ) + scale_color_brewer(palette = "Dark2") + labs(x = NULL, y = "Scaled Relative Abundance", title = "") + theme_bw(base_size = 16) + theme(legend.position = "bottom") + ggtitle(label = paste(species[i], "Migration Chronology in Michigan", sep = " ")) + theme(plot.title = element_text(face = "bold", hjust = 0.5)) + guides(color = "none") ggsave(filename = paste("plots/mi/MI_migchron_",species[i],".png", sep = ""), height = 10, width = 10, dpi = 600) } </code>
ebird <- left_join(ebird, cor,  
                   by = c("Species", "region")) %>%
  dplyr::select(-Date1.y, -survey.y) %>%
  rename(Date1 = Date1.x) %>%
  mutate(rho = factor(rho)) # added rho, which is what I'm trying to label each plot with


species <- unique(ebird$Species) # 30 species 

for (i in 1:length(species)){    # loop over species
  
  ggplot(ebird[(ebird$Species == species[i]),],
         aes(x = Date1, 
             y = scaled_ebird,
             )) +
    facet_wrap(~region,           # facet by region
               scales = "free", 
               drop = TRUE) +
    geom_line(aes(x = Date1, y = scaled_ebird), 
              #color = Species), 
              linewidth = 1) +
    geom_ribbon(aes(ymin = scaled_ebird_lower, ymax = scaled_ebird_upper), alpha = 0.2) +
    geom_line(aes(x = Date1, y = scaled_survey), linewidth = 1, linetype = "dashed") +
    geom_point(aes(x = Date1, y = scaled_survey), pch = 21, fill = "black", size = 3) +
    geom_text(aes(label = rho)) + # attempting to annotate each plot!!!
    scale_x_date(expand = c(0,0.5), 
                 limits = c(as.Date("2022-09-01"), 
                            as.Date("2023-01-31")
                 ), 
                 breaks = seq.Date(as.Date("2022-09-01"),
                                   as.Date("2023-01-31"), 
                                   by ="1 month"),
                 date_labels ="%b", 
                 minor_breaks = seq.Date(as.Date("2022-09-01"), 
                                         as.Date("2023-01-31"), 
                                         by ="1 week")
    ) +
    scale_color_brewer(palette = "Dark2") +
    labs(x = NULL, 
         y = "Scaled Relative Abundance",
         title = "") +
    theme_bw(base_size = 16) +
    theme(legend.position = "bottom") +
    ggtitle(label = paste(species[i], 
                          "Migration Chronology in Michigan", 
                          sep = " ")) +
    theme(plot.title = element_text(face = "bold", hjust = 0.5)) +
    guides(color = "none")
  
  ggsave(filename = paste("plots/mi/MI_migchron_",species[i],".png", sep = ""),
         height = 10,
         width = 10,
         dpi = 600)
  
}

My problem is that when annotating each plot with rho it labels ALL of them (see image below). Goal I would like to simply annotate each facet with a single number that I turned into a factor (122 levels). Likely a smple fix but can’t seem to get it to work. Any help appreciated.

Example Problem Plot

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật