I am interested in learning how to connect points with the arrow()
function on a Cartesian plane using the ggplot2 package.
Data:
Coord = data.frame(x=c(2,-5,7),y=c(4,12,-78))
ggplot()+ coord_cartesian(xlim = c(-136,136), ylim = c(-6,210))
I tried using geom_segment
, but I am not sure how to proceed.
I wanted to learn how to connect one point to many points ?
I intend to use this method to make pass maps for soccer.
If I understand correctly what you want, you need to create variables xend
and yend
in your data frame that represent the ends of the lines.
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),]
Plot with 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))
UPDATE: From one point to many points: Example:
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))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.