我想使用笛卡尔坐标系绘制图,然后将其覆盖在极坐标图背景上,就像coord_polar
. Panel.background
fromtheme.R
只有element_rect
; 理想情况下,我可以使用类似element_polar
.
有什么办法可以做到这一点?
简单地使用coord_polar
是行不通的,因为我还在绘制各种其他几何图形,这些几何图形以特殊方式映射到coord_polar
(例如,geom_ellispis
从ggforce
包中)。
可重现的示例代码:
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))
# 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()
# 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()
我想要第三个情节的极地背景,以及第一个情节的未扭曲椭圆。有没有办法做到这一点?