6

I have a plot in polar coordinates. I used geom_path to connect the points, but I'd like the paths to be straight lines. Here's what I have so far:

example <- data.frame(c(5,4,3),c(0.9,1.1,0.6))

colnames(example) <- c("r", "theta")

myplot <- ggplot(example, aes(r, theta)) + geom_point(size=3.5) +
  coord_polar(theta="y", start = 3/2*pi, direction=-1) + 
  scale_x_continuous(breaks=seq(0,max(example$r)), lim=c(0, max(example$r))) + 
  scale_y_continuous(breaks=round(seq(0, 2*pi, by=pi/4),2), expand=c(0,0), lim=c(0,2*pi)) +
  geom_text(aes(label=rownames(example)), size=4.4, hjust=0.5, vjust=-1) + 
  geom_path()

I appreciate any suggestions.

Maria Reyes
  • 369
  • 3
  • 18
  • [**This**](http://rstudio-pubs-static.s3.amazonaws.com/5795_e6e6411731bb4f1b9cc7eb49499c2082.html) may give you some ideas. After some initial attempts ("(3) Create a radial plot using ggplot2") with a problem similar to yours ("The main problems [...] Straight lines linking the plotted points are required,"), they end up with "(5) Create a radial plot using the function CreateRadialPlot". – Henrik Jul 05 '14 at 20:37
  • [**This Q&A**](http://stackoverflow.com/questions/9614433/creating-radar-chart-a-k-a-star-plot-spider-plot-using-ggplot2-in-r) shows the same problem with curved lines. `radarchart` in `fmsb` package is mentioned. – Henrik Jul 05 '14 at 21:03
  • Thanks for the tip Henrik. I'll check out the pages that you recommended. – Maria Reyes Jul 05 '14 at 23:44
  • Does this answer your question? [ggplot2: connecting points in polar coordinates with a straight line 2](https://stackoverflow.com/questions/42562128/ggplot2-connecting-points-in-polar-coordinates-with-a-straight-line-2) – psychOle May 26 '20 at 11:08

1 Answers1

6

Try this, but note that this is just an ad-hoc workaround and may not work in future.

example <- data.frame(c(5,4,3),c(0.9,1.1,0.6))

colnames(example) <- c("r", "theta")
is.linear.polar2 <- function(x) TRUE
coord_polar2 <-   coord_polar(theta="y", start = 3/2*pi, direction=-1) 
class(coord_polar2) <- c("polar2", class(coord_polar2))

myplot <- ggplot(example, aes(r, theta)) + geom_point(size=3.5) +
  coord_polar2+
  scale_x_continuous(breaks=seq(0,max(example$r)), lim=c(0, max(example$r))) + 
  scale_y_continuous(breaks=round(seq(0, 2*pi, by=pi/4),2), expand=c(0,0), lim=c(0,2*pi)) +
  geom_text(aes(label=rownames(example)), size=4.4, hjust=0.5, vjust=-1) + 
  geom_path()

enter image description here

kohske
  • 65,572
  • 8
  • 165
  • 155
  • Thanks kohske! I'm fairly new to using ggplot2 so I was hoping you could explain what `is.linear.polar2 <- function(x) TRUE` and `class(coord_polar2) <- c("polar2", class(coord_polar2))` each do to achieve the straight lines – Maria Reyes Jul 07 '14 at 22:38
  • @Maria They are the deep internals of ggplot2, so it's difficult to explain here. – kohske Jul 15 '14 at 04:24