我有一個5列的文本表。我想在同一個圖上繪製4列作爲單個密度圖。我可以如下做到這一點: 對於上述情節ggplot2傳說着色
代碼:
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)
我現在想對情節側的一個傳奇。從我的理解,我需要移動colour
的aes
我做這個括號內如下:
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)
此輸出:
每幅圖的顏色是現在錯誤(與第一個圖相比)以及圖例中的標籤。
如何我:
- 正確着色
- 刪除每個密度圖
- 糾正傳說
另請參閱:http://stackoverflow.com/q/10349206/892313 –