2016-08-01 172 views
0

数据框有许多连续的数字列(例如gr)和样本标识符wellseq。每个wellseq有许多行数据。在数据框架中 - 10227行有94个不同级别的wellseq。从数据帧报头上行是:R ggplot中堆积的直方图100 +定性颜色

gr wellseq 
1 27.7049  1 
2 31.1149  1 
3 34.5249  1 
4 39.7249  1 
5 44.9249  1 
6 50.1299  1 

gr柱的概要是如下:

summary(GR) 
     gr    
Min. :-6.94  
1st Qu.:10.71 
Median :13.76 
Mean :18.99 
3rd Qu.:20.70 
Max. :98.42 
NA's :55 

基本为gr整个数据的适当创建直方图。为了进一步分析,需要确定每个wellseq在直方图中的贡献。所使用的ggplot()脚本是:

p2 <- ggplot() + theme_bw() + 
     geom_histogram(data=GR, na.rm= TRUE, mapping = aes(x=gr, fill=factor(GR$wellseq)), 
      bins = 10) + scale_color_brewer(palette = "Dark2") + 
     scale_x_continuous(limits = c(-10, 100)) + 
     labs(title=paste("Gamma Ray","Histogram", sep=" ")) + 
     theme(legend.position = "none") 

Sequential Color in Histogram 所得输出具有颜色 - 这是“连续”,而不是“质”调色板“深色2”。我尝试使用“如何在R中生成一些最独特的颜色?”中的答案。 @ stackoverflow.com并创建所需的颜色。

Dcolor = grDevices::colors()[grep('gr(a|e)y', grDevices::colors(), invert = T)] 
DcolorR <- sample(Dcolor, 433, replace = F) 

使用scale_colour_manual(values = DcolorR)

给出了同样的直方图。对于y使用..count..,直方图确实显示了不同的wellseq的边界,但未按需填充。

p3 <- ggplot() + theme_bw() + 
    geom_histogram(data=GR, na.rm= TRUE, mapping = aes(x=gr, y= ..count.., col = factor(GR$wellseq), bins = 10)) + 
    scale_colour_manual(values = DcolorR) + 
    scale_x_continuous(limits = c(-10, 100)) + 
    labs(title=paste("Gamma Ray"," Frequency Histogram", sep=" ")) + 
    theme(legend.position = "none") 
fill = 1 # leads to blue colored staked histogram 

..count.. creates discrete boundaries in the bars. Fill is single color 我试图得到一个情节就像连接。请指导。提前致谢。

Desired Output

+3

你需要使用'补=''不山坳='控制填充颜色。然后将'scale_color_manual'改为'scale_fill_manual'.Also,请参阅[如何制作一个很好的R可重现示例](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible -例)。 – C8H10N4O2

回答