如何在ggplot中制作融化的数据集灰度

时间:2012-06-18 02:21:16

标签: r ggplot2

这应该是一个非常直截了当的问题。我有以下代码,它形成了我想要的情节:

library(reshape)
library(ggplot2)
require(ggplot2)
split1_data<-structure(list(Loci = structure(1:8, .Label = c("Baez", "Blue", 
"C147", "C204", "C21", "C278_PT", "C294", "C316"), class = "factor"), 
    All = c(0.3357, 0.4166, 0.0242, 0.9708, 0.4518, 0.0666, 0, 
    0.5925), X1_only = c(0.4758, 0.3188, 0.1465, 0.3209, 1, 0.0278, 
    0.2065, 0.6187), X78_only = c(0.3379, 0.4102, 0.2134, 0.6807, 
    0.8242, 1, 0.0046, 0.279), X8_removed = c(0.0967, 0.5831, 
    0.058, 0.9268, 0.3518, 0.0629, 0, 0.6229), X8_only = c(0.1169, 
    0.8327, 0.2169, 0.0907, 1, 1, 0.07, 0.486), X7_removed = c(0.2989, 
    0.7268, 0.0087, 0.8874, 0.5853, 0.0568, 0, 0.7622), X7_only = c(1, 
    0.5714, 0.2825, 0.8673, 0.5557, 0.6861, 0.0044, 0.1146), 
    X5_removed = c(1, 0.1453, 0.0176, 0.8428, 0.2277, 0.2563, 
    0, 0.5326), X5_only = c(0.0642, 0.631, 0.5193, 0.979, 0.5348, 
    0.1304, 0.02, 0.0217), X4_removed = c(0.4492, 0.3821, 0.0121, 
    0.9957, 0.5158, 0.0498, 0, 0.718), X4_only = c(0.6485, 0.0709, 
    0.1639, 0.6908, 1, 1, 0.4469, 0.639), X3_removed = c(0.3009, 
    0.3414, 0.02, 0.9935, 0.4216, 0.1273, 0, 0.6406), X3_only = c(1, 
    0.9325, 0.772, 0.5505, 1, 0.2068, 0.0829, 0.17), X2_removed = c(0.6335, 
    0.349, 0.2095, 0.9777, 0.8928, 0.0571, 0, 0.4285), X2_only = c(0.191, 
    0.4397, 0.0403, 0.3606, 0.0089, 1, 0.0033, 0.659), X1_removed = c(0.1653, 
    0.7658, 0.0718, 0.7705, 0.4193, 0.1894, 0, 0.5167)), .Names = c("Loci", 
"All", "X1_only", "X78_only", "X8_removed", "X8_only", "X7_removed", 
"X7_only", "X5_removed", "X5_only", "X4_removed", "X4_only", 
"X3_removed", "X3_only", "X2_removed", "X2_only", "X1_removed"
), row.names = c(NA, 8L), class = "data.frame")

split1_datam<-melt(split1_data,id="Loci")

p1<- ggplot(split1_datam, aes(x =Loci, y = value, color = variable, width=.15)) +  
         scale_fill_grey() + 
         geom_bar(position="dodge")+ 
         geom_hline(yintercept=0.05)+ 
         opts(axis.text.x  = theme_text(angle=90, size=8)) +
         scale_y_discrete(breaks=seq(0,1)) + 
         ylab(NULL)
p1

然而,我希望将剧情设为灰度,但似乎无法弄清楚如何实现这一点(注意:上面描述的scale_fit_grey()对我不起作用)。有什么建议?非常感谢!

1 个答案:

答案 0 :(得分:6)

ggplot2 通常会让人们在一开始就惹人注意的是colorfill之间的区别。对于条形,矩形,基本上任何填充区域等2D对象,color会影响边框颜色,fill会影响内部颜色。

在你的情节中,你映射color = variable,但fill中没有aes的映射。我想知道你在fill = variable内是否有aes()然后使用scale_fill_grey

否则,您将使用color = variablescale_color_grey,但这只会“显示”条形边框,而不是填充区域。

例如,fill = variablescale_fill_grey()我得到的结论是:

enter image description here