簡體   English   中英

R ggplot2:如何在直角坐標系中用箭頭連接點?

[英]R ggplot2: How to connect points with arrows in Cartesian coordinates?

我對學習如何使用包在笛卡爾平面上使用arrow()函數連接點感興趣。

數據:

Coord = data.frame(x=c(2,-5,7),y=c(4,12,-78))
ggplot()+ coord_cartesian(xlim = c(-136,136), ylim = c(-6,210)) 

我嘗試使用geom_segment ,但不確定如何繼續。

我想學習如何將一個點連接到多個點?

我打算使用這種方法制作足球通行證地圖。

如果我正確理解了您想要的內容,則需要在數據框中創建代表行yend變量xendyend

df <- data.frame(x=c(2,-5,7),y=c(4,12,-78))
df$xend <- c(df$x[2:nrow(df)], NA)
df$yend <- c(df$y[2:nrow(df)], NA)
df <- df[1:(nrow(df)-1),]

geom_segment

ggplot(df)+ coord_cartesian(xlim = c(-136,136), ylim = c(-6,210))+
 geom_segment(aes(x=x,y=y, xend=xend, yend=yend))

更新:從一點到多點:示例:

df <- data.frame(x=c(2,2,2,2,2),y=c(4,4,4,4,4), xend=c(34,3,12,100,-123), yend=c(18,-5,44,200,178))

ggplot(df)+ coord_cartesian(xlim = c(-136,136), ylim = c(-6,210))+
     geom_segment(aes(x=x,y=y, xend=xend, yend=yend))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM