3

I'm new to R (and stack overflow) and would appreciate any help! I haven't been able to find a solution from other questions. I'm trying to:

  1. Assign 2 different continuous color gradients in the x and y direction based on different variables. (plot1 in code below)
  2. Retain these color assignments for a different plot of different variables of the same data frame. (plot2 in code below)

I have only been able to manage assigning color based on one variable as shown below.

Example:

####set up data####
#using ggplot2 package
library(ggplot2)

df <- data.frame(c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4), 
                 c(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4),
                 c(256, 4, 196, 16, 144, 36, 100, 64, 81, 49, 121, 25, 169, 9, 225, 1),
                 c(0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610))
colnames(df) <- c("X", "Y", "V1", "V2")

####plot1: Simple plot of V1 vs V2#####
 #I would like the color to go towards blue as V1 increases on x axis (as it is now)
 #I would also like the color to go towards red as V2 increases on y axis
 #Ideally, when both x and y are large, it would be some purple color

plot1 <- ggplot(df, aes(x=V1, y = V2, color= V1)) + geom_point()+ 
  scale_color_continuous(low="black", high="light blue")+ 
  labs(y= "V2: I want this to go from black --> red", x = "V1: black--> blue")
plot1

####plot2: plot of x vs y but with color assignment same as plot1####
#The position based on X and Y is correct but,
#   I'd like to have the color assigned based on V1 and V2 (same as plot1)
plot2 <- ggplot(df, aes(x=X, y=Y, color=V1)) + geom_point()+
  scale_color_continuous(low="black", high="light blue")
plot2
bhraynor
  • 33
  • 3

1 Answers1

0

I've tried a modification scoa's answer here, maybe this will do the trick. You may have to manually build your legends though, as this approach makes the default legend unappealing.

With your data above:

df <- data.frame(X = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4), 
                 Y = c(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4),
                 V1 = c(256, 4, 196, 16, 144, 36, 100, 64, 81, 49, 121, 25, 169, 9, 225, 1),
                 V2 = c(0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610))

First, scale your V1 and V2 values to a max R and B values for an RGB color:

xcol <- df$V1 / max(df$V1) 
ycol <- df$V2 / max(df$V2) 

Now, make a named color vector using rgb() and the scaled values:

col <- rgb(ycol, 0, xcol)
names(col) <- as.character(df$V1)

Now you can plot, passing V1 as a factor to the color argument:

ggplot(df, aes(x = V1, y = V2, color = as.factor(V1))) +
  geom_point(size = 5) +
  scale_color_manual(values = col) +
  theme_classic() +
  theme(legend.position = "none") 

enter image description here

And for the second plot, keep the color arguments the same:

ggplot(df, aes(x=X, y=Y, color=as.factor(V1))) +
  geom_point(size = 5) +
  scale_color_manual(values = col) +
  theme_classic() +
  theme(legend.position = "none") 

enter image description here

Luke C
  • 10,081
  • 1
  • 14
  • 21
  • Thank you this works perfectly. For anyone referencing this in the future, I was able to apply it to my much larger data set. I had to switch the column I assigned the color by to my equivalent of V2 for some reason... maybe because it has a much larger scale than the V1 in my data set. – bhraynor Jul 02 '18 at 15:37