【问题标题】:Packed Bubble Pie Charts in R?R中的填充气泡饼图?
【发布时间】:2016-12-21 21:29:42
【问题描述】:

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

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

【问题讨论】:

    标签: r bubble-chart


    【解决方案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")
    

    随后在评论中提到的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 更好的方法

    【讨论】:

    • 谢谢。这当然是一个很好的步骤,但我正在寻找一种解决方案,它可以自动计算气泡的位置并以它们相互接触的方式定位它们(​​如上示例链接中的包装布局) 包装气泡图的想法是它们只代表一个单一的度量变量。就像在 Tableau 中一样:interworks.com/blog/ccapitula/2015/01/06/… 我想通过让填充气泡也代表某个维度或分类变量(比如 R 中的因子变量)来超越这一点。
    • 感谢@Cyrus,该解决方案适用于布局,但我仍然不确定如何继续使用 geom_segment 使气泡看起来像你建议的馅饼。
    猜你喜欢
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    相关资源
    最近更新 更多