Specifying gpar settings for grid arrows in R
Asked Answered
M

1

8

I'm developing a plot of proportionally sized arrows, and have a workflow for size, direction and arrow-head size, but there are a couple of issues with the arrowheads: 1. they have rounded line-join and 2. they don't close (see bottom right corner) even when type='closed'.

require(ggplot2)
require(grid)
d = data.frame(x = 1:10, y = 0, size = 1:10)

ggplot(d, aes(x, y, size = size)) +
  geom_segment(aes(xend = x, yend = y + size), 
               arrow = arrow(length = unit(d$size, "mm"), type='closed')) +
  scale_size(range = c(2, 4))

enter image description here

Arrows are based on grid graphics, but I cannot figure out how to specify the setting. get.gpar() yields:

$lineend
[1] "round"

$linejoin
[1] "round"

but gpar(linejoin = 'mitre', lineend = 'butt') does not change this. Is there any way to change these settings? Thanks in advance.


Edit

Plot image including grid.segments arrow added:

enter image description here

Matson answered 18/12, 2013 at 12:20 Comment(5)
I don't get the same output, what graphics device etc. are you using?Paralyse
do you see the same problem with raw grid, e.g. grid.segments(x0=0.2, y0=0.2, x1=0.6, y1=0.6, arrow=arrow(type="closed"), gp=gpar(lwd=10, linejoin="mitre", fill="black"))Paralyse
Ah, it seems problem 2 (polygon closure) was just R gui's graphics device. But the rounding problem is still an issue (see new plot image above). This is result of png and pdf outputs (ggsave and base).Matson
geom_segment doesn't seem to use linejoin and linemitre, you would have better luck with geom_path. You could post a feature request on github.Paralyse
Thanks for the tips baptiste. Have submitted a feature requestMatson
M
4

Found a solution (credit to @baptiste for the pointer). If you use the geom_segment source code in github and make the following alterations you will end up with lovely pointy arrowheads:

1 require(proto)

2 add ggplot::: in first line of GeomSegment (currently line 51) as follows:

GeomSegment <- proto(ggplot:::Geom, {

3 add argument linejoin = 'mitre' to gpar in segmentsGrob (line 23):

lwd=size * .pt, lty=linetype, lineend = lineend, linejoin = 'mitre'),

Run both functions and you'll end up with:

enter image description here

Matson answered 18/12, 2013 at 15:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.