user2300940
user2300940

Reputation: 2385

add color to geom_text in ggplot

When running the code below I am not able to give colors to the text in geom_text. Does anyone see where the error is? It workes in my other data.

ggplot(TumorNormalmiR148a_3p.m3, aes(X2,value)) + 
  geom_dotplot(aes(fill=Sample),binaxis = "y") + coord_flip() +
  theme_bw(base_size=8) +
  theme(axis.text.y=element_text(hjust = 0)) +
  geom_text(aes(x, y, label=FDR, colour=coloursmir148a),data=pvaluesmir148acombined,size=2, show_guide=F)  +
  scale_color_manual(values=coloursmir148a) +
  labs(y="log2 RPM", x="IsomiRs (hsa-miR-148a-5p)")

colors:

> coloursmir148a
 [1] "black" "black" "red"   "red"   "red"   "red"   "red"   "black" "red"   "red"   "red"   "black" "red"   "red"  
[15] "black" "black" "black" "black" "red"   "red"   "red"   "red"   "red"   "red"   "black" "red"   "red"   "black"
[29] "red"   "black" "red"   "red"   "red"   "black"

pvalues:

> pvaluesmir148acombined
           FDR  x  y
1  p = 8.7e-02  1 13
2  p = 6.2e-02  2 13
3  p = 3.5e-05  3 13
4  p = 2.8e-04  4 13
5  p = 2.6e-05  5 13
6  p = 5.1e-07  6 13
7  p = 8.4e-07  7 13
8  p = 6.3e-01  8 13
9  p = 5.5e-03  9 13
10 p = 8.9e-06 10 13
11 p = 3.4e-07 11 13
12 p = 1.1e-01 12 13
13 p = 1.6e-03 13 13
14 p = 1.6e-03 14 13
15 p = 5.4e-02 15 13
16 p = 1.2e-01 16 13
17 p = 7.1e-02 17 13
18 p = 3.3e-01 18 13
19 p = 6.4e-03 19 13
20 p = 2.3e-04 20 13
21 p = 3.8e-02 21 13
22 p = 8.3e-04 22 13
23 p = 2.1e-03 23 13
24 p = 5.1e-05 24 13
25 p = 1.9e-01 25 13
26 p = 3.7e-03 26 13
27 p = 2.8e-03 27 13
28 p = 4.5e-01 28 13
29 p = 3.8e-04 29 13
30 p = 3.3e-01 30 13
31 p = 8.8e-03 31 13
32 p = 8.4e-04 32 13
33 p = 8.6e-05 33 13
34 p = 9.8e-02 34 13

The data:

> TumorNormalmiR148a_3p.m3
                                      GeneID Sample        value              X1                        X2
