我想在一个点图中包含一系列同心圆作为网格.目标是让观察者知道图中哪些点具有大致相同的幅度.我创建了一个hack来做到这一点:
add_circle_grid <- function(g,ncirc = 10){
gb <- ggplot_build(g)
xl <- gb$panel$ranges[[1]]$x.range
yl <- gb$panel$ranges[[1]]$y.range
rmax = sqrt(max(xl)^2+max(yl)^2)
theta=seq(from=0,by=.01,to=2*pi)
for(n in 1:ncirc){
r <- n*rmax/ncirc
circle <- data.frame(x=r*sin(theta),y=r*cos(theta))
g<- g+geom_path(data=circle,aes(x=x,y=y),alpha=.2)
}
return(g+xlim(xl)+ylim(yl))
}
xy<-data.frame(x=rnorm(100),y=rnorm(100))
ggplot(xy,aes(x,y))+geom_point()
ggg<-add_circle_grid(ggplot(xy,aes(x,y))+geom_point())
print(ggg)
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有更多的ggplot方法来做到这一点.我也考虑使用极坐标,但它不允许我以相同的方式设置x和y限制.最后,我不介意指示每个圆的半径的小文本标签.
编辑 也许这是要求太多,但我还想要另外两件事.