R 中的打包气泡饼图?
Packed Bubble Pie Charts in R?
R 中是否有任何包可以帮助我创建包装气泡图,其中单个气泡充当饼图?
这里提到了在 D3 中实现的这种可视化的示例:
http://bl.ocks.org/jsl6906/4a1b818b64847fb05d56
您可以编写自己的函数:
pie_bubbles<-function(xpos,ypos,radii,sectors,
sector_col=NULL,main="",xlab="",ylab="") {
xlim<-c(min(xpos-radii),max(xpos+radii))
ylim<-c(min(ypos-radii),max(ypos+radii))
nbubbles<-length(xpos)
if(is.null(sector_col)) {
sector_col<-list()
for(scol in 1:nbubbles)
sector_col[[scol]]<-rainbow(length(sectors[[scol]]))
}
plot(0,xlim=xlim,ylim=ylim,type="n",
main=main,xlab=xlab,ylab=ylab)
for(bubble in 1:nbubbles)
floating.pie(xpos=xpos[bubble],ypos=ypos[bubble],
x=sectors[[bubble]],radius=radii[bubble],
col=sector_col[[bubble]])
}
# set the x positions
xpos<-c(2,4,6,8,10)
# and the y positions
ypos<-c(4,8,6,10,2)
# the radii are the "bubble" radii
radii<-c(1,0.5,1.2,0.7,1.3)
# these are the sector extents of the pies
sectors<-list(1:4,c(5,3,8,6,2),c(3,2,1),c(3,7,5,8),c(2.5,3.7))
# get the plotrix package
library(plotrix)
pie_bubbles(xpos,ypos,radii,sectors,main="Pie bubbles")
"Touching bubbles" 正如 OP 随后在评论中提到的那样:
ncircles <- 200
limits <- c(-50, 50)
inset <- diff(limits) / 3
rmax <- 20
xyr <- data.frame(
x = runif(ncircles, min(limits) + inset, max(limits) - inset),
y = runif(ncircles, min(limits) + inset, max(limits) - inset),
r = rbeta(ncircles, 1, 10) * rmax)
library(packcircles)
res <- circleLayout(xyr, limits, limits, maxiter = 1000)
cat(res$niter, "iterations performed")
library(ggplot2)
library(gridExtra)
dat.after <- circlePlotData(res$layout)
doPlot <- function(dat, title)
ggplot(dat) +
geom_polygon(aes(x, y, group=id), colour="brown", fill="burlywood", alpha=0.3) +
coord_equal(xlim=limits, ylim=limits) +
theme_bw() +
theme(axis.text=element_blank(),
axis.ticks=element_blank(),
axis.title=element_blank()) +
labs(title=title)
grid.arrange(
doPlot(dat.before, "before"),
doPlot(dat.after, "after"),
nrow=1)
您必须添加 geom_segment
才能使气泡看起来像馅饼,但我确信有比使用 ggplot2
更好的方法
R 中是否有任何包可以帮助我创建包装气泡图,其中单个气泡充当饼图?
这里提到了在 D3 中实现的这种可视化的示例: http://bl.ocks.org/jsl6906/4a1b818b64847fb05d56
您可以编写自己的函数:
pie_bubbles<-function(xpos,ypos,radii,sectors,
sector_col=NULL,main="",xlab="",ylab="") {
xlim<-c(min(xpos-radii),max(xpos+radii))
ylim<-c(min(ypos-radii),max(ypos+radii))
nbubbles<-length(xpos)
if(is.null(sector_col)) {
sector_col<-list()
for(scol in 1:nbubbles)
sector_col[[scol]]<-rainbow(length(sectors[[scol]]))
}
plot(0,xlim=xlim,ylim=ylim,type="n",
main=main,xlab=xlab,ylab=ylab)
for(bubble in 1:nbubbles)
floating.pie(xpos=xpos[bubble],ypos=ypos[bubble],
x=sectors[[bubble]],radius=radii[bubble],
col=sector_col[[bubble]])
}
# set the x positions
xpos<-c(2,4,6,8,10)
# and the y positions
ypos<-c(4,8,6,10,2)
# the radii are the "bubble" radii
radii<-c(1,0.5,1.2,0.7,1.3)
# these are the sector extents of the pies
sectors<-list(1:4,c(5,3,8,6,2),c(3,2,1),c(3,7,5,8),c(2.5,3.7))
# get the plotrix package
library(plotrix)
pie_bubbles(xpos,ypos,radii,sectors,main="Pie bubbles")
"Touching bubbles" 正如 OP 随后在评论中提到的那样:
ncircles <- 200
limits <- c(-50, 50)
inset <- diff(limits) / 3
rmax <- 20
xyr <- data.frame(
x = runif(ncircles, min(limits) + inset, max(limits) - inset),
y = runif(ncircles, min(limits) + inset, max(limits) - inset),
r = rbeta(ncircles, 1, 10) * rmax)
library(packcircles)
res <- circleLayout(xyr, limits, limits, maxiter = 1000)
cat(res$niter, "iterations performed")
library(ggplot2)
library(gridExtra)
dat.after <- circlePlotData(res$layout)
doPlot <- function(dat, title)
ggplot(dat) +
geom_polygon(aes(x, y, group=id), colour="brown", fill="burlywood", alpha=0.3) +
coord_equal(xlim=limits, ylim=limits) +
theme_bw() +
theme(axis.text=element_blank(),
axis.ticks=element_blank(),
axis.title=element_blank()) +
labs(title=title)
grid.arrange(
doPlot(dat.before, "before"),
doPlot(dat.after, "after"),
nrow=1)
您必须添加 geom_segment
才能使气泡看起来像馅饼,但我确信有比使用 ggplot2