考虑以下轨迹:
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
来自箭头定义本身的美学?
最佳答案
我们可以通过使用具有实线的单独几何图形绘制箭头来解决此问题。像这样的东西:
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")))
关于r - ggplot 带虚线的箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48650464/