【问题标题】:how to use facet_wrap to ggplot multiple location point in R?如何使用 facet_wrap 在 R 中绘制多个位置点?
【发布时间】:2021-03-05 13:22:55
【问题描述】:

我正在尝试使用facet_wrapggplot 功能plot 多个位置数据。我在创建 legends 时遇到了麻烦(完全错过了 95% 的置信区间)。以下是我的代码,如果有任何建议,我将不胜感激。

library(ggplot2)
library(lubridate)

set.seed(123)

DF1 <- data.frame(Date = seq(as.Date("2001-01-01"), to = as.Date("2005-12-31"), by = "1 month"),
                  Ob = runif(60,1,5), L95 =runif(60, 0,4), U95 = runif(60,2,7), Sim = runif(60,1,5),
                  Loc = rep("Upstream", 60))

DF2 <- data.frame(Date = seq(as.Date("2001-01-01"), to = as.Date("2005-12-31"), by = "1 month"),
                  Ob = runif(60,1,5), L95 =runif(60, 0,4), U95 = runif(60,2,7), Sim = runif(60,1,5),
                  Loc = rep("Downstream", 60))

DF <- dplyr::bind_rows(DF1,DF2)

DF$Loc <- factor(DF$Loc, levels = c("Upstream","Downstream"))


ggplot(DF, aes(x = Date))+
  geom_ribbon(aes(ymin = L95, ymax = U95), fill = "grey30", alpha = 0.4)+
  geom_line(aes(y = Ob, color = "blue"), size = 1 )+
  geom_line(aes(y = Sim, color = "black"), size =  1, linetype = "dashed")+
  geom_vline(xintercept = as.Date("2004-12-01"),color = "red", size = 1.30)+
  facet_wrap(~ Loc, ncol = 1, scales = "free_y")+ 
  theme_bw()+
  scale_color_identity(guide = "legend", breaks = c("grey30", "blue", "black"),
                       labels = c("95% confidence bound", "Observation","Simulation"))

【问题讨论】:

    标签: r dataframe ggplot2 plot lubridate


    【解决方案1】:

    您的填充在aes 函数之外,因此它不会出现在图例中。

    ggplot(DF, aes(x = Date))+
      geom_ribbon(aes(ymin = L95, ymax = U95, color = "grey30"), fill = "grey30", alpha = 0.4)+
      geom_line(aes(y = Ob, color = "blue"), size = 1 )+
      geom_line(aes(y = Sim, color = "black"), size =  1, linetype = "dashed")+
      geom_vline(xintercept = as.Date("2004-12-01"),color = "red", size = 1.30)+
      facet_wrap(~ Loc, ncol = 1, scales = "free_y")+ 
      theme_bw()+
      scale_color_identity(guide = "legend", breaks = c("grey30", "blue", "black"),
                           labels = c("95% confidence bound", "Observation","Simulation"))
    
      ggplot(DF, aes(x = Date))+
        geom_ribbon(aes(ymin = L95, ymax = U95, fill = "grey30"), alpha = 0.4)+
        geom_line(aes(y = Ob, color = "blue"), size = 1 )+
        geom_line(aes(y = Sim, color = "black"), size =  1, linetype = "dashed")+
        geom_vline(xintercept = as.Date("2004-12-01"),color = "red", size = 1.30)+
        facet_wrap(~ Loc, ncol = 1, scales = "free_y")+ 
        theme_bw()+
        scale_color_identity(guide = "legend", breaks = c( "blue", "black"),
                             labels = c( "Observation","Simulation"),
                             name = 'Legend')+
        scale_fill_identity(guide = "legend", labels = c("95% confidence bound"), 
                            name=NULL)+
        theme(legend.spacing.y = unit(-0.2, "cm"))+
        theme(legend.title = element_text(margin=margin(b = 0.4, unit='cm')))
    

    【讨论】:

    • 我无法删除图例周围的框 - 我试过 theme(legend.key = element_rect(colour = NA, fill = NA), legend.box.background = element_blank()) 但它没有用 - 对此有什么想法吗?
    • 不确定我是否理解!你在寻找类似stackoverflow.com/questions/49373461/… 的东西吗?
    • 是的,但它在这里不起作用——不知道为什么?你能去掉图例周围的方框吗?
    • 你说得对,这似乎不起作用。在答案中查看我的编辑
    • 谢谢@Liman。有没有办法只在第一个方面添加注释?我试过annotate(geom = "Text", x = as.Date("2000-01-01"), y = 5, label = "Calibration"),但这在两个方面都添加了注释。
    【解决方案2】:

    我会这样做。

    首先,您将 fill 用于功能区,而不是 color。其次,您需要将fill 实际映射到aes 中,而不仅仅是将其设置在aes 之外。然后我会在aes 调用中给出您想要的名称,并使用scale_*_manual 设置您想要的值:

    ggplot(DF, aes(x = Date))+
      geom_ribbon(aes(ymin = L95, ymax = U95, fill = "95% confidence bound"), alpha = 0.4)+
      geom_line(aes(y = Ob, color = "Observation"), size = 1 )+
      geom_line(aes(y = Sim, color = "Simulation"), size =  1, linetype = "dashed")+
      geom_vline(xintercept = as.Date("2004-12-01"),color = "red", size = 1.30)+
      facet_wrap(~ Loc, ncol = 1, scales = "free_y")+ 
      theme_bw()+
      scale_color_manual(values = c('blue', 'black'), name = NULL) +
      scale_fill_manual(values = 'grey30', name = NULL)
    

    有很多有效的方法可以解决这个问题,但有多少人这样做。

    【讨论】:

      猜你喜欢
      • 2022-11-12
      • 2020-04-29
      • 2022-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多