# [R] ggplot2 - curveGrob - annotation_custom

milena milena.stat at gmail.com
Wed Jan 20 22:49:01 CET 2016

```Dear R users,

I am struggling to understand *curveGrob* and *annotation_custom* command
in *ggplot*.

In brief my issue can be approximated to drawing a downward sloping arrow
curve
from point (5,5) to (10,0) but keep on getting (5,0) to (10,5) |=> see
attached arrows.png

require(grid)
g<-qplot(c(0,10),c(0,10))
myCurve<-curveGrob(0, 0, 1, 1, default.units = "npc",
curvature = 0.3, angle = 90, ncp = 20, shape = 1,
square = FALSE, squareShape = 1,
inflect = FALSE, arrow = arrow(), open = TRUE,
debug = FALSE,
name = NULL, gp = gpar(col="blue"), vp = NULL)

myCurve2<-curveGrob(0, 0, 1, 1, default.units = "npc",
curvature = -0.3, angle = 60, ncp = 10, shape = 1,
square = FALSE, squareShape = 1,
inflect = FALSE, arrow = arrow(), open = TRUE,
debug = FALSE,
name = NULL, gp = gpar(), vp = NULL)

g +
annotation_custom(grob=myCurve,0,10,0,10) + # plot from 0,0 to 10,10
*annotation_custom(grob=myCurve,5,10,5,0) + # !!!!!this should draw from
(5,5) to (10,0) but it does not*
annotation_custom(grob=myCurve2,2.5,6,2.5,10)   # plot from 2.5,2.5 to 6,6

In more detail:

I am building a shiny application (like in shiny_pic.png).
after choosing country 1 and country 2, R would plot a map of Europe,
highlight the two selected countries with different colors
and draw a curved arrow from country 1 in its respective color towards
country2
and vice versa from country 2 to country 1 in the color of country 2.

size of the arrow would correspond with trade between the two countries.

since it is a shiny application I need a code that works fast.
map does not have to be beautiful, the speed of the application is a
priority.

for the curved arrows I tried geom_curve but either the arrow was not drawn
at all
or would only appear after a wait for several minutes...

I searched stackoverflow and have found a very similar problem:
http://stackoverflow.com/questions/20216179/plot-curved-lines-between-two-locations-in-ggplot2

and although it works very well for a couple of countries like Italy-Poland
(the only thing I had to change inside curveGrov2(arrow(ends="first"))
for the arrow head to appear at the beginning, not the end.

but the problem starts with country combinations like Austria-Cyprus or
France-Greece.
It seems that x-coordinates are respected, but y-coordinates inverted
to draw an *upward* looking curve.

it has been a while that I am looking at this problem so perhaps I have
lost a fresh eye.
is there some parameter I need to tweak in curveGrob function?

fyi: my map does not have to be plotted with ggplot2.
I am open to other solutions such as spplot etc.
as long as it works fast (shiny)
and draws the arrows as desired
with every country combination and code execution.

I would be grateful for any help and I am open to every feedback
if there is something I can improve in my code please fell free...

Milena

aka suzukiblue
-------------- next part --------------
A non-text attachment was scrubbed...
Name: shiny_pic.png
Type: image/png
Size: 89577 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20160120/2bdb176e/attachment-0004.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Italy-Poland.png
Type: image/png
Size: 11819 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20160120/2bdb176e/attachment-0005.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Austria-Cyprus.png
Type: image/png
Size: 12047 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20160120/2bdb176e/attachment-0006.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: France-Greece.png
Type: image/png
Size: 11894 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20160120/2bdb176e/attachment-0007.png>
```