我想创建一个基于数据MIA_YEAR的线图。它看起来已经相当不错了,但我想把那些数据点移到(几乎)接触线的图下。或者用数学术语来说:如果斜率增加,则数据点应在线图下方,如果斜率减小,则数据点应在图上方,而不是所有数据点都在线上方。that is how it looks like until now -->。
MIA_Year %>%
ggplot(aes(x = YEAR, y = Percent)) +
geom_line(group=1, color = "steelblue", linewidth = 1) + #group = 1 needed when x is a factor
geom_text(data = MIA_Year, aes(label=n), vjust = -0.9) +
scale_y_continuous(limits = c(0, 100), breaks = seq(0, 100, by = 10)) +
labs(x = "", y = "Percent of Articles") +
theme(axis.text.x = element_text(angle = 45, hjust = 1), panel.border = element_rect(fill = NA), panel.background = element_rect(fill = "white"), panel.grid = element_line(colour = "grey85"))
这是我的代码。我尝试包含像这样的vjust = ifelse(diff(Percent) < 0, -0.9, 0.9)
,但它不适用于ggplot,我得到了这个错误,尽管对象肯定在MIA_YEAR中:(差异中的误差(y =百分比):没有找到对象'Percent')(也许你也可以向我解释一下,为什么它不工作?)
非常感谢你的帮助!
编辑1.这里是数据(我希望我得到了点与复制的权利)
structure(list(YEAR = structure(1:13, .Label = c("1990", "2000",
"2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017",
"2018", "2019", "2020"), class = "factor"), n = c(25L, 27L, 74L,
95L, 79L, 79L, 98L, 98L, 102L, 79L, 101L, 86L, 99L), Percent = c(26.6,
25.23, 36.63, 48.72, 44.63, 36.24, 43.75, 42.24, 44.54, 34.96,
46.98, 35.98, 41.42)), class = c("grouped_df", "tbl_df", "tbl",
"data.frame"), row.names = c(NA, -13L), groups = structure(list(
YEAR = structure(1:13, .Label = c("1990", "2000", "2010",
"2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018",
"2019", "2020"), class = "factor"), .rows = structure(list(
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -13L), .drop = TRUE))
1条答案
按热度按时间0dxa2lsx1#
基于你的想法,这里有一个工作方法,正如我的评论中提到的,我“轻推”标签的y位置,并使用
vjust
进行对齐。另外请注意,我在放置标签时考虑了lead
的第一个差异或斜率。