像网格一样的同心圆,以原点为中心 [英] Concentric Circles like a grid, centered at origin
问题描述
我想在点图中包含一系列同心圆作为网格。目标是让观众了解剧情中的哪些点具有大致相同的幅度。
我创建了一个hack来做到这一点:$ b
$ b
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(θ))
g < - g + geom_path(data = circle,aes(x = x,y = y),alpha = 0.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)
但我想知道是否有更多ggplot的方式来做到这一点。我也考虑过使用极坐标,但它不允许我以相同的方式设置x和y极限。
最后,我不介意指示每个圆的半径的小文本标签。
编辑
也许这个问的太多了,但还有其他两件事是我想要的。
- 轴限制应该保持不变(可以通过ggplot_build)
- 这可以使用facets吗?据我所知,如果我想动态地添加圆圈,需要以某种方式找出方面。
< pre $ set.seed(1)
pre>
xy < - data.frame(x = rnorm(100),y = rnorm(100))
rmax = sqrt (max(xy $ x)^ 2 + max(xy $ y)^ 2)
theta = seq(from = 0,by = .01,to = 2 * pi)
ncirc = 10
dat.circ = do.call(rbind,
lapply(seq_len(ncirc),function(n){
r < - n * rmax / ncirc
data .frame(x = r * sin(theta),y = r * cos(theta),r = round(r,2))
}))
rr< - unique (dat.circ $ r)
dat.text = data.frame(x = rr * cos(30),y = rr * sin(30),label = rr)
library(ggplot2)
ggplot(xy,aes(x,y))+
geom_point()+
geom_path(data = dat.circ,alpha =。 2,aes(group = factor(r)))+
geom_text(data = dat.text,aes(label = rr),vjust = -1)
I would like to include a sequence of concentric circles as a grid in a plot of points. The goal is to give the viewer an idea of which points in the plot have approximately the same magnitude. I created a hack to do this:
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)
But I was wondering if there is a more ggplot way to do this. I also considered using polar coordinates but it does not allow me to set x- and y-limits in the same way. Finally, I wouldn't mind little text labels indicating the radius of each circle.
EDIT Perhaps this is asking too much but there are two other things that I would like.
- The axis limits should stay the same (which can be done via ggplot_build)
- Can this work with facets? As far as I can tell you would need to somehow figure out the facets if I want to add the circles dynamically.
set.seed(1)
xy <- data.frame(x=rnorm(100),y=rnorm(100))
rmax = sqrt(max(xy$x)^2+max(xy$y)^2)
theta=seq(from=0,by=.01,to=2*pi)
ncirc=10
dat.circ = do.call(rbind,
lapply(seq_len(ncirc),function(n){
r <- n*rmax/ncirc
data.frame(x=r*sin(theta),y=r*cos(theta),r=round(r,2))
}))
rr <- unique(dat.circ$r)
dat.text=data.frame(x=rr*cos(30),y=rr*sin(30),label=rr)
library(ggplot2)
ggplot(xy,aes(x,y))+
geom_point() +
geom_path(data=dat.circ,alpha=.2,aes(group=factor(r))) +
geom_text(data=dat.text,aes(label=rr),vjust=-1)
这篇关于像网格一样的同心圆,以原点为中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!