ggplot2 分面并为 R 中的 for 循环中的每个面添加唯一标签

问题描述 投票:0回答:1

我正在尝试为图中的每个多面面板添加一个独特的标签。下面是我注释过的代码,下面会详细介绍:

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)
  
}

我的问题是,当用

rho
注释每个图时,它会标记所有图(见下图)。 目标 我想简单地用一个数字来注释每个方面,并将其转换为一个因子(122 个级别)。可能是一个简单的修复,但似乎无法让它工作。任何帮助表示赞赏。

问题图示例 enter image description here

编辑尝试乔恩的解决方案后的新示例图: enter image description here

r for-loop ggplot2 geom-text annotate
1个回答
0
投票

虽然这没有直接回答问题,但我想我应该发布一个合理(且更简单)的解决方案的答案。这要归功于米歇尔和乔恩。我没有添加

geom_label
,而是基于串联组(
region
rho
)进行分面。下面是使用
storms
数据的可重现示例。

library(dplyr) 

storms %>% 
  filter(year == 2022) %>% 
  group_by(name) %>%
  mutate(rho = factor(sample(x = -1:1, 1, replace = TRUE)),
         name2 = paste(name, ";", "rho =", rho)) %>%
  ggplot(aes(lubridate::make_datetime(year, month, day, hour), wind)) + 
  geom_point() + 
  geom_line() + 
  #geom_text(aes(label = lat)) + 
  facet_wrap(~name2, scales = "free") 

© www.soinside.com 2019 - 2024. All rights reserved.