2014-11-05 2 views
7

R에서 버블 차트를 인쇄하고 싶습니다. 내가 만나는 문제는 내 x 축과 y 축이 이산되어 있다는 것입니다. 이론적으로 이것은 많은 데이터 포인트 (거품)가 같은 좌표에서 끝난다는 것을 의미합니다. 데이터 포인트 주위에 흩어져 있지만, 거품이 각각의 x/y 좌표에 속하는지 확실하게하는 사분면 내에서 분산시킬 수 있습니다.ggplot2를 사용한 버블 차트

작은 예제로 가장 잘 표현 된 것 같습니다.

# 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

+0

마지막주의 : 이미지에 대한 링크를 게시물에 추가하면 담당자가 충분한 사람이 실제 이미지로 대체 할 가능성이 높습니다. – tonytonov

+0

아마도 geom_point 대신 geom_jitter (...)를 사용할 수 있으며 겹침으로 인해 투명도를 추가하는 데 alpha 매개 변수를 사용할 수도 있습니다. –

답변

14

(죄송합니다, 나는 때문에 너무 적은 명성의 순간에 이미지를 추가 할 수 없습니다).

enter image description here

:

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

알파와 조합 대신 포인트 geom_jitter 사용 다음 알파 수준 :에

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

결과 01 23,516,

EDIT :

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

이 결과 : 범례에서 아티팩트를 회피하기 위해 알파는 AES 외부에 배치되어야

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 : 그럼,이 시간이 걸렸습니다.

나는 나의 코멘트에 링크 된 예제를 따른다. 나는 당신의 필요에 맞게 코드를 조정했다. 우선 I 줄거리 이외의 지터 값을 생성 :

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

I는 다음과 geom_point geom_text X에 값들을 호출 Y는 내부 AES 좌표. 이것은 거품을 흔들어 대고 레이블을 매칭하여 작동했습니다. 그러나 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의 크기로이 이미지를 내보냈습니다.

테두리 추가 요청에 대해서는 불필요하다고 생각합니다. 거품이 어디에 속해 있는지이 플롯에서 매우 분명합니다 & 국경은보기가 조금 엉망이 될 것이라고 생각합니다. 그러나 당신이 여전히 국경을 원한다면 나는 한 번 봐서 내가 할 수있는 것을 보게 될 것입니다.

+0

거의 완벽합니다. 각 버블 옆에 레이블을 붙이는 법도 알고 있습니까? 또한 geom_jitter를 사용하는 것이 좋습니다. 정확히 무엇이 필요한가. 그러나 거품이 마침내 그들이하는 일을 마쳤으므로 어떻게 다른 사분면을 구별 할 수 있습니까? LocationA/High, LocationB/High 등을 중심으로 직사각형을 그리는 방법이 있습니까? 라벨이 발행 된 경우 +1하고 받아들입니다! – Roman

+0

내가 명성을 얻 자마자 +1 할 것입니다. 사과. – Roman

+0

geom_text (hjust = 1, size = 3, position = position_jitter())는 lables가 겹치지 않게 할 것이지만 거품과 일치하지 않을 것으로 생각합니다. 그 문제를 해결하는 방법에 대한 아이디어를 보려면 여기를 확인하십시오 : http://stackoverflow.com/questions/6551147/adding-text-to-ggplot-geom-jitter-points-that-match-a-condition 내일 다시 봐. – Docconcoct