3

I am making a plot where the pvalues are plotted as text using geom_text. The pvalues are given in the file pvaluesmir21combined.

I want to add a red colour to my text (FDR) if the value is < 5e-02. Any idea how this can be done?

ggplot(TumorNormalmiR21_5p.m3, aes(X2,value)) + 
  geom_dotplot(aes(fill=variable),binaxis = "y") + coord_flip() +
  theme_bw(base_size=8) +
  theme(axis.text.y=element_text(hjust = 0)) +
  geom_text(aes(x, y, label=FDR, group=NULL),data=pvaluesmir21combined,size=2)


> pvaluesmir21combined

           FDR  x  y
1  p = 8.3e-02  1 13
2  p = 6.3e-05  2 13
3  p = 3.2e-17  3 13
4  p = 4.8e-22  4 13
5  p = 3.1e-10  5 13
6  p = 6.7e-11  6 13
7  p = 3.2e-24  7 13
8  p = 2.1e-06  8 13
9  p = 1.9e-02  9 13
10 p = 9.4e-06 10 13
11 p = 1.5e-03 11 13
smci
  • 32,567
  • 20
  • 113
  • 146
BioMan
  • 694
  • 11
  • 23
  • Specify a vector of colors to `element_text`. See http://stackoverflow.com/questions/20609716/changing-format-of-some-axis-labels-in-ggplot2-according-to-condition – Roman Luštrik Jul 17 '15 at 08:31
  • 1
    Something like `geom_text(aes(x, y, label=FDR, group=NULL, colour=ifelse(as.numeric(sub("p = ", "", FDR)) < 5e-02 , "red", "black")),data=pvaluesmir21combined,size=2)`. – lukeA Jul 17 '15 at 08:34
  • it worked. However, they became red and turquoise. – BioMan Jul 17 '15 at 08:37
  • You should use `scale_color_manual` or any other function to choose the color – PAC Jul 17 '15 at 08:59

2 Answers2

4

You can try with the function scale_color_manual

ggplot(TumorNormalmiR21_5p.m3, aes(X2,value)) + 
 geom_dotplot(aes(fill=variable),binaxis = "y") + coord_flip() +
 theme_bw(base_size=8) +
 theme(axis.text.y=element_text(hjust = 0)) +
 geom_text(aes(x, y, label=FDR, group=NULL, color = ifelse(as.numeric(sub("p = ", "", FDR)) < 5e-02, 0, 1),data=pvaluesmir21combined,size=2) + 
 scale_color_manual(values = c("red", "black"))

See this link http://docs.ggplot2.org/0.9.3.1/scale_manual.html

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
PAC
  • 5,178
  • 8
  • 38
  • 62
  • Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous Error in eval(expr, envir, enclos) : object 'x' not found – BioMan Jul 17 '15 at 09:07
  • This works: geom_text(aes(x, y, label=FDR, colour=colours),data=pvaluesmir21combined,size=2) + scale_color_manual(values=colours) + – BioMan Jul 17 '15 at 09:12
  • colours <- ifelse(as.numeric(sub("p = ", "", pvaluesmir21combined$FDR)) < 5.0e-02 , "red", "black") – BioMan Jul 17 '15 at 09:13
0

I just want to add another alternative solution, I've found useful to add a ** in case the p-value is significant (< 5.0e-02). The only thing is that you would need to be aware on the x,y coordinates that you are using. It would be something like

geom_text(data = pvaluesmir21combined, aes(label = ifelse(as.numeric(sub("p = ", "", FDR)) < 5e-02, "**", " ")), color = "red" ) + 
MEC
  • 71
  • 1
  • 10