【问题标题】:Bubble Chart with ggplot2带有ggplot2的气泡图
【发布时间】:2014-11-05 12:17:28
【问题描述】:

我想在 R 中打印气泡图。我遇到的问题是我的 x 轴和我的 y 轴都是离散的。从理论上讲,这意味着许多数据点(气泡)最终位于同一坐标上。我宁愿让它们散布在数据点周围,但仍然在一个象限内,明确气泡属于各自的 x/y 坐标。

我认为最好通过一个小例子来证明这一点。以下代码应突出显示问题:

# Example
require(ggplot2)
zz <- textConnection("Row PowerSource ProductSegment Price Model ManufacturingLocation Quantity
1 High SegmentA Low ModA LocationA 5000
2 Low SegmentB Low ModB LocationB 25000
3 High SegmentC Low ModC LocationC 15000
4 Low SegmentD High ModD LocationD 30000
5 High SegmentE High ModE LocationA 2500
6 Low SegmentA Low ModF LocationB 110000
7 High SegmentB Low ModG LocationC 20000
8 Low SegmentC Low ModH LocationD 3500
9 High SegmentD Low ModI LocationA 65500
10 Low SegmentE Low ModJ LocationB 145000
11 High SegmentA Low ModK LocationC 15000
12 Low SegmentB Low ModL LocationD 5000
13 High SegmentC Low ModM LocationA 26000
14 Low SegmentD Low ModN LocationB 14000
15 High SegmentE Mid ModO LocationC 75000
16 Low SegmentA High ModP LocationD 33000
17 High SegmentB Low ModQ LocationA 14000
18 Low SegmentC Mid ModR LocationB 33000
19 High SegmentD High ModS LocationC 95000
20 Low SegmentE Low ModT LocationD 4000
 ")
df2 <- read.table(zz, header= TRUE)
close(zz)
df2


ggplot(df2, aes(x = ManufacturingLocation, y = PowerSource, label = Model)) +
    geom_point(aes(size = Quantity, colour = Price)) + 
    geom_text(hjust = 1, size = 2) +
    scale_size(range = c(1,15)) +
    theme_bw()

如何将气泡分散一点以显示每个类别中的不同产品及其数量?

(抱歉,由于声誉太少,我暂时无法添加图片)

【问题讨论】:

  • 是您的最后一个注意事项:在帖子中添加图片链接,有足够代表的人可能会将其替换为实际图片。
  • 也许您可以使用 geom_jitter(...) 代替 geom_point 并且还可以使用 alpha 参数来增加由于重叠导致的透明度。

标签: r ggplot2 bubble-chart


【解决方案1】:

正如 Tom Martens 指出的那样,调整 alpha 可以显示任何重叠。以下 alpha 级别:

ggplot(df2, aes(x = ManufacturingLocation, y = PowerSource, label = Model)) +
    geom_point(aes(size = Quantity, colour = Price, alpha=.02)) + 
    geom_text(hjust = 1, size = 2) +
    scale_size(range = c(1,15)) +
    theme_bw()

结果:

用geom_jitter代替point,结合alpha:

ggplot(df2, aes(x = ManufacturingLocation, y = PowerSource, label = Model)) +
    geom_jitter(aes(size = Quantity, colour = Price, alpha=.02)) + 
    geom_text(hjust = 1, size = 2) +
    scale_size(range = c(1,15)) +
    theme_bw()

产生这个:

编辑:为了避免传说中的伪影,应该将 alpha 放在 aes 之外:

ggplot(df2, aes(x = ManufacturingLocation, y = PowerSource, label = Model)) +
    geom_point(aes(size = Quantity, colour = Price),alpha=.2) +
    geom_text(hjust = 1, size = 2) +
    scale_size(range = c(1,15)) +
    theme_bw()

导致:

和:

 ggplot(df2, aes(x = ManufacturingLocation, y = PowerSource, label = Model)) +
    geom_jitter(aes(size = Quantity, colour = Price),alpha=.2) +
    geom_text(hjust = 1, size = 2) +
    scale_size(range = c(1,15)) +
    theme_bw()

导致:

编辑 2:所以,这需要一段时间才能弄清楚。

我按照我在评论中链接到的示例进行操作。我调整了代码以满足您的需求。首先,我在绘图之外创建了抖动值:

df2$JitCoOr <- jitter(as.numeric(factor(df2$ManufacturingLocation)))
df2$JitCoOrPow <- jitter(as.numeric(factor(df2$PowerSource)))

然后我将这些值称为 aes 内的 geom_point 和 geom_text x 和 y 坐标。这通过抖动气泡和匹配标签来实现。然而,它弄乱了 x 和 y 轴标签,所以我重新定义了它们,如 scale_x_discrete 和 scale_y_discrete 所示。这是情节代码:

ggplot(df2, aes(x = ManufacturingLocation, y = PowerSource)) +
geom_point(data=df2,aes(x=JitCoOr, y=JitCoOrPow,size = Quantity, colour = Price), alpha=.5)+
geom_text(data=df2,aes(x=JitCoOr, y=JitCoOrPow,label=Model)) + 
scale_size(range = c(1,50)) +
scale_y_discrete(breaks =1:3 , labels=c("Low","High"," "), limits = c(1, 2))+
scale_x_discrete(breaks =1:4 , labels=c("Location A","Location B","Location C","Location D"), limits = c(1,2,3,4))+ 
theme_bw()

给出这个输出:

您可以通过上面的 scale_size 调整气泡的大小。我导出了这个尺寸为 1000*800 的图像。

关于您添加边框的要求,我认为这是不必要的。在这个情节中,气泡所属的位置非常清楚,我认为边界会使它看起来有点难看。但是,如果您仍然想要边框,我会看看,看看我能做什么。

【讨论】:

  • 这几乎是完美的。您还知道如何在相应气泡旁边获取标签吗?此外,使用 geom_jitter 的建议绝对很棒。正是需要的。但是,既然气泡终于做到了它们应该做的事情,我该如何区分不同的象限呢?有没有办法在 LocationA/High、LocationB/High 等周围绘制一个矩形(左右)? +1 并在标签问题排序后接受!
  • 我有 15 点声望就会给 +1。道歉。
  • 我现在没有时间解决这个问题,但是类似: geom_text(hjust = 1, size = 3,position=position_jitter()) 将停止标签重叠,但我怀疑它们不会匹配气泡。在这里查看有关如何解决该问题的想法:stackoverflow.com/questions/6551147/… 我明天再看看。
  • 很好的答案。演示所有关键问题 - 非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-27
  • 2018-06-13
  • 2023-03-12
  • 2012-01-28
  • 1970-01-01
  • 1970-01-01
  • 2014-04-02
相关资源
最近更新 更多