tss28
tss28

Reputation: 75

How to specify two thresholds for ggplot?

I have a dataset called volcano that looks like this:

    DiffMean     P.value
 -0.0246757556   0.1
  0.0050993889   0.002
 -0.0169992614   0.008
  0.0039905857   0.03
 -0.0081568420   0.02
 -0.0279989935   0.03
  0.0313951281   0.44   
 -0.0097932018   0.22
 -0.1033745673   0.003
  0.1143251388   0.02
 -0.0738617112   0.004
 -0.0011579184   0.1
 -0.0008561962   0.022   
  0.0435398270   0.11
 -0.0380242369   0.05
  0.1533720177   0.03

I want to plot this using ggplot, but I want the colors to be red if DiffMean < 0 and P.value < 0.05 or blue if DiffMean > 0 and P.value < 0.05.

What I have so far is:

 volcano$threshold = as.factor(abs(volcano$DiffMean)>0 & volcano$p.value.adj< 0.05)
 ggplot(data=volcano, aes(x=DiffMean, y=-1*log10(p.value), colour=threshold)) +
 geom_point(aes(alpha=0.4, size=1.75)) +
 xlim(c(-1,1)) + ylim(c(0,25))

But I don't know how to use this two thresholds.

Upvotes: 1

Views: 1623

Answers (1)

agstudy
agstudy

Reputation: 121588

I would do something like this :

volcano$threshold <- 
  factor(ifelse(volcano$DiffMean>0 & volcano$p.value< 0.05,
                1,
                ifelse(volcano$DiffMean<0 & volcano$p.value< 0.05,
                       -1,
                        0)
                ))
library(ggplot2)
ggplot(data=volcano, aes(x=DiffMean, y=-1*log10(p.value), colour=threshold)) +
  geom_point(alpha=0.4, size=5) +
  scale_y_log10()

enter image description here

Upvotes: 1

Related Questions