我想知道如何使用 ggplot2 添加更高级的颜色方案,而无需手动设置。
这是我到目前为止的照片:
我想让所有数据都遵循梯度,但是每个放线菌都有不同的颜色阴影,与厚壁菌门相同,依此类推。我可以为我的数据框分配一个变量,该变量代表它所在的分类组,但是我将如何使用此方法或其他方法来更改我正在寻找的颜色?
以下是数据(融化前)的样子:
样本列是无关紧要的。nseqs 也无关紧要。组列用于绘制到正确的网格,在这种情况下基于种族。细菌类型也是列。每行包含一个比例/百分比的细菌类型。因此,每行细菌类型比例的总和为 100。xorder 用于对网格中相应位置的条进行排序,并且是预先确定的。
同样,我希望几何条不仅通过它们的 fill=variable 着色,而且还通过每个变量的类型来着色。所有具有相似阴影的放线菌等。
这是处理绘图的当前代码段:
fdata$nseqs <- factor(fdata$nseqs)
fdata$xorder <- factor(fdata$xorder)
ggfdata <- melt(fdata, id.var=c('group','nseqs','sample', 'xorder'))
p <- ggplot(ggfdata, aes(x=xorder, y=value, fill = variable)) + geom_bar(stat='identity') + facet_grid(~group, scales='free_x', space='free_x') + scale_y_continuous() + labs(title=paste('Taxonomic Distribution - grouped by',colnames(meta.frame)[i])) + ylab('Percentage') + xlab('(sorted within group by increasing number of sequences)') + theme(axis.ticks.x = element_blank(), axis.text.x = element_blank()) + scale_x_discrete() + scale_color_brewer()
print(p)
简化的原始数据集(仅 3 个样本 IE 栏) (excel 表中的数据图像)
这是融化的原始简化数据的 dput() :
samplesstructure(list(group = c("af", "as", "af", "af", "as", "af",
"af", "as", "af", "af", "as", "af", "af", "as", "af", "af", "as",
"af", "af", "as", "af", "af", "as", "af", "af", "as", "af", "af",
"as", "af"), nseqs = structure(c(1L, 1L, 2L, 1L, 1L, 2L, 1L,
1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L,
2L, 1L, 1L, 2L, 1L, 1L, 2L), class = "factor", .Label = c("1",
"2")), sample = c("Abidjan.534R", "Tanger.534R", "Salvador.534R",
"Abidjan.534R", "Tanger.534R", "Salvador.534R", "Abidjan.534R",
"Tanger.534R", "Salvador.534R", "Abidjan.534R", "Tanger.534R",
"Salvador.534R", "Abidjan.534R", "Tanger.534R", "Salvador.534R",
"Abidjan.534R", "Tanger.534R", "Salvador.534R", "Abidjan.534R",
"Tanger.534R", "Salvador.534R", "Abidjan.534R", "Tanger.534R",
"Salvador.534R", "Abidjan.534R", "Tanger.534R", "Salvador.534R",
"Abidjan.534R", "Tanger.534R", "Salvador.534R"), xorder = structure(c(1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), class = "factor", .Label = c("1",
"2", "3")), variable = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L,
3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L,
8L, 9L, 9L, 9L, 10L, 10L, 10L), .Label = c("Actinobacteria (Propionibacterium)",
"Actinobacteria (other)", "Actinobacteria (Corynebacterium)",
"Actinobacteria (Micrococcaceae)", "Firmicutes (Streptococcus)",
"Firmicutes (other)", "Firmicutes (Staphylococcus)", "Firmicutes (Clostridiales_Incertae_Sedis_XI)",
"Proteobacteria", "other"), class = "factor"), value = c(28.4151076597247,
14.3313054752169, 68.823080833757, 25.5794799388163, 19.8663608257704,
6.17691916624301, 28.9210495352394, 51.4411090056847, 6.72343670564311,
0.176491351923756, 0.1196768724444, 4.28317234367056, 1.00011766090128,
0.3889498354443, 2.99949161159126, 1.69431697846806, 2.4434028124065,
4.30859176410778, 2.05906577244382, 1.1169841428144, 0.699034062023386,
9.80115307683257, 7.28034307370101, 0.59735638027453, 1.6825508883398,
2.3237259399621, 3.34265378749364, 0.670667137310272, 0.688142016555301,
2.04626334519573)), .Names = c("group", "nseqs", "sample", "xorder",
"variable", "value"), row.names = c(NA, -30L), class = "data.frame")