I have a data set called d1 similar to:
location, depth.from, depth.to, val, type
I have a loop that creates a fairly complex plot for each unique location (it's glueing together many things using grid.arrange, which is why I can't use facet_wrap on the location to keep the legend/color consistent of one part of the plot).
Say there are 4 categories for "type", the problem when one location has a different number of "types" than the other, the colors assigned are not consistent between each plot. I can manually force them to be the same, but I am trying to generalize this function. Google has failed me.
For the following block, d1 is a subset of the data based on the location type, e.g.
d1 <- subset(myData, location == location.list[i])
Looking at the plot, which is within the loop:
p1 <- ggplot(data = d1, aes (y=val, x=depth.from))+
layer(geom = "point", size = 2) +
geom_rect(data=d1, aes(xmin=Depth.to, xmax=Depth.from, ymin=0, ymax=100, fill = type), linetype =0, alpha=0.3)+
scale_fill_brewer(palette="Set1")
the geom_rect command is going through the data and based on the depth from and depth to, creating a overlay based on fill type. I can use scale_fill_manual("Lith", c("Val1" = "DodgerBlue4"...) etc to manually set it, but that defeats the purposes. If I have:types like where I want something like:
Bird_one = blue
Bird_two = red
Bird_three = green
I want bird_three to be green, even if bird_two doesn't exist, without having to explicitly set it using scale_fill_manual. Is there a way to set a global list of names for the color palette? Perhaps by providing the array from something like:
myData <- read.csv("mydata.csv"
typeList <- unique(myData$type)


