0
votes

(I posted this same question at the R forum on talkstats.com and on a ggplot2 forum.)

In cell biology, people often publish images in which one protein is labeled green and a second protein is labeled red, and then they show an image of the green protein only, then the red protein only, and then a "merge" to show where both proteins are present. The merge ends up showing mostly green where only protein 1 is, mostly red where only protein 2 is, and yellow where they both are, and you can see gradients where there are increasing levels of protein 2 in a background of constant protein 1, etc. I want to do the same sort of thing with ggplot, with the x and y axis showing locations of two measurements and the intensity of color indicating the magnitude of the measurement. In the attached example, measurements 1 and 2 are similar over most of x and y coordinates, but measurement 1 is a bit higher from y axis 50 to 100 and measurement 2 is a bit higher from y axis 75 to 125. In my example, graph1 gives me what I want (albeit in blue rather than red), but then I show an unsuccessful "graph2" in which measurements 2 simply overwrite measurements 1.

I hope that with the attached script, this confusing question becomes more clear. Can anyone tell me how to make the desired merge of two different colors (ideally red for 1 and green for the other)? (Any constructive criticism of the somewhat ugly way in which I've created the data frame would also be welcome.)

Thanks.

Eric

enter code here

# merging two different colors with ggplot2 geom_point plot

library(tidyverse)

set.seed(8)

x <- seq(1,100)

y <- seq(1,250)

z <- integer(length(x) * length(y))

df <- data.frame('x_coordinate' = rep(x, times = length(y)), 
    'y_coordinate' = rep(y, each = length(x)), 
    'z_coordinate1' = z, 
    'z_coordinate2' = z)

df[,3] <- sample(1:100, length(df[,3]), replace = TRUE)
df[,4] <- sample(1:100, length(df[,3]), replace = TRUE)

for (i in 1:100) { 
    df[df[,1] == i & df[,2] > 50 & df[,2] <= 100,3] <- sample(25:125, 50)
    df[df[,1] == i & df[,2] > 75 & df[,2] <= 125,4] <- sample(25:125, 50)
}

# graph 1:  
ggplot(data = df, 
    mapping = aes(x = x_coordinate, y = y_coordinate, color = z_coordinate1)) + 
    geom_point(size = 0.2)

# unsuccessful graph2:  
ggplot(data = df, 
    mapping = aes(x = x_coordinate, y = y_coordinate, color = z_coordinate1)) + 
    geom_point(size = 0.2) + 
    geom_point(data = df, 
    mapping = aes(x = x_coordinate, y = y_coordinate, color = z_coordinate2), 
    size = 0.2)
1

1 Answers

1
votes

Ggplot is designed for "long" or "tidy" fromatted data. (Hence the 'tidy'verse title of the package. The DF you provide is in long format. Pivot the DF first, then employ the alpha aesthetic to obtain the color superposition you are looking for.

df %>% 
  gather(key = CoordType, value = Intensity, z_coordinate1:z_coordinate2) %>% ## Pivot the 2 last columns into a single column, so each of the DF's row representes a single obeservation
  ggplot(aes(x = x_coordinate, y = y_coordinate, colour = CoordType)) +
  geom_point(aes(alpha = .$Intensity/max(.$Intensity)), ## Create a 1:0 value of transparency (alpha) as an aestethic for every point
             size = 0.2) +
scale_color_manual(values = c("green", "red")) ## state the colours you want.

enter image description here