我有一个包含 5 列的文本表。我想在同一个图上绘制 4 列作为单独的密度图。我可以实现如下:
上图的代码:
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)
这输出:
现在每个图的颜色都是错误的(与第一个图相比)以及图例中的标签。
我怎样才能:
- 修正着色
- 删除每个密度图的深色轮廓
- 更正图例