2012-09-28 64 views
0

我有一個5列的文本表。我想在同一個圖上繪製4列作爲單個密度圖。我可以如下做到這一點: 對於上述情節enter image description hereggplot2傳說着色

代碼:

library(ggplot2) 
library(grid) 

dat <- read.table(textConnection(" 
file  low  high  avg    lowest 
102   4218.0  5437.0  4739.0   4723.0 
103   4516.0  5765.0  5061.0   5036.0 
104   4329.0  5554.0  4858.0   4838.0 
107   4094.0  5261.0  4596.0   4578.0 
108   4334.0  5569.0  4865.0   4846.0 
109   4397.0  5596.0  4924.0   4896.0 
110   4046.0  5257.0  4555.0   4547.0 
"), header=TRUE) 

x_low = dat$low 
x_high = dat$high 
x_avg = dat$avg 
x_lowest = dat$lowest 

plotter = ggplot() + geom_density(aes(x=x_low), colour="red", fill="red", alpha = .3, data=data.frame(dat$low)) 
plotter = plotter + geom_density(aes(x=x_high),colour="blue", fill="blue", alpha = .3, data=data.frame(dat$high)) 
plotter = plotter + geom_density(aes(x=x_avg), colour="green", fill="green", alpha = .3, data=data.frame(dat$avg)) 
plotter = plotter + geom_density(aes(x=x_lowest), colour="purple", fill="purple", alpha = .3, data=data.frame(dat$lowest)) 
plotter = plotter + xlim(c(2000,7000)) 
print(plotter) 

我現在想對情節側的一個傳奇。從我的理解,我需要移動colouraes

我做這個括號內如下:

library(ggplot2) 
library(grid) 

dat <- read.table(textConnection(" 
file  low  high  avg    lowest 
102   4218.0  5437.0  4739.0   4723.0 
103   4516.0  5765.0  5061.0   5036.0 
104   4329.0  5554.0  4858.0   4838.0 
107   4094.0  5261.0  4596.0   4578.0 
108   4334.0  5569.0  4865.0   4846.0 
109   4397.0  5596.0  4924.0   4896.0 
110   4046.0  5257.0  4555.0   4547.0 
"), header=TRUE) 

x_low = dat$low 
x_high = dat$high 
x_avg = dat$avg 
x_lowest = dat$lowest 

plotter = ggplot() + geom_density(aes(x=x_low, colour="red", fill="red"), alpha = .3, data=data.frame(dat$low)) 
plotter = plotter + geom_density(aes(x=x_high, colour="blue", fill="blue"), alpha = .3, data=data.frame(dat$high)) 
plotter = plotter + geom_density(aes(x=x_avg, colour="green", fill="green"), alpha = .3, data=data.frame(dat$avg)) 
plotter = plotter + geom_density(aes(x=x_lowest, colour="purple", fill="purple"), alpha = .3, data=data.frame(dat$lowest)) 

plotter = plotter + xlim(c(2000,7000)) 
print(plotter) 

此輸出:

enter image description here

每幅圖的顏色是現在錯誤(與第一個圖相比)以及圖例中的標籤。

如何我:

  1. 正確着色
  2. 刪除每個密度圖
  3. 糾正傳說
+0

另請參閱:http://stackoverflow.com/q/10349206/892313 –

回答

1

可以簡化這個,如果你重新組織的黑色輪廓的數據使用reshape2包中的melt。我想下面的代碼將讓你在正確的傳說,你想填充顏色,並擺脫密度圖的輪廓:

dat.m <- melt(dat, id="file") 
ggplot(dat.m, aes(value, fill=variable)) + geom_density(alpha = .3, color=NA) + scale_fill_manual(values=c("red", "blue", "green", "purple")) 
+0

實際上熔化了什麼? – Harpal

+1

它將您的數據從「寬」格式重塑爲「長」格式。因此,不是將「高」,「低」,「平均」和「最低」作爲單獨的列,而是有一列(默認情況下稱爲「變量」)具有這些標籤和另一列(默認稱爲「值」 )具有相應的值。你可以得到關於reshape2軟件包[here]的更多信息和文檔(http://had.co.nz/reshape/)。 –

1

Dan M.'s answer是地道的,最直接的方法,但我想證明另一種在某些情況下可能有其位置。你有colourfill比例尺,其中包含他們應該使用的顏色;你正在尋找的比例是身份尺度。添加行:

plotter = plotter + scale_fill_identity("", 
    labels=c("red"="low", "blue"="high", "green"="avg", "purple"="lowest"), 
    guide="legend") 
plotter = plotter + scale_colour_identity("", 
    labels=c("red"="low", "blue"="high", "green"="avg", "purple"="lowest"), 
    guide="legend") 

""參數擺脫了傳說稱號(如果你想要的東西有,更換此),labels給每種顏色必須標。需要命名向量來確保顏色和標籤之間的匹配是正確的(否則,標籤必須按字母順序按顏色給出)。 guide=TRUE繪製了一個傳說;默認情況下,身份標尺沒有圖例。 enter image description here