我正在嘗試創建一個類似于以下示例的圖表。也就是說,我希望添加指向 2019 年到 2021 年運動方向的箭頭。

我有以下示例資料集:
structure(list(jurisdiction = c("Alabama", "Illinois", "Nebraska",
"Oklahoma", "Alabama", "Illinois", "Nebraska", "Oklahoma"), xvar = c(4.9,
1.3, 3.4, 7.2, 3.2, 3.5, 4.7, 5.5), year = c("2021", "2021",
"2021", "2021", "2019", "2019", "2019", "2019"), pair = c(1,
7, 15, 19, 1, 7, 15, 19)), row.names = c(69L, 46L, 105L, 44L,
1L, 10L, 22L, 28L), class = "data.frame")
我使用了以下 ggplot 代碼
ggplot(dt,aes(x=xvar,y=reorder(jurisdiction, -pair),color=year))
geom_point()
geom_line(aes(group=pair))
創建以下 ggplot

我正在努力弄清楚如何在點之間添加箭頭。我已經嘗試了以下代碼,這些代碼基于此stackoverflow 討論:
geom_segment(data=reshape(dt,v.names="rh_percentage",idvar="jurisdiction",timevar="year",direction="wide"),
aes(x=rh_percentage.2019,xend=rh_percentage.2021,y=jurisdiction,yend=jurisdiction),size=2,
arrow=arrow(length=unit(0.5,"cm")))
不幸的是,這會產生以下錯誤:
Error in FUN(X[[i]], ...) : object 'rh_percentage.2019' not found
In addition: Warning message:
In reshapeWide(data, idvar = idvar, timevar = timevar, varying = varying, :
some constant variables (xvar) are really varying
我將不勝感激人們提出的任何建議。我可能只是把問題表述錯了,但我還沒有在 StackOverflow 上找到關于這個特定問題的討論。謝謝您的幫助!
uj5u.com熱心網友回復:
切換到geom_path你可以這樣做:
library(ggplot2)
ggplot(dt, aes(x = xvar, y = reorder(jurisdiction, -pair), color = year))
geom_point()
geom_path(aes(group = pair),
arrow = arrow(ends = "first", length = unit(0.1, "inches")),
show.legend = FALSE, color = "grey65")

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/466715.html
上一篇:每N個月將網格線添加到geom_point圖中,日期為x軸
下一篇:如何在y軸上排序時間
