2013-05-16 49 views
4

一個複雜的氣泡圖我有一個數據集是這樣的(簡化的用於說明目的):如何創建中的R

zz <- textConnection("Company Market.Cap Institutions.Own Price.Earnings Industry 
ExxonMobil 405.69 50% 9.3 Energy 
Citigroup 156.23 67% 18.45 Banking 
Pfizer 212.51 73% 20.91 Pharma 
JPMorgan 193.1 75% 9.12 Banking 
") 
Companies <- read.table(zz, header= TRUE) 
close(zz) 

我想創建氣泡圖(當然,像氣泡圖)具有以下屬性:

  • 每個氣泡是一個公司,隨着氣泡綁市場帽的尺寸,
  • 氣泡綁行業的顏色,
  • 與x軸有兩個類別,Industries.Own和Price.Earnings,
  • ,y軸是1-10的比例,每個公司的值被歸一化爲該比例。 (我當然可以做外R上的正常化,但我相信R採用的是可能的。)

需要明確的是,每家公司都會出現在結果中的每一列,例如埃克森美孚將接近兩者的底部Institutions.Own專欄和Price.Earnings專欄;理想情況下,公司的名稱會出現在其兩個氣泡中或旁邊。

回答

6

我認爲這涉及到你的所有觀點。請注意 - 您的Institutions.Own是因爲%而被作爲因素讀入的......我只是簡單地將其刪除以便於時間...您需要在某處解決該問題。一旦完成,我會使用ggplot並相應地映射您的不同美學。如果需要,你可以擺弄軸標題等。

#Requisite packages 
library(ggplot2) 
library(reshape2) 
#Define function, adjust this as necessary 
rescaler <- function(x) 10 * (x-min(x))/(max(x)-min(x)) 
#Rescale your two variables 
Companies$Inst.Scales <- with(Companies, rescaler(Institutions.Own)) 
Companies$Price.Scales <- with(Companies, rescaler(Price.Earnings)) 
#Melt into long format 
Companies.m <- melt(Companies, measure.vars = c("Inst.Scales", "Price.Scales")) 
#Plotting code 
ggplot(Companies.m, aes(x = variable, y = value, label = Company)) + 
    geom_point(aes(size = Market.Cap, colour = Industry)) + 
    geom_text(hjust = 1, size = 3) + 
    scale_size(range = c(4,8)) + 
    theme_bw() 

結果:

enter image description here