0
votes

Hello I am trying to overlay values of different measurements taken at the same coordinates with ggplot. The measurements have X and Y coordinates and intensity values for the different conditions. I would like the intensity values to be plotted in different colors and both be visible by using alpha. I've tried different combinations of alpha/color and fill arguments but I can't seem to find the right one. Any help would be appreciated

library(ggplot2)
library(reshape2)

#sample data
data <- data.frame(coordX = c(5, 10, 15, 20), coordY = c(5, 10, 20, 30), var1 = c(0.0, 0.02, 0.02, 0.02), var2 = c(0.34, 0.26, 0.31, 0.28))

library(reshape2)

#my best attempt so far
ggplot(data = melt(data, id.vars = c("coordX", "coordY"), value.name = "pixel.intensity"), aes(x = coordX, y = coordY)) + geom_point(aes(color= variable, fill = pixel.intensity, alpha =0.5))
2

2 Answers

0
votes

If I understand you correctly, you want to plot two points from two different groups at the same location, but have the two groups being different colors. The alpha of each point will be controlled by a fourth continuous variable, so that the apparent color at each plotted location will be a mix of the two colors, weighted according to this fourth variable.

This kinda works, but not as well as you would hope, for a number of reasons.

To start with, the data you provided is far too unbalanced in favour of the second group, so the points will basically all just appear blue. We therefore need different values of the pixel.intensity variable to show the effect does work:

library(ggplot2)

df <- structure(list(coordX = c(5, 10, 15, 20, 5, 10, 15, 20), coordY = c(5, 
10, 20, 30, 5, 10, 20, 30), variable = structure(c(1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L), .Label = c("var1", "var2"), class = "factor"), 
    pixel.intensity = c(0, 0.42, 0.51, 0.02, 0.34, 0.1, 0.15, 
    0.28)), row.names = c(NA, -8L), class = "data.frame")


ggplot(df, aes(x = coordX, y = coordY)) + 
  geom_point(aes(alpha = pixel.intensity, color = variable), size = 5) +
  scale_alpha_identity()

You can see you need a substantial difference in alpha for the points to have anything other than a gray, washed out tinge. Also, if both alphas are low, the points will be very faint.


EDIT

From comments by the OP, it is apparent that we are actually talking about mixing RGB colors here. In that case, suppose we had the following data frame, with red, blue and green intensities scaled between 0 and 1:

red   <- rep(c(seq(0, 1, 0.1), rep(1, 10)), 21)
green <- rep(c(rep(1, 10), seq(1, 0, -0.1)), 21)
blue  <- rev(rep(c(seq(0, 1, 0.1), rep(1, 10)), each = 21))
red   <- red * rev(blue)
green <- green * rev(blue)

x <- rep(0:20, 21)
y <- rep(0:20, each = 21)

df <- data.frame(x, y, red, blue, green)

Then we can plot the rgb values by simply doing this;

df$rgb <- rgb(red, green, blue)

ggplot(df, aes(x, y, color = rgb)) + geom_point(size = 5) + scale_color_identity()

enter image description here

Or if you want to actually emulate pixels:

ggplot(df, aes(x, y, fill = rgb)) + geom_tile() + scale_fill_identity()

enter image description here Created on 2020-08-02 by the reprex package (v0.3.0)

0
votes

Does this help?

#sample data
data <- data.frame(coordX = c(5, 10, 15, 20), coordY = c(5, 10, 20, 30), var1 = c(0.0, 0.02, 0.02, 0.02), var2 = c(0.34, 0.26, 0.31, 0.28))

library(reshape2)
data2 = melt(data, id.vars = c("coordX", "coordY"), value.name = "pixel.intensity")

ggplot(data2,
       aes(x = jitter(coordX), y = jitter(coordY), 
           alpha = pixel.intensity, color = variable, fill= variable)) + 
  geom_point() +
  theme_bw()

I pulled the melt out of the code so it was easier to look at the data and I scooted the variables around a bit so you could see both.