I have a data frame that I want to plot a path from. The data frame also indicates the direction the arrows should have. I have been using the ggplot2
package and the geom_path()
function for this, however, I want to add arrows that indicate the direction of the path. I used the arrow()
function but I don't know how to make the arrows the way I would like (example below).
library(ggplot2)
X <- c(0.596, 0.641, 0.695, 0.741, 0.788, 0.837,
0.887, 0.937, 0.993, 0.984, 0.934, 0.886,
0.838, 0.778, 0.738, 0.681, 0.642, 0.593)
Y <- c(23.630, 24.085, 24.643, 25.067, 25.491, 25.899,
26.305, 26.670, 27.049, 27.025, 26.836, 26.636,
26.429, 26.152, 25.965, 25.664, 25.442, 24.510)
Direction <- c(rep('up', 9), rep('down', 9))
DF <- data.frame(X, Y, Direction)
ggplot(data = DF,
aes(x = X,
y = Y)) +
geom_path(arrow = arrow(ends = "both"))
ggplot2 result
Desired result
You can try this. It assigns up arrow and down arrow based on the values in Direction
. It does not follow the path orientation though.
DF %>%
ggplot(aes(x = X, y = Y, shape = factor(Direction))) + # converting to factor
scale_shape_manual(values = c(24, 25)) + # 24 means up arrow and 25 down arrow
geom_point(size = 2, fill = "black") +
geom_line() +
theme(legend.position = "none")