【问题标题】:Create bubble chart with biggest bubble at the center创建以最大气泡为中心的气泡图
【发布时间】:2015-04-29 16:44:34
【问题描述】:

我正在尝试使用一组数据创建气泡图,如下所示:

X --> 10
Y --> 20
Z --> 5
Q --> 10

我只需要让最大的气泡(基于其数量)位于中心(给予或接受),其余的气泡围绕它而不重叠。

我见过的所有 R 示例都需要二维数据集,并且由于我拥有的数据只有一维,我想知道是否可以在 R 中创建这样的图。

如果有人能给我一些有用的提示,那就太好了。顺便说一句,我需要使用SA tools,所以像d3js 这样的东西是没有选择的。但是,我愿意使用 R 以外的工具。

我不太确定这个问题是否应该在 Stack OverflowCross Validated 中提出,所以如果版主认为它不属于这里,我会删除它。

【问题讨论】:

  • 气泡图通常需要 3 维数据:点的 x 和 y 坐标以及气泡的半径。使用一维数据,您将如何定位点或确定气泡的大小?
  • 我非常同意@AlexA。在这种情况下,条形图会更好。
  • @AlexA。我基本上想假设这个数字是圆的半径。因为我希望最大的气泡位于中心,因此其他人只是围绕它说唱。正如我之前所说,我是 R 中的菜鸟,所以我不确定这样的假设是否有效
  • @nafas 气泡图与饼图有同样的问题:它们歪曲数据的差异,因为人类在估计区域差异方面非常糟糕。如果您缩放半径而不是面积,这甚至更糟糕,因为将值加倍意味着使圆大于 4 倍。我看不出条形图是如何不相关的:如果数据与您编写的数据相似,那么条形图是最好的解决方案之一。如果数据不同,请提供适当的数据。

标签: r bubble-chart


【解决方案1】:

应该这样做,主要思想是您按半径的值排序,所以第一个是最大的,然后移动它周围的值(一侧奇数,甚至另一侧),以便这些值是双向递减。

代码中的进一步解释。

library(plotrix)
library(RColorBrewer)

# Set the random seed, to get reproducible results
set.seed(54321)

# Generate some random values for the radius
num.circles <- 11
rd <- runif(num.circles, 1, 20)

df <- data.frame(labels=paste("Lbl", 1:num.circles), radius=rd)

# Sort by descending radius. The biggest circle is always row 1
df <- df[rev(order(df$radius)),]

# Now we want to put the biggest circle in the middle and the others on either side
# To do so we reorder the data frame taking the even values first reversed, then the odd values.
# This ensure the biggest circle is in the middle

df <- df[c(rev(seq(2, num.circles, 2)), seq(1, num.circles, 2)),]

# Space between the circles. 0.2 * average radius seems OK
space.between <- 0.2 * mean(df$radius)

# Creat an empty plot
plot(0, 0, "n", axes=FALSE, bty="n", xlab="", ylab="", 
     xlim=c(0, sum(df$radius)*2+space.between*num.circles),
     ylim=c(0, 2.5 * max(df$radius)))

# Draw the circle at half the height of the biggest circle (plus some padding)
xx <- 0
mid.y <- max(df$radius) * 1.25

# Some nice degrading tones of blue
colors <- colorRampPalette(brewer.pal(8,"Blues"))(num.circles/2)

for (i in 1:nrow(df))
  {
  row <- df[i,]
  x <- xx + row$radius + i*space.between
  y <- mid.y

  # Draw the circle
  draw.circle(x, y, row$radius, 
              col=colors[abs(num.circles/2-i)])
  # Add the label
  text(x, y, row$labels, cex=0.6)

  # Update current x position
  xx <- xx + row$radius * 2
  }

结果:

Live version on RFiddle.

【讨论】:

  • 这是个好伙伴,我想把圆圈按二维顺序排列,例如here.但这似乎更好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-07
  • 2011-12-23
  • 2013-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多