[英]How adjust direction of my arrow in ggplot2?
我正在创建一个ggplot
图表,我希望在两点之间有一些箭头。 使用geom_line(arrow = arrow())
可以轻松完成主要任务,但正如您所见,对于某些观察(德国、法国),箭头并不表示“Previo”到“Actual”的正确方向。 这是我的代码:
Countries = c("Eurozone","Eurozone","France","France","Germany","Germany","Japan","Japan","United.Kingdom","United.Kingdom","United.States","United.States")
PMI = c(62.5,63.3,59.3,59.2,66.6,66.4,52.7,53.3,58.9,60.7,59.1,60.6)
Delta1M = c(4.6,0.8,3.2,-0.1,5.9,-0.2,1.3,0.6,3.8,1.8,0.5,1.5)
Mes = c("Previo","Actual","Previo","Actual","Previo","Actual","Previo","Actual","Previo","Actual","Previo","Actual")
t1 <- data.frame(Countries,PMI,Delta1M,Mes)
t1$Mes <- factor(t1$Mes, levels = c("Previo","Actual"))
t1 %>% arrange(Countries) %>%
group_by(Countries) %>%
ggplot(aes(x = PMI, y = Delta1M, group = Countries))+
geom_point(aes(color = Countries, alpha = Mes), size = 3)+
geom_line(arrow = arrow(length=unit(0.15,"cm"), ends="last", type = "closed"))+
geom_hline(yintercept = 0)+
geom_vline(xintercept = 50)+
theme_minimal()+
scale_alpha_manual(values = c(0.3,1))
按 Mes 排序并使用geom_path
似乎可以工作,方法是按排序数据的顺序绘制线条,而不是基于 x 级数。
t1 %>% arrange(Countries, Mes) %>%
ggplot(aes(x = PMI, y = Delta1M, group = Countries))+
geom_point(aes(color = Countries, alpha = Mes), size = 3)+
geom_path(arrow = arrow(length=unit(0.15,"cm"), ends="last", type = "closed"))+
geom_hline(yintercept = 0)+
geom_vline(xintercept = 50)+
theme_minimal()+
scale_alpha_manual(values = c(0.3,1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.