I am looking for a way to control the colors of the sections in a bar plot so that they are stable if I then plot a subset of the data. I have seen this solution: How to assign colors to categorical variables in ggplot2 that have stable mapping?
Which looks very promising but I cannot seem to apply it to my data. I suspect this has to do with the layer I am using.
My code for generating the plot is:
plot = ggplot(subdata,mapping = aes(x = as.factor(group))) +
layer(geom = "bar", mapping = aes(fill = as.factor(NUM_MOTIFS)))
I can get the levels for the full dataset but when I tried adding it to the plot I kept getting this error:
Error: Aesthetics must either be length one, or the same length as the dataProblems:as.factor(NUM_MOTIFS)
No matter where I put it... Any ideas?
EDIT: Example data:
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))
Many thanks!
