0

I am using geom_segment arrows and I have a few issues I am searching for advice to correct. First, the arrow head is off center on its line. Second, the "main" line of the segment pokes through the arrow head if lineend is set to "butt" or "square". These are both there for me if type="closed", but not as obvious.

df2 <- expand.grid(
  lineend = c('round', 'butt', 'square'),
  linejoin = c('round', 'mitre', 'bevel'),
  stringsAsFactors = FALSE
)
df2 <- data.frame(df2, y = 1:9)

ggplot(df2, aes(x = 1, y = y, xend = 2, yend = y, label = paste(lineend, linejoin))) +
  geom_segment(
    lineend = df2$lineend, linejoin = df2$linejoin,
    size = 1.5, arrow = arrow(length = unit(0.3,"cm"))
  ) +
  geom_text(hjust = 'outside', nudge_x = -0.2) +
  xlim(0.5, 2)

enter image description here

The round bevel arrow is perhaps the most obvious of the first issue, while you can see the corner of the main line poking through the first arrow at the top, for example.

Then there is the old corner glitch when type ="closed".

df2 <- expand.grid(
  lineend = c('round', 'butt', 'square'),
  linejoin = c('round', 'mitre', 'bevel'),
  stringsAsFactors = FALSE
)
df2 <- data.frame(df2, y = 1:9)

ggplot(df2, aes(x = 1, y = y, xend = 2, yend = y, label = paste(lineend, linejoin))) +
  geom_segment(
    lineend = df2$lineend, linejoin = df2$linejoin,
    size = 1.5, arrow = arrow(length = unit(0.3,"cm"), type = "closed")
  ) +
  geom_text(hjust = 'outside', nudge_x = -0.2) +
  xlim(0.5, 2)

enter image description here

I saw these two posts, which pointed to modifying this source code, but with outdated instructions (it appears me that the source code was since updated). This issue is present if lineend="butt", and can be partially fixed by changing to "square", but this still looks lumpy with some size combinations.

Any help would be appreciated for any of these 3, but particularly the first two (which I can't find any other posts about).

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
Andre
  • 101
  • 4
  • this might be a limitation of ggplot2 in how the arrows are drawn, but for purely aesthetic reasons have you tried using different graphical devices? I wonder if that might smooth out some of the arrowheads to meet your preferences – EJJ Oct 23 '20 at 16:00
  • Sorry, I am unfamiliar. What do you mean by graphical devices? Are you referring to base graphics outside of ggplot? – Andre Oct 23 '20 at 16:08
  • 1
    not an expert but when you save any plot generated in R (ggplot2 or base) it needs to pass through a specified [graphical device](https://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/Devices.html). I've found that depending on your OS and the specific device you use, the resulting image resolution/"quality" can vary (beyond reasons I know). An example is when I use Windows and use the default png() device, lines can have jagged appearances. my naive suggestion is to explore if changing the device will smooth out the arrows in the resulting image – EJJ Oct 23 '20 at 16:19
  • I appreciate the suggestion, but I was unable to correct these issues by switching to device to PDF. Thanks regardless. – Andre Oct 23 '20 at 18:22

0 Answers0