我试图用数据框中的坐标和文本注释ggplot2,该数据框是用于绘制数据的数据框的次要数据框.
当前的情节如下所示,数据和代码如下:

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())
Run Code Online (Sandbox Code Playgroud)
请记住,这是在循环中绘制的数据框列表中的一个数据框.
我希望能够做的是根据相应的年份绘制以下数据框(权重)中的文本列.文本表示每年标题变量的权重.
谢谢,
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")
Run Code Online (Sandbox Code Playgroud)
您只需添加一个geom_text()图层,指定新数据,x和y参数即可.但是,您需要将其他规范移出原始ggplot()调用(我已删除其他不必要的位 - 实验室,主题):
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))
Run Code Online (Sandbox Code Playgroud)
