Home > Back-end >  ggplot only black dots and no colors
ggplot only black dots and no colors

Time:10-29

I have a dataframe in R as this (only sample posted here):

              DE      logFC     pval log2fc_color  rank
1    Upregulated  3.4958900 264.1088      #00519e   110
2    Upregulated  2.9649858 254.4343      #00519e   222
3    Upregulated  1.5794640 246.1799      #00519e   988
4    Upregulated  1.6678800 235.9541      #00519e   901
5    Upregulated  3.9374453 229.3586      #00519e    62
6          Top10  6.2492052 227.5845      #d51030     4
7          Top10  7.4608894 153.1061      #d51030     1
8          Top10  6.4259618 135.0821      #d51030     3
9          Top10  5.9704932 118.9760      #d51030     7
10         Top10  6.0201382 112.0736      #d51030     6
11 Downregulated -2.6035129 311.1072      #00519e  2624
12 Downregulated -3.8199728 306.3362      #00519e  3023
13 Downregulated -2.8384308 299.8498      #00519e  2749
14 Downregulated -2.9654265 299.6843      #00519e  2804
15 Downregulated -5.4785903 297.3173      #00519e  3135
16            NA -0.8830659 189.6105      #a7a7a7 17041
17            NA  0.9867408 181.4278      #a7a7a7    30
18            NA -0.9681908 170.5315      #a7a7a7 17357
19            NA  0.8636595 166.6765      #a7a7a7   451
20            NA  0.9811910 165.4120      #a7a7a7    44

and I am trying to do a volcano plot. I would like to color by different category of DE and I also have the color defined in the log2fc_color column. However, when I try to plot using the following command, I am only getting black dots.

ggplot(df.t1, aes(x=logFC, y=pval, fill=log2fc_color))   geom_point()

or

ggplot(df.t1, aes(x=logFC, y=pval, fill=DE))   geom_point()

enter image description here

CodePudding user response:

Two things going on here:

  1. geom_point can use fill=, but for most shapes it has no visual effect; for this, you either need to shift to using colour= or shift to a shape= that uses the fill attribute (e.g., shape=21);
  2. Based on the #rrggbb nature of your log2fc_color, I believe you want the literal colors in the column, not a color-factor based on the categorical values of the strings; wrap it in I(.) to force the colors themselves.
ggplot(df.t1, aes(x=logFC, y=pval, color=I(log2fc_color)))  
  geom_point()

ggplot2 with literal color assignment

Or, using colour=DE and manual colors, which adds a legend:

ggplot(df.t1, aes(x=logFC, y=pval, color=DE))  
  geom_point()  
  scale_colour_manual(
    values = c(Upregulated = "#00519e", Top10 = "#d51030", Downregulated = "#00519e"),
    na.value = "#a7a7a7"
  )

(Use scale_colour_manual(guide = FALSE, ...) if you want to suppress the legend in this case.)

ggplot2 with colour scale and legend

  • Related