2

I have the following data frame;

df <- data.frame(expand.grid(A = seq(0,1,0.1), B = seq(0,1,0.1), C = seq(0,1,0.1)))
df <- df[rowSums(df[,1:3]) == 1,]
df <- data.frame(df, value_A = 0.2 * df$A + -0.4 * df$B + 0 * df$C, value_B = 0.8 * df$A + 0.1 * df$B + 0.5 * df$C, value_C = 0 * df$A + 0.8 * df$B + 0 * df$C)
df[,c(4:6)] <- (df[,c(4:6)] + abs(apply(df[,c(4:6)],1,min))) / (apply(df[,c(4:6)],1,max) + abs(apply(df[,c(4:6)],1,min)))

df <- data.frame(df, color = rgb(red=df$value_A, green=df$value_B, blue=df$value_C))

In the data-frame, each row gives me a proportion of A's, B's and C's that sum up to one. Further, for each A, B and C, I have a value.

From these three values I generate one RGB value that gives me an indication of the relative importance of A,B & C under different proportions of A, B and C's.

Now I would like to plot these RGB values as a surface in a ternary plot.

I can plot one "dimension" with the following code using the ggtern package in R;

library(ggtern)

ggtern(df, aes(A,B,C, value=value_A)) + 
theme_showarrows() + 
stat_interpolate_tern(geom="polygon",
                     formula=value~x+y,
                     n=20, method='lm',
                     breaks=seq(0,1, by=0.001),
                     aes(fill=..level..), expand=F
) +
scale_fill_gradient(low="red", high="green") 

But instead of value_A, I actually want to use the RGB values directly.

However, I can not figure out, how, instead of value=value_A I can specify the color value for each point from which the surface is calculated directly.

Is this possible with ggplot2 / ggtern?

grueb
  • 123
  • 11
  • This belongs on [SO], not here. We will migrate this for you. – gung - Reinstate Monica Feb 12 '16 at 18:35
  • Not sure if this will help or not, but you might be able to set the value to RBG color codes using functions like this? https://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/rgb.html – Bajcz Feb 12 '16 at 18:47
  • @ Bajcz; Yes, that is what I use to generate the RGB values .. however, plotting the surface does not take these RGB values directly, but expects a vector of values that is transformed into a color-gradient ... My problem is that I already have the color values and from these values want to create the surface. – grueb Feb 12 '16 at 20:17
  • You might need to take the approach of the heatmap, like this: http://stackoverflow.com/questions/26221236/ternary-heatmap-in-r – Nicholas Hamilton Apr 16 '16 at 07:53

0 Answers0