Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 209
K. Acharya Avatar asked Oct 02 '17 00:10

K. Acharya


People also ask

How do I assign a color to a variable in R?

In R, colors can be specified either by name (e.g col = “red”) or as a hexadecimal RGB triplet (such as col = “#FFCC00”). You can also use other color systems such as ones taken from the RColorBrewer package.

How do you add color to a geom point?

To color the points in a scatterplot using ggplot2, we can use colour argument inside geom_point with aes. The color can be passed in multiple ways, one such way is to name the particular color and the other way is to giving a range or using a variable.

What colors does Ggplot recognize?

By default, ggplot graphs use a black color for lines and points and a gray color for shapes like the rectangles in bar graphs.


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

like image 127
eipi10 Avatar answered Nov 15 '22 05:11

eipi10