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:
<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