4

I'm doing a complicated ggplot visualization, where I plot several time series for different countries (in facets). I would like to manually annotate the series for only one of the facets to make it easier to quickly see which is which, without having to look over to the legend.

Here's my simplified example:

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

which produces the following figure:

enter image description here

How can I remove the annotations in the left facet (which I've manually crossed out)?

Update: Responding to @user20650' hint, I've also tried this:

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

Which produces the following error:

Error: geom_text requires the following missing aesthetics: y
ulima2_
  • 1,276
  • 1
  • 13
  • 23

1 Answers1

3

Solved it like this in the end:

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

Which makes:

enter image description here

ulima2_
  • 1,276
  • 1
  • 13
  • 23