How to draw a nice arrow in ggplot2

落花浮王杯 提交于 2019-11-30 06:00:01

I usually use geom_segment to create arrow. But to do that we need to modify the data from "long" to "wide" format (usually using dcast from reshape2 or data.table package). But this time I tried using base's reshape function.

ggplot(DATA, aes(x=VALUE, y=NAME)) + 
  geom_point(size=5, aes(colour=YEAR)) +
  geom_segment(data = reshape(DATA, v.names="VALUE", idvar = "NAME", timevar = "YEAR", direction = "wide"),
               aes(x=VALUE.2011, xend=VALUE.2016, y=NAME, yend=NAME), size = 2,
               arrow = arrow(length = unit(0.5, "cm")))

EDIT: I just found that same issue pertains for "closed" type arrows. For now, try to save the plot as a vector graph (pdf or svg, using ggsave or Export menu in Plots tab). The result is not "messy".

In the latest (and I mean devtools::install_github("tidyverse/ggplot2") latest, as of writing this answer - I'm sure they'll mainline it soon enough) version of ggplot2, there is a linejoin argument to geom_segment. Using linejoin='mitre' will provide crisp edges. See the following for details.

There is a very simple, but somewhat "hacky" solution to this.

The idea is to draw the lines first (at the desired thickness, but no arrowheads), but a little bit shorter (can be calculated in some cases). Then just draw a second line, without changing the size with arrowheads. The resulting overlay will look the way you want.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!