我的数据由一组带半径的圆组成.x,y和半径的比例相同.
x y radius
0.1 0.8 0.1
0.4 0.4 0.2
0.6 0.2 0.9
0.3 0.6 0.5
0.5 0.5 0.2
...
0.9 0.1 0.1
Run Code Online (Sandbox Code Playgroud)
我用的时候:
myplot <- ggplot() + geom_point(data=df, aes(x=x, y=y, size=(2*radius)))
Run Code Online (Sandbox Code Playgroud)
得到的图是一个气泡图,其大小按比例缩放到半径.我想要一个气泡图,其中radius of bubble = radius
(即气泡的半径是原始单位).
我怎样才能实现这个目标(在ggplot2中)?
事实证明,似乎没有一种简单的方法可以做到这一点.
我检查了由baptiste链接的邮件列表条目(用于单个圆圈),并使用for循环扩展,一次绘制一个圆圈.
df = data.frame(x=c(0.1,0.4,0.6, 0.3, 0.5,0.9), y=c(0.8,0.4,0.2,0.6,0.5,0.1), r=c(0.1,0.2,0.2,0.1,0.2,0.1))
angle <- seq(-pi, pi, length = 50)
myplot = ggplot()
for (i in 1:length(df$x)) {
df_temp = data.frame(x = df$x[i] + df$r[i]*sin(angle), y = df$y[i] + df$r[i]*cos(angle))
myplot = myplot + geom_polygon(data=df_temp, aes(x=x, y=y), inherit.aes=F)
}
myplot = myplot + geom_point(data=df, aes(x=x, y=y))
Run Code Online (Sandbox Code Playgroud)
这给出了:
示例数据集略有变化,以使图中的内容更清晰.我还在这里绘制了圆心的坐标.
编辑:建议改进,仅绘制一个多边形图层.
circularise <- function(d, n=360){
angle <- seq(-pi, pi, length = n)
make_circle <- function(x,y,r,id){
data.frame(x=x+r*cos(angle), y=y+r*sin(angle), id)
}
lmat <- mapply(make_circle, id = seq_len(nrow(d)),
x = d[,1], y=d[,2], r=d[,3], SIMPLIFY = FALSE)
do.call(rbind, lmat)
}
circles <- circularise(df)
p = ggplot() +
geom_point(data=df, aes(x=x, y=y))
p + geom_polygon(aes(x,y,group=id, fill=id), data=circles) +
coord_fixed()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1782 次 |
最近记录: |