创建中心最大气泡的气泡图
Create bubble chart with biggest bubble at the center
我正在尝试使用一组数据创建气泡图,如下所示:
X --> 10
Y --> 20
Z --> 5
Q --> 10
我只需要让最大的气泡(根据其数量)位于中心(给予或接受),其余气泡围绕它而不重叠。
我见过的所有 R 示例都需要一个二维数据集,并且由于我拥有的数据只有一维,所以我想知道是否可以在 R 中创建此类图表。
如果有人能给我一些有用的提示,那就太好了。顺便说一下,对于此任务,我需要使用 SA tools
,因此无法选择 d3js
之类的东西。但是,我愿意使用 R 以外的工具。
我不太确定这个问题是否应该在 On Stack Overflow or Cross Validated 中提出,所以如果版主认为它不属于这里,我会删除它。
应该这样做,主要思想是按半径值排序,所以第一个是最大的,然后移动它周围的值(一侧为奇数,另一侧为偶数),以便价值在双向下降。
代码中的进一步解释。
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
}
结果:
我正在尝试使用一组数据创建气泡图,如下所示:
X --> 10
Y --> 20
Z --> 5
Q --> 10
我只需要让最大的气泡(根据其数量)位于中心(给予或接受),其余气泡围绕它而不重叠。
我见过的所有 R 示例都需要一个二维数据集,并且由于我拥有的数据只有一维,所以我想知道是否可以在 R 中创建此类图表。
如果有人能给我一些有用的提示,那就太好了。顺便说一下,对于此任务,我需要使用 SA tools
,因此无法选择 d3js
之类的东西。但是,我愿意使用 R 以外的工具。
我不太确定这个问题是否应该在 On Stack Overflow or Cross Validated 中提出,所以如果版主认为它不属于这里,我会删除它。
应该这样做,主要思想是按半径值排序,所以第一个是最大的,然后移动它周围的值(一侧为奇数,另一侧为偶数),以便价值在双向下降。
代码中的进一步解释。
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
}
结果: