【问题标题】:Bubble Chart with bubbles aligned along their bottom edges气泡图,气泡沿其底部边缘对齐
【发布时间】:2014-09-04 11:11:25
【问题描述】:

有没有一种简单的方法可以像这样在 R 中制作气泡图:

我已经玩弄了ggplot 的虚假数据,并且已经走到了这一步:

cat<-c("A", "A", "B", "B", "C", "C")
chara<-c("1", "0", "1", "0", "1", "0")
percent<-c(80, 20, 60, 40, 90,10)
xcoord<-c(10,10,11,11,12,12)
ycoord<-c(10,10,10,10,10,10)

DF<-data.frame(cat,chara, percent, xcoord, ycoord)

NewBubbleChart <- ggplot(DF, aes(x = cat, y = "", size = percent, label = NULL, fill = chara), legend = FALSE) +
                    geom_point(color = "grey50", shape = 21, alpha = 0.99) +  
                   #geom_text(size=4) +
                    theme_bw() +
                    scale_size(range = c(5, 20))

NewBubbleChart <- NewBubbleChart +
                    scale_fill_manual(name = "Type",
                                      values = c("darkblue", "lightblue"),
                                      labels = c("0" = "Type 0", "1" = "Type 1"))

我最终没有使用 xcoord 和 ycoord 部分,但我把它留在了里面。我知道条形图也可以,但需要一个气泡图。

【问题讨论】:

    标签: r ggplot2 geometry bubble-chart


    【解决方案1】:

    这似乎非常接近。

    library(ggplot2)
    # function to calculate coords of a circle
    circle <- function(center,radius) {
      th <- seq(0,2*pi,len=200)
      data.frame(x=center[1]+radius*cos(th),y=center[2]+radius*sin(th))
    }
    # example dataset, similar to graphic
    df <- data.frame(bank=paste("Bank",LETTERS[1:5]),start=1000*(5:1),end=500*(5:1))    
    max <- max(df$start)
    n.bubbles <- nrow(df)
    scale <- 0.4/sum(sqrt(df$start))
    # calculate scaled centers and radii of bubbles
    radii <- scale*sqrt(df$start)
    ctr.x <- cumsum(c(radii[1],head(radii,-1)+tail(radii,-1)+.01))
    # starting (larger) bubbles
    gg.1  <- do.call(rbind,lapply(1:n.bubbles,function(i)cbind(group=i,circle(c(ctr.x[i],radii[i]),radii[i]))))
    text.1 <- data.frame(x=ctr.x,y=-0.05,label=paste(df$bank,df$start,sep="\n"))
    # ending (smaller) bubbles
    radii <- scale*sqrt(df$end)
    gg.2  <- do.call(rbind,lapply(1:n.bubbles,function(i)cbind(group=i,circle(c(ctr.x[i],radii[i]),radii[i]))))
    text.2 <- data.frame(x=ctr.x,y=2*radii+0.02,label=df$end)
    # make the plot
    ggplot()+
      geom_polygon(data=gg.1,aes(x,y,group=group),fill="dodgerblue")+
      geom_path(data=gg.1,aes(x,y,group=group),color="grey50")+
      geom_text(data=text.1,aes(x,y,label=label))+
      geom_polygon(data=gg.2,aes(x,y,group=group),fill="green2")+
      geom_path(data=gg.2,aes(x,y,group=group),color="grey50")+
      geom_text(data=text.2,aes(x,y,label=label), color="white")+
      labs(x="",y="")+scale_y_continuous(limits=c(-0.1,2.5*scale*sqrt(max(df$start))))+
      coord_fixed()+
      theme(axis.text=element_blank(),axis.ticks=element_blank(),panel.grid=element_blank())
    

    所以这是一个“气泡中的气泡”图表,它代表了两个事件或时间(在您的图表中,经济崩溃之前和之后)之间指标(您的图表中的银行市值)的变化。为了使它起作用,结束条件必须小于开始条件(否则“内部”气泡大于外部气泡)。

    诀窍在于让圆圈沿着它们的底部边缘对齐。这使用geom_point(...) 真的很难,所以我选择只为气泡绘制圆圈。

    我怀疑您必须在实际案例中手动调整文本的位置。如果您想要多行(如图所示),您可能会考虑 ggplot facets。

    最后,如果您想要对圆圈进行阴影处理(例如,使用颜色渐变),这并不是 ggplot 的真正用途:这是可能的,但 IMO 的工作量远远超过其价值。

    【讨论】:

    • 哇,这看起来很棒!非常感谢您帮助我编写代码——我永远不会想出这个解决方案(否则我会花很长时间)——所以我猜 ggplot 相对灵活。不太担心圈子的阴影 - 再次感谢!
    【解决方案2】:

    我不确定您希望图表的外观如何,但您可以尝试使用 googleVis 包中的 gvisBubbleChart

    library(googleVis)
    ##
    DF <- cbind(
      DF,
      ID=paste0(DF$cat,DF$chara)
    )
    bChart <- gvisBubbleChart(
      data=DF,
      idvar="ID",
      xvar="xcoord",
      yvar="chara",
      colorvar="cat",
      sizevar="percent",
      options=list(vAxis='{minValue:.8, maxValue:3}')
    )
    plot(bChart)
    

    【讨论】:

    • 类似于这种类型的图表,但是 A0 在 A1 的类型上,B0 在 B1 的顶部...看起来在 ggplot 中没有简单的方法来做到这一点
    • 您是否特别希望在ggplot2 中执行此操作,或者没关系?我确信可以通过一些调整来完成,我不介意帮助你。
    • 不一定是ggplot2。任何帮助/建议都会很棒——我对 R 中的绘图还是很陌生。
    猜你喜欢
    • 2015-10-16
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    相关资源
    最近更新 更多