gpt4 book ai didi

r - 从第二个数据帧注释 ggplot2

转载 作者:行者123 更新时间:2023-12-01 16:26:36 24 4
gpt4 key购买 nike

我正在尝试使用数据框中的坐标和文本来注释 ggplot2,该数据框是用于绘制数据的次要数据框。

当前绘图如下所示,数据和代码如下:

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

请记住,这是循环绘制的一系列数据帧中的一个数据帧。

我想要做的是根据相应的年份绘制以下数据框(权重)中的文本列。文本表示每年标题变量的权重。

谢谢

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

最佳答案

您可以简单地添加一个 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))

enter image description here

关于r - 从第二个数据帧注释 ggplot2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23463866/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com