[R] ggplot2, arrows and polar coordinates

Pascal Oettli kridox at ymail.com
Fri Mar 15 10:53:21 CET 2013


Dear R users,

The following issue has been already documented, but, if I am not 
mistaken, not yet solved.

This issue appears while trying to plot arrows with "geom_segment" 
(package ggplot2), with polar coordinates ("coord_polar"). The direction 
of some arrows is wrong (red rectangle). Please find herewith an example.

Does someone know how to deal with that issue?

Best Regards,
Pascal Oettli


#----------------------------------------------------------------------------------
# Example adapted from the help page of "geom_segment"

library(ggplot2)
library(grid)

d <- data.frame(x1=-135.3, x2=-158.3, y1=37.2, y2=45.2)

p <- ggplot(seals, aes(x = long, y = lat))

p1 <-
   ggplot() +
   coord_cartesian() +
   geom_rect(data=d, mapping=aes(xmin=x1, xmax=x2, ymin=y1, ymax=y2), 
fill="red", color="red", alpha=0.5) +
   geom_segment(data=seals, aes(x = long, y = lat, xend = long + 
delta_long, yend = lat + delta_lat), arrow = arrow(length = unit(0.2,"cm")))

p2 <-
   ggplot() +
   coord_polar() +
   geom_rect(data=d, mapping=aes(xmin=x1, xmax=x2, ymin=y1, ymax=y2), 
fill="red", color="red", alpha=0.5) +
   geom_segment(data=seals, aes(x = long, y = lat, xend = long + 
delta_long, yend = lat + delta_lat), arrow = arrow(length = unit(0.2,"cm")))

grid.newpage()
pushViewport(viewport(layout = grid.layout(3, 2, heights = unit(c(0.5, 
0.5, 5), "null"))))
grid.text("Example taken from '?geom_segment'", vp = 
viewport(layout.pos.row = 1, layout.pos.col = 1:2))
grid.text("Cartesian coordinates", vp = viewport(layout.pos.row = 2, 
layout.pos.col = 1))
grid.text("Polar coordinates", vp = viewport(layout.pos.row = 2, 
layout.pos.col = 2))
print(p1, vp = viewport(layout.pos.row = 3, layout.pos.col = 1))
print(p2, vp = viewport(layout.pos.row = 3, layout.pos.col = 2))



More information about the R-help mailing list