merging two different colors with ggplot2 geom_point plot

34 views
Skip to first unread message

Eric Foss

unread,
Feb 16, 2018, 5:58:29 PM2/16/18
to ggplot2
(I also posted this on the R forum at talkstats.com.)

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

 

merging_two_colors_ggplot_question_021618_1.R

Eric Foss

unread,
Feb 19, 2018, 3:03:37 PM2/19/18
to ggplot2
Here is a little more discussion of my question at stackoverflow

David Kahle

unread,
Feb 19, 2018, 4:24:30 PM2/19/18
to Eric Foss, ggplot2
Maybe something like this?

library("tidyverse"); theme_set(theme_bw())
set.seed(8)

# make the dataset
n_x <- 100
n_y <- 250

list(x = 1:n_x, y = 1:n_y) %>% 
  cross_df() %>% 
  mutate(
    z1 = rnorm(n_x*n_y) + 1.5*(50 < y & y <= 100),
    z1 = as.vector(scale(z1)),
    z2 = rnorm(n_x*n_y) + 1.5*(75 < y & y <= 125),
    z2 = as.vector(scale(z2))
  ) -> df

  

# plot both datasets (z1 and z2)
df %>% 
  gather(which_z, value, -x, -y) %>% 
  ggplot(aes(x, y, fill = value)) +
    geom_raster() +
    facet_wrap(~ which_z)

# look only at z1
df %>% 
  ggplot(aes(x, y, fill = z1)) +
  geom_raster() +
  scale_fill_gradient2(low = "red", mid = "white", high = "green")

df %>% 
  ggplot(aes(x, y, fill = z1, alpha = (z1 >= 0))) +
  geom_raster() +
  scale_fill_gradient2(low = "red", mid = "white", high = "green") +
  scale_alpha_manual(
    values = c("TRUE" = 1, "FALSE" = 0),
    guide = FALSE
  )

df %>% 
  ggplot(aes(x, y, fill = z1, alpha = (z1 <= 0))) +
  geom_raster() +
  scale_fill_gradient2(low = "red", mid = "white", high = "green") +
  scale_alpha_manual(
    values = c("TRUE" = 1, "FALSE" = 0),
    guide = FALSE
  )

Cheers
david.

===================================

David J. Kahle, Ph.D.
Associate Professor
Department of Statistical Science
Baylor University
www.kahle.io

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages