How to assign same color to factors across plots in a nested loop for ggplot?

Viewed 2359

I am trying to use scale_fill_manual to assign corresponding colors to factors across many plots in a nested for loop. However, the resulting plots end up all being black.

My overall loop is as follows:

for(i in seq(from=0, to=100, by=10)){
   for{j in seq(from=0, to=100, by=10)){
       print(ggplot(aes(x , y), data = df)+
        geom_point(inherit.aes = FALSE,data = subset(df,factor_x==i&factor_y==j), aes(x, y, size=point,color=Group))+
        theme_bw()}}

I am trying to assign each factor in "Group" its own color that plots consistently. I've tried using:

col<-colorRampPalette(brewer.pal(9,"Set1"))(16)

Then I assigned each color to a specific factor in "Group."

However, when using scale manual in the nested loop there is no color at all for the factors.

for(i in seq(from=0, to=100, by=10)){
   for{j in seq(from=0, to=100, by=10)){
       print(ggplot(aes(x , y), data = df)+
        geom_point(inherit.aes = FALSE,data = subset(df,factor_x==i&factor_y==j), aes(x, y, size=point))+
        theme_bw()+scale_fill_manual(values=col)}}

How can I integrate a color scheme for the categorical values in "Group" across the many plots generated in the loop?

1 Answers

The idea is to create a named vector of colors that assigns desired colors to each potential level of the factor variable you're using for the color (or fill) aesthetic in your plot. Then use that color vector in scale_color_manual (or scale_fill_manual) to set the plot colors. This will assign the desired color to the desired factor level, regardless of whether a given factor level is present in the particular data frame used for a given plot.

Here's a simple example:

library(ggplot2)

# Plotting function
pfunc = function(data, x, y, col_var, color_vec, drop=TRUE) {
  ggplot(data, aes_string(x, y, colour=col_var)) +
    geom_point(size=3) +
    scale_colour_manual(values=color_vec, drop=drop)
}

Now run the function with the built-in iris data frame using the whole data frame and a subset of the data.

# Named vector giving desired color for each Species
col = setNames(c("green","red","blue"), levels(iris$Species))

pfunc(iris, "Petal.Width", "Petal.Length", "Species", col)
pfunc(subset(iris, Species=="versicolor"), 
      "Petal.Width", "Petal.Length", "Species", col)
pfunc(subset(iris, Species=="versicolor"), 
      "Petal.Width", "Petal.Length", "Species", col, drop=FALSE)

enter image description here

Or with the diamonds data frame:

n = length(levels(diamonds$color))
col = setNames(hcl(seq(15,375,length=n+1)[1:n], 100, 65), levels(diamonds$color))

set.seed(2)
dat = diamonds[sample(1:nrow(diamonds), 200), ]

pfunc(dat, "carat", "price", "color", col)
pfunc(subset(dat, color %in% c("D","G")), "carat", "price", "color", col)
pfunc(subset(dat, color %in% c("G","I")), "carat", "price", "color", col)

enter image description here

Related