1       hsa-miR-148a-3p_AGTGCACTACAGAACTTTGT Normal  0.771999520 hsa-miR-148a-3p      AGTGCACTACAGAACTTTGT
2      hsa-miR-148a-3p_CAGTGCACTACAGAACTTTGT Normal  1.287154580 hsa-miR-148a-3p     CAGTGCACTACAGAACTTTGT
3     hsa-miR-148a-3p_CAGTGCACTACAGAACTTTGTC Normal  2.187647871 hsa-miR-148a-3p    CAGTGCACTACAGAACTTTGTC
4        hsa-miR-148a-3p_TCAGTGCACTACAGAACTT Normal  6.752524925 hsa-miR-148a-3p       TCAGTGCACTACAGAACTT
5       hsa-miR-148a-3p_TCAGTGCACTACAGAACTTT Normal  8.442635161 hsa-miR-148a-3p      TCAGTGCACTACAGAACTTT
6      hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTA Normal  2.173341120 hsa-miR-148a-3p     TCAGTGCACTACAGAACTTTA
7     hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTAA Normal  2.926393331 hsa-miR-148a-3p    TCAGTGCACTACAGAACTTTAA
8     hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTAT Normal  1.449873411 hsa-miR-148a-3p    TCAGTGCACTACAGAACTTTAT
9      hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTG Normal  9.853894140 hsa-miR-148a-3p     TCAGTGCACTACAGAACTTTG
10    hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGA Normal  9.397111369 hsa-miR-148a-3p    TCAGTGCACTACAGAACTTTGA
11   hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGAA Normal  4.305334683 hsa-miR-148a-3p   TCAGTGCACTACAGAACTTTGAA
12  hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGAAA Normal  1.084027377 hsa-miR-148a-3p  TCAGTGCACTACAGAACTTTGAAA
13   hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGAC Normal  0.004323489 hsa-miR-148a-3p   TCAGTGCACTACAGAACTTTGAC
14   hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGAT Normal  1.792349294 hsa-miR-148a-3p   TCAGTGCACTACAGAACTTTGAT
15    hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGC Normal  3.622005753 hsa-miR-148a-3p    TCAGTGCACTACAGAACTTTGC
16    hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGG Normal  2.428371837 hsa-miR-148a-3p    TCAGTGCACTACAGAACTTTGG
17    hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGT Normal 13.315790055 hsa-miR-148a-3p    TCAGTGCACTACAGAACTTTGT
18   hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTA Normal  7.118488238 hsa-miR-148a-3p   TCAGTGCACTACAGAACTTTGTA
19  hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTAA Normal  4.771188637 hsa-miR-148a-3p  TCAGTGCACTACAGAACTTTGTAA
20 hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTAAA Normal  0.263900902 hsa-miR-148a-3p TCAGTGCACTACAGAACTTTGTAAA
21  hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTAT Normal  2.554341244 hsa-miR-148a-3p  TCAGTGCACTACAGAACTTTGTAT
22   hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTC Normal 12.443752172 hsa-miR-148a-3p   TCAGTGCACTACAGAACTTTGTC
23  hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTCA Normal  5.457857372 hsa-miR-148a-3p  TCAGTGCACTACAGAACTTTGTCA
24  hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTCC Normal  1.136643196 hsa-miR-148a-3p  TCAGTGCACTACAGAACTTTGTCC
25  hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTCG Normal  1.351033273 hsa-miR-148a-3p  TCAGTGCACTACAGAACTTTGTCG
26  hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTCT Normal  9.005237355 hsa-miR-148a-3p  TCAGTGCACTACAGAACTTTGTCT
27 hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTCTT Normal  0.962934496 hsa-miR-148a-3p TCAGTGCACTACAGAACTTTGTCTT
28   hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTT Normal  8.760597462 hsa-miR-148a-3p   TCAGTGCACTACAGAACTTTGTT
29  hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTTA Normal  4.804634779 hsa-miR-148a-3p  TCAGTGCACTACAGAACTTTGTTA
30  hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTTG Normal  0.411903244 hsa-miR-148a-3p  TCAGTGCACTACAGAACTTTGTTG
31  hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTTT Normal  7.974917053 hsa-miR-148a-3p  TCAGTGCACTACAGAACTTTGTTT
32 hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTTTT Normal  2.561256273 hsa-miR-148a-3p TCAGTGCACTACAGAACTTTGTTTT
33     hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTT Normal  2.257174177 hsa-miR-148a-3p     TCAGTGCACTACAGAACTTTT
34    hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTTT Normal  1.061006153 hsa-miR-148a-3p    TCAGTGCACTACAGAACTTTTT
35      hsa-miR-148a-3p_AGTGCACTACAGAACTTTGT  Tumor  1.310698364 hsa-miR-148a-3p      AGTGCACTACAGAACTTTGT
36     hsa-miR-148a-3p_CAGTGCACTACAGAACTTTGT  Tumor  1.937236644 hsa-miR-148a-3p     CAGTGCACTACAGAACTTTGT
37    hsa-miR-148a-3p_CAGTGCACTACAGAACTTTGTC  Tumor  3.325631898 hsa-miR-148a-3p    CAGTGCACTACAGAACTTTGTC
38       hsa-miR-148a-3p_TCAGTGCACTACAGAACTT  Tumor  6.068923394 hsa-miR-148a-3p       TCAGTGCACTACAGAACTT
39      hsa-miR-148a-3p_TCAGTGCACTACAGAACTTT  Tumor  7.518270841 hsa-miR-148a-3p      TCAGTGCACTACAGAACTTT
40     hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTA  Tumor  1.182664552 hsa-miR-148a-3p     TCAGTGCACTACAGAACTTTA
41    hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTAA  Tumor  2.124052190 hsa-miR-148a-3p    TCAGTGCACTACAGAACTTTAA
42    hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTAT  Tumor  1.748184901 hsa-miR-148a-3p    TCAGTGCACTACAGAACTTTAT
43     hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTG  Tumor  9.377855816 hsa-miR-148a-3p     TCAGTGCACTACAGAACTTTG
44    hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGA  Tumor  8.663287427 hsa-miR-148a-3p    TCAGTGCACTACAGAACTTTGA
45   hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGAA  Tumor  3.487867176 hsa-miR-148a-3p   TCAGTGCACTACAGAACTTTGAA
46  hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGAAA  Tumor  0.726020881 hsa-miR-148a-3p  TCAGTGCACTACAGAACTTTGAAA
47   hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGAC  Tumor  0.927396886 hsa-miR-148a-3p   TCAGTGCACTACAGAACTTTGAC
48   hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGAT  Tumor  1.241343458 hsa-miR-148a-3p   TCAGTGCACTACAGAACTTTGAT
49    hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGC  Tumor  4.258175322 hsa-miR-148a-3p    TCAGTGCACTACAGAACTTTGC
50    hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGG  Tumor  3.016538579 hsa-miR-148a-3p    TCAGTGCACTACAGAACTTTGG
51    hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGT  Tumor 13.802813336 hsa-miR-148a-3p    TCAGTGCACTACAGAACTTTGT
52   hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTA  Tumor  7.540749259 hsa-miR-148a-3p   TCAGTGCACTACAGAACTTTGTA
53  hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTAA  Tumor  5.609024723 hsa-miR-148a-3p  TCAGTGCACTACAGAACTTTGTAA
54 hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTAAA  Tumor  1.410504309 hsa-miR-148a-3p TCAGTGCACTACAGAACTTTGTAAA
55  hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTAT  Tumor  3.190967221 hsa-miR-148a-3p  TCAGTGCACTACAGAACTTTGTAT
56   hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTC  Tumor 13.298492973 hsa-miR-148a-3p   TCAGTGCACTACAGAACTTTGTC
57  hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTCA  Tumor  6.351985283 hsa-miR-148a-3p  TCAGTGCACTACAGAACTTTGTCA
58  hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTCC  Tumor  2.409922139 hsa-miR-148a-3p  TCAGTGCACTACAGAACTTTGTCC
59  hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTCG  Tumor  1.833656206 hsa-miR-148a-3p  TCAGTGCACTACAGAACTTTGTCG
60  hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTCT  Tumor  9.849472676 hsa-miR-148a-3p  TCAGTGCACTACAGAACTTTGTCT
61 hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTCTT  Tumor  1.954490774 hsa-miR-148a-3p TCAGTGCACTACAGAACTTTGTCTT
62   hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTT  Tumor  9.082469578 hsa-miR-148a-3p   TCAGTGCACTACAGAACTTTGTT
63  hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTTA  Tumor  5.798622044 hsa-miR-148a-3p  TCAGTGCACTACAGAACTTTGTTA
64  hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTTG  Tumor  0.788161684 hsa-miR-148a-3p  TCAGTGCACTACAGAACTTTGTTG
65  hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTTT  Tumor  8.803714361 hsa-miR-148a-3p  TCAGTGCACTACAGAACTTTGTTT
66 hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTGTTTT  Tumor  3.630516106 hsa-miR-148a-3p TCAGTGCACTACAGAACTTTGTTTT
67     hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTT  Tumor  1.347143694 hsa-miR-148a-3p     TCAGTGCACTACAGAACTTTT
68    hsa-miR-148a-3p_TCAGTGCACTACAGAACTTTTT  Tumor  1.739820923 hsa-miR-148a-3p    TCAGTGCACTACAGAACTTTTT

