1

Following this link:

ggplot2 polar plot arrows

I have been wondering how to get closed arrows on a polar plot in ggplot2.

Here is my code:

# make some data     
    polar <- structure(list(degree = c(120L, 30L, -120L, 60L, 150L, -90L, 
    -60L, 0L), value = c(0.5, 0.2, 0.2, 0.5, 0.4, 0.14, 0.5, 0.6)), .Names = c("degree", 
    "value"), class = "data.frame", row.names = c(NA, -8L))

    require(ggplot2)
    require(grid)

    base <- ggplot(polar, aes(x=degree, y=value, axis.x = NULL, axis.y = NULL))
    p <- base 
    p <- p + geom_segment(aes(y=0, xend=degree, yend=value)) # add the lines


    awid <- 1
    p <- p + geom_segment(aes(y=0, xend=degree, yend=value)) + geom_segment(aes(y=value-0.05,yend=value,x=degree-awid/value,xend=degree)) +  geom_segment(aes(y=value-0.05,yend=value,x=degree+awid/value,xend=degree))
    p

However, the arrowheads I get are open arrows, not closed. I was wondering how to replace the arrowheads with (essentially) filled triangles and still be in polar coordinates. Any suggestions?

Thanks a bunch in advance!

PS; updated to add issues with arrow. Here is the code I ran:

 require(ggplot2)
 require(grid)

 # Data     
 polar <- structure(list(degree = c(120L, 30L, -120L, 60L, 150L, -90L, -60L, 0L), 
                         value = c(0.5, 0.2, 0.2, 0.5, 0.4, 0.14, 0.5, 0.6)), 
                    .Names = c("degree", "value"), 
                    class = "data.frame", row.names = c(NA, -8L))

 ggplot(polar, aes(x=degree, y=value)) +
   geom_segment(aes(y=0, xend=degree, yend=value), arrow=arrow(type="closed")) +
   coord_polar()

And here is the plot I get: enter image description here

user3236841
  • 1,088
  • 1
  • 15
  • 39

1 Answers1

1

Using the arrow() function from grid, you only need one call to geom_segment.

 require(ggplot2)
 require(grid)

 # Data     
 polar <- structure(list(degree = c(120L, 30L, -120L, 60L, 150L, -90L, -60L, 0L), 
                         value = c(0.5, 0.2, 0.2, 0.5, 0.4, 0.14, 0.5, 0.6)), 
                    .Names = c("degree", "value"), 
                    class = "data.frame", row.names = c(NA, -8L))

 ggplot(polar, aes(x=degree, y=value)) +
   geom_segment(aes(y=0, xend=degree, yend=value), arrow=arrow(type="closed")) +
   coord_polar()

enter image description here

eipi10
  • 91,525
  • 24
  • 209
  • 285
  • 2
    Thanks! Could you provide me the complete code? I tried this earlier (and now) and the arrowheads all point westward. – user3236841 Oct 03 '15 at 15:56
  • I've added all the code I ran. All I really did was import your `polar` data and then run the `ggplot` code in my original answer. – eipi10 Oct 03 '15 at 16:23
  • I cannot reproduce this either. – Axeman Oct 03 '15 at 16:27
  • Hmm. I ran the code in a clean session and got the same result. I'm running R 3.2.2, RStudio 0.99.484, and ggplot2 version 1.0.1.9003 on OSX 10.10.4. – eipi10 Oct 03 '15 at 16:32
  • I am also on R.3.2.2. My ggplot2 is at 1.0.1 also. \n packageVersion("grid") [1] ‘3.2.2’ > packageVersion("ggplot2") [1] ‘1.0.1’ – user3236841 Oct 03 '15 at 16:50
  • Same versions here. I'm on Windows 10 though. – Axeman Oct 03 '15 at 18:44
  • I am on linux, and I ran the code on a clean session also. I wonder what is different with eipi10's code. I don't use Rstudio but why should that have anytihing to do with it. – user3236841 Oct 03 '15 at 19:09
  • When you say you can't reproduce the plot I posted, what, specifically, happens when you run the code? Are you also getting all arrows pointing westward? – eipi10 Oct 03 '15 at 19:46
  • I have added both the code and the plot which gives issues. All done from a clean R session in a directory with ls() yielding character(0) at the start. – user3236841 Oct 03 '15 at 20:39
  • Yes, I'm getting the same things as OP. I'm using RStudio. Perhaps it works differently on the Quartz device? I don't know. – Axeman Oct 03 '15 at 21:55