4

I'd like make a plot using cartesian coordinate system, but then have it overlaid on a polar plot background, like that produced by coord_polar. Panel.background from theme.R only has element_rect; Ideally I could use something like element_polar.

Any way to do this?

Simply using coord_polar doesn't work because I'm also plotting various other geoms that map idiosyncratically onto coord_polar (geom_ellispis from the ggforce package, for example).

Reproducible example code:

library(ggplot2)
library(ggforce) # NB this is the github version #install_github("thomasp85/ggforce"). Includes 'geom_ellipsis'


#### Make example data
r<-runif(50,-100,100)    # radial coordinates
theta<-runif(50,0,2)     # theta

a<-runif(50,1,20)
b<-runif(50,1,20)

# Convert r and theta to cartesian:
x<-r*cos(theta*pi)    # x-coordinate of ellipse foci
y<-r*sin(theta*pi)    # y-coordinate of ellipse foci

angle.random<-runif(50,min=0,max=2) # random angle for ellipsis rotation

df<-as.data.frame(cbind(r,theta,x,y,a,b,angle.random))

# Make plots

# Plot should look like this:
ggplot(df,aes(x,y))+
  geom_point(aes(x,y))+
  geom_ellipsis(data=df,aes(x0=x,y0=y,a=a,b=b,angle=angle.random,fill=T))

geom_ellipsis plot on cartesian coordinates

# But I want the panel background in polar coordinates (and auto-adjusing to scale), like this:
ggplot(df,aes(x,y))+
  geom_point(aes(x,y))+
  coord_polar()

Polar coordinates

# However, using geom_ellipsis (among other functions) has idiosyncratic effects in non-cartesian coordinate systems:
ggplot(df,aes(r,theta))+
  geom_point(aes(x,y))+
  geom_ellipsis(data=df,aes(x0=x,y0=y,a=a,b=b,angle=angle.random,fill=T))+
  coord_polar()

geom_ellipsis on coord_polar is distorted

I would like the polar background from the 3rd plot, with the un-distorted ellipses from the first plot. Is there any way to do this?

divibisan
  • 11,659
  • 11
  • 40
  • 58
Allen
  • 41
  • 2
  • 1
    Maybe you are looking for a smilier thing as here? (I had suggested a workaround in this question, simply using a plot combining package for overlay of the plots) https://stackoverflow.com/questions/51368699/annotating-a-polar-ggplot-with-segments-straight-lines-and-unintended-coordina/51376386#51376386 – tjebo Aug 05 '18 at 16:40
  • Sadly, using package cowplot in that way doesn't work - the scale of a coord_polar plot seems to be unity, even when a dummy variable is used to make it larger. When mapped onto a cartesian plot then it doesn't fit properly. I ended up simply using 'geom_circle' and 'geom_link' from the ggtools package to draw a windrose on manually. Not a perfect solution by far but works for the time being! – Allen Aug 09 '18 at 23:05
  • 1
    Actually, polar plots are not exactly 'idiosyncratic', maybe it's the use of them which is eccentric (pun moderately intended). Maybe it would help to explain what you would like to achieve. Because from what I understand, you would like to show your ellipses as they would be on a cartesian coordinate system, on a polar plot, but this would represent entirely different data (?). – tjebo Aug 12 '18 at 11:58

0 Answers0