Upvotes: 1

Views: 2759

Answers (1)

jlhoward
jlhoward

Reputation: 59335

Looking at your data, it seems like you want the labels to be red if p<0.05 and black otherwise (e.g., significant, not significant). Here's a better way to achieve this result.

[ In what follows I'm calling your main data.frame df, and the data.frame containing the labels lbls. You don't need a vector of colors. ]

library(ggplot2)
ggplot(df, aes(X2,value)) + 
  geom_dotplot(aes(fill=Sample),binaxis = "y") + coord_flip() +
  theme_bw(base_size=10) +
  theme(axis.text.y=element_text(hjust = 0)) +
  geom_text(aes(x, y=max(df$value)+2, label=FDR, 
                color=ifelse(as.numeric(gsub("p = ","",FDR))<0.05,"sig","not.sig")),
            data=lbls,size=2, show_guide=F)  +
  scale_color_manual(values=c(sig="red",not.sig="black")) +
  labs(y="log2 RPM", x="IsomiRs (hsa-miR-148a-5p)")

So in this we extract the p-value from the FDR column in lbls using:

as.numeric(gsub("p = ","",FDR)

and set the color aesthetic to "sig" or "not.sig" depending on whether the p-value is < 0.05 using:

color=ifelse(as.numeric(gsub("p = ","",FDR))<0.05,"sig","not.sig")

Then we use scale_color_manual(...) as in the comment, passing a named vector as the values=... argument:

values=c(sig="red",not.sig="black")

Also, we use max(df$values)+2 for the y-aesthetic in geom_text(...) so that all the labels are to the right of the right-most points.

Finally, I assume you must have the p-values somewhere else to create the labels, so you could just use that instead of parsing the FDR column, but I don't have access to that part of your dataset.

Upvotes: 5

Related Questions