2012-09-28 54 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