R中的打包气泡饼图?

问题描述 投票:1回答:1

R中是否有任何软件包可以帮助我创建打包的气泡图,其中各个气泡充当饼图?

这里提到了在D3中实现的这种可视化示例:http://bl.ocks.org/jsl6906/4a1b818b64847fb05d56

r bubble-chart
1个回答
1
投票

您可以编写自己的函数:

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")

enter image description here

“ 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)

enter image description here

您必须添加geom_segment才能使气泡看起来像馅饼,尽管我敢肯定有比使用ggplot2更好的方法>

© www.soinside.com 2019 - 2024. All rights reserved.