【发布时间】:2015-05-19 17:24:58
【问题描述】:
我正在寻找一种方法来控制条形图中各部分的颜色,以便在我绘制数据子集时它们保持稳定。 我已经看到了这个解决方案: How to assign colors to categorical variables in ggplot2 that have stable mapping?
这看起来很有希望,但我似乎无法将其应用于我的数据。我怀疑这与我使用的图层有关。
我生成情节的代码是:
plot = ggplot(subdata,mapping = aes(x = as.factor(group))) +
layer(geom = "bar", mapping = aes(fill = as.factor(NUM_MOTIFS)))
我可以获得完整数据集的级别,但是当我尝试将其添加到绘图时,我一直收到此错误:
Error: Aesthetics must either be length one, or the same length as the dataProblems:as.factor(NUM_MOTIFS)
不管我把它放在哪里... 有什么想法吗?
编辑: 示例数据:
fulldata = data.frame(group = rep("A",10), NUM_MOTIFS = c(0,0,1,1,1,2,2,2,4,5))
subdata = data.frame(group = rep("B",8), NUM_MOTIFS = c(0,0,1,1,2,2,2,2))
非常感谢!
【问题讨论】:
-
最好提供一些示例数据供其他人测试您的代码
-
谢谢,阿克伦。请看我的编辑