gpt4 book ai didi

r - 使用 ggplot2 绘制气泡图

转载 作者:行者123 更新时间:2023-12-01 16:30:23 25 4
gpt4 key购买 nike

我想在 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()

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

(抱歉,由于信誉太少,我目前无法添加图片)

enter image description here

最佳答案

正如 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()

结果:

enter image description here

使用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()

产生这个:

enter image description here

编辑:为了避免图例中的伪影,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()

结果:

enter image description here

和:

 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()

结果:

enter image description here

编辑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()

这给出了这个输出:

enter image description here

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

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

关于r - 使用 ggplot2 绘制气泡图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26757026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com