Question

I am trying to annotate a ggplot2 with coordinates and text from a data frame that is secondary to the one used to plot the data.

The current plot looks like this, with the data & code following:

enter image description here

U21.sub = structure(list(Year = c(2012, 2012, 2012, 2012, 2012, 2012, 2013, 
2013, 2013, 2013, 2013, 2013, 2014, 2014, 2014, 2014, 2014, 2014, 
2012, 2013, 2014), Criteria = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("Connectivity", "Environment", "Output", "Resources", 
"Total"), class = "factor"), Weighting = c(0.1, 0.1, 0.1, 0.1, 
0.1, 0.1, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.2, 0.2, 0.2, 
0.2, 0.2, 0.2, NA, NA, NA), Rank = c(4L, 5L, 6L, 17L, 36L, 48L, 
2L, 7L, 9L, 10L, 16L, 50L, 3L, 8L, 10L, 13L, 15L, 45L, NA, NA, 
NA), Country = structure(c(2L, 30L, 49L, 7L, 50L, 9L, 2L, 49L, 
30L, 50L, 7L, 9L, 49L, 2L, 7L, 30L, 50L, 9L, 51L, 51L, 51L), .Label = c("Argentina", 
"Australia", "Austria", "Belgium", "Brazil", "Bulgaria", "Canada", 
"Chile", "China", "Croatia", "Czech Republic", "Denmark", "Finland", 
"France", "Germany", "Greece", "Hong Kong SAR", "Hungary", "India", 
"Indonesia", "Iran", "Ireland", "Israel", "Italy", "Japan", "Korea", 
"Malaysia", "Mexico", "Netherlands", "New Zealand", "Norway", 
"Poland", "Portugal", "Romania", "Russian Federation", "Saudi Arabia", 
"Serbia", "Singapore", "Slovakia", "Slovenia", "South Africa", 
"Spain", "Sweden", "Switzerland", "Taiwan", "Thailand", "Turkey", 
"Ukraine", "United Kingdom", "United States", "Asia: Average"
), class = "factor"), Score = c(94.5, 83.2, 82.5, 55.3, 35.3, 
12.8, 96.7, 81.1, 73, 72.2, 63.3, 12.3, 91.4, 87.6, 80.2, 75.9, 
74, 29.8, 40.37, 41.43, 54.8), Contribution.to.total.score = c(9.45, 
8.32, 8.25, 5.53, 3.53, 1.28, 14.5, 12.2, 11, 10.8, 9.5, 1.8, 
18.28, 17.52, 16.04, 15.18, 14.8, 5.96, NA, NA, NA)), .Names = c("Year", 
"Criteria", "Weighting", "Rank", "Country", "Score", "Contribution.to.total.score"
), row.names = c(148L, 149L, 150L, 161L, 180L, 192L, 392L, 397L, 
399L, 400L, 406L, 440L, 643L, 648L, 650L, 653L, 655L, 685L, 741L, 
746L, 751L), class = "data.frame")

p = ggplot(data = U21.sub[[i]], aes(x = as.factor(Year), y = Score, group = Country,     colour = Country)) + 
      geom_line(size = 1.25) + labs(title = names[i], x = "Year") + theme(legend.title = element_blank()) 

Keep in mind that this is one data frame from a list of dataframes that are being plotted in a loop.

What I want to be able to do is plot the text column from the following data frame (weightings) in line with the respective year. The text represents the weight of the title variable in each year.

Thanks,

weightings = structure(list(x = structure(1:3, .Label = c("2012", "2013", 
"2014"), class = "factor"), y = c(20, 20, 20), text = structure(1:3, .Label = c("10%", 
"15%", "20%"), class = "factor")), .Names = c("x", "y", "text"
), row.names = c(NA, -3L), class = "data.frame")
Was it helpful?

Solution

You can simply add a geom_text() layer, specifying new data, x and y arguments. However, you need to move other specifications out of the original ggplot() call (I've removed other unnecessary bits - labs, theme):

p = ggplot(data = U21.sub, aes(x = as.factor(Year), y = Score)) + 
  geom_line(aes(group = Country, colour = Country), size = 1.25) + 
  geom_text(data = weightings, aes(x = x, y = y, label = text))

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top