泡泡图与ggplot2 [英] Bubble Chart with ggplot2

查看:433
本文介绍了泡泡图与ggplot2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我认为这是最好的由一个小例子演示。下面的代码应该强调这个问题:

 #示例
require(ggplot2)
zz< - textConnection(Row PowerSource产品细分价格模型制造地点数量
1高段A低ModA位置A 5000
2低段B低ModB位置B 25000
3高段C低ModC位置C 15000
4低段D高ModD LocationD 30000
5高段E高ModE位置A 2500
6低段A低ModF位置B 110000
7高段B低ModG位置C 20000
8低段C低ModH位置D 3500
9高段D低ModI位置A 65500
10低段E低ModJ位置B 145000
11高段A低段K位置C 15000
12低段B低段ModL位置D 5000
13高段C低ModM LocationA 26000
14低段D低ModN位置B 14000
15高段E Mid ModO位置C 75000
16低段A高ModP位置D 33000
17高段B低ModQ位置A 14000
18低段C中间ModR位置B 33000
19高段D高ModS位置C 95000
20低段E低ModT位置D 4000

df2 < - read.table(zz,header = TRUE)
close(zz)
df2


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

如何分散气泡以显示每个类别中的不同产品和他们的数量?



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



解决方案

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

$ $ p $ $ $ $ $ $ ggplot(df2,aes(x = ManufacturingLocation,y = PowerSource,label = Model))+
geom_point(aes(size = Quantity,color = 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,color = Price,alpha = .02))+
geom_text(hjust = 1,size = 2)+
scale_size range = c(1,15))+
theme_bw()

产生这个结果:





编辑:为了避免th在传说中,人工制品中的alpha应放置在aes之外:

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





和:

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

导致:





编辑2:所以,这花了一段时间的数字出。



我在我的评论中关注了我链接到的示例。我调整了代码以满足您的需求。首先,我在图的外部创建了抖动值:

  df2 $ JitCoOr < - 抖动(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,color = 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,) ,限制= c(1,2))+
scale_x_discrete(休息= 1:4,标签= c(位置A,位置B,位置C,位置D),限制= c(1,2,3,4))+
theme_bw()

这个输出:





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



关于您添加边框的请求,我认为这没有必要。在这个情节中,气泡属于&我认为边界会使它看起来有点难看。但是,如果你仍然想要边框,我会看看,看看我能做些什么。


I want to print a bubble chart in R. The problem I run into is that both, my x and my y axis are discrete. In theory this means a lot of data points (bubbles) end up on the same coordinate. I would rather have them scattered around the data point, but still within a quadrant that makes clear the bubble belongs to the respective x/y coordinate.

I think it is best demonstrated by a little example. The following code should highlight the problem:

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

How can I scatter the bubbles a little bit to show the different products in each categories and their quantity?

(Apologies, I can't add an image at the moment because of too few reputations)

解决方案

As Tom Martens pointed out adjusting alpha can show any overlapping. The following alpha level:

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

results in:

Using geom_jitter instead of point, combined with 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()

produces this:

EDIT: In order to avoid the artefact in the legend the alpha should be placed outside the 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()

resulting in:

and:

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

resulting in:

EDIT 2: So, this took a while to figure out.

I followed the example I linked to in my comment. I adjusted the code to suit your needs. First of all I created the jitter values outside of the plot:

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

I then called those values into the geom_point and geom_text x and y coordinates inside aes. This worked by jittering the bubbles and matching labels to them. However it messed up the x and y axis labels so I relabled them as can be seen in scale_x_discrete and scale_y_discrete. Here is the plot code:

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

Which gives this output:

You can adjust the size of the bubbles via scale_size above. I exported this image with dimensions of 1000*800.

Regarding your request to add borders I think it is unnecessary. It is very clear in this plot where the bubbles belong & I think borders would make it look a bit ugly. However, if you still want borders I'll have a look and see what I can do.

这篇关于泡泡图与ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