6

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?

3
  • Unfortunately I do not understand well what do you want to do. Can you share a bit of your data? What do you mean with "there is no color at all for the factors"? Commented Oct 2, 2017 at 1:34
  • You are using scale_fill_manual rather than scale_color_manual. Also you didn't assign color = Group in your aesthetic mappings... Commented Oct 2, 2017 at 1:51
  • My apologies for being vague. I will share some sample data with an edit. As I responded to the answer below, I am trying to plot data from 4 data.frames that share the same variable "species".I would like to have a corresponding color to each species across the different plots Commented Oct 4, 2017 at 12:46

1 Answer 1

8

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

Sign up to request clarification or add additional context in comments.

2 Comments

This is very helpful! However, my issue is arising when setting the levels of the specified colors. I am using multiple data.frames that have the same set of species. So I have tried using: allspecies<-unique(c(as.character(df1$species),as.character(df2$species),as.character(df3$species),as.character(df4$species))). This is what my final issue is when trying to plot all 4 of these with corresponding colors to species.
I was able to fix the issue! Thank you for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.