考虑以下轨迹:
test = data.frame(x = c(9500, 25500, 47500), y = c(10.03, 7.81, 0.27))
我想将此绘制为ggplot2
的路径,并用箭头标识方向:
ggplot(test) + aes(x = x, y = y) +
geom_path(size = 1,
arrow = arrow(type = "open", angle = 30, length = unit(0.1, "inches")))
看起来很好。但是,如果我想使用虚线,箭头也会用虚线绘制,看起来很糟糕:
ggplot(test) + aes(x = x, y = y) +
geom_path(linetype ="dashed", size = 1,
arrow = arrow(type = "open", angle = 30, length = unit(0.1, "inches")))
有没有办法从箭头定义本身中排除linetype
美学?
答案 0 :(得分:2)
我们可以通过使用具有实线的单独几何体绘制箭头来解决此问题。像这样:
geom_end_arrow = function(path, arrow, ...) {
path = tail(path,2)
x.len = diff(path$x)/1000
y.len = diff(path$y)/1000
path$x[1] = path$x[2] - x.len
path$y[1] = path$y[2] - y.len
geom_path(data = path, mapping = aes(x=x, y=y), arrow=arrow, ...)
}
ggplot(test) + aes(x = x, y = y) +
geom_path(linetype ="dashed", size = 1) +
geom_end_arrow(test, size = 1, arrow = arrow(type = "open", angle = 30, length = unit(0.1, "inches")))