代码之家  ›  专栏  ›  技术社区  ›  ulima2_

仅在一个方面内使用ggplot的注释

  •  4
  • ulima2_  · 技术社区  · 7 年前

    我正在做一个复杂的ggplot可视化,我为不同的国家绘制了几个时间序列(在方面)。我想手动注释该系列中只有一个方面,使其更容易快速看到哪个是哪个,而不必查看图例。

    下面是我的简化示例:

    library(tidyverse)
    
    df <- tribble(
      ~year, ~country, ~series1, ~series2, 
      #--|--|--|----
      2003, "USA", 8, 5,
      2004, "USA", 9, 6, 
      2005, "USA", 11, 7, 
      2006, "USA", 10, 8,
      2007, "USA", 11, 4,
      2008, "USA", 14, 10,
      2009, "USA", 16, 12,
      2010, "USA", 12, 8,
      2011, "USA", 12, 13,
      2012, "USA", 13, 10,
      2013, "USA", 11, 5,
      2005, "FRA", 5, 6, 
      2006, "FRA", 6, 8, 
      2007, "FRA", 5, 7, 
      2008, "FRA", 4, 8,
      2009, "FRA", 9, 11,
      2010, "FRA", 7, 9, 
      2011, "FRA", 14, 11,
      2012, "FRA", 7, 11, 
      2013, "FRA", 6, 6,
      2014, "FRA", 5, 7,
      2015, "FRA", 4, 5
    )
    
    ggplot(df, aes(x = year)) +
      geom_line(aes(y = series1, color = "First series")) +
      geom_line(aes(y = series2, color = "Second series")) +
      facet_wrap(~country) +
      annotate("text", x = 2014, y = 13, label = "First series") +
      annotate("text", x = 2014, y = 8, label = "Second series")
    

    enter image description here

    回复@user20650' hint ,我也试过:

    ann_text <- tribble(
      ~year, ~country, ~series1, ~series2,
      #--|--|--|----
      2014, "USA", 13, 8
    )
    
    ggplot(df, aes(x = year)) +
      geom_line(aes(y = series1, color = "First series")) +
      geom_line(aes(y = series2, color = "Second series")) +
      facet_wrap(~country) +
      geom_text(data = ann_text, label = "Text")
    

    这会产生以下错误:

    Error: geom_text requires the following missing aesthetics: y
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   ulima2_    7 年前

    最后这样解决了:

    library(tidyverse)
    
    df <- tribble(
      ~year, ~country, ~series1, ~series2, 
      #--|--|--|----
      2003, "USA", 8, 5,
      2004, "USA", 9, 6, 
      2005, "USA", 11, 7, 
      2006, "USA", 10, 8,
      2007, "USA", 11, 4,
      2008, "USA", 14, 10,
      2009, "USA", 16, 12,
      2010, "USA", 12, 8,
      2011, "USA", 12, 13,
      2012, "USA", 13, 10,
      2013, "USA", 11, 5,
      2005, "FRA", 5, 6, 
      2006, "FRA", 6, 8, 
      2007, "FRA", 5, 7, 
      2008, "FRA", 4, 8,
      2009, "FRA", 9, 11,
      2010, "FRA", 7, 9, 
      2011, "FRA", 14, 11,
      2012, "FRA", 7, 11, 
      2013, "FRA", 6, 6,
      2014, "FRA", 5, 7,
      2015, "FRA", 4, 5
    )
    
    ann_text1 <- tribble(
      ~year, ~country, ~series1, ~series2,
      #--|--|--|----
      2014, "USA", 13, 8
    )
    
    ann_text2 <- tribble(
      ~year, ~country, ~series1, ~series2,
      #--|--|--|----
      2014, "USA", 8.5, 9
    )
    
    ggplot(df, aes(x = year)) +
      geom_line(aes(y = series1, color = "First series")) +
      geom_line(aes(y = series2, color = "Second series")) +
      facet_wrap(~country) +
      geom_text(data = ann_text1, aes(y = series1), label = "First") +
      geom_text(data = ann_text2, aes(y = series1), label = "Second")
    

    enter image description here