Home > Software design >  how do I specify colour when using fill in ggplot2 and how to print pvalue onto plot
how do I specify colour when using fill in ggplot2 and how to print pvalue onto plot

Time:09-22

I have the following dataset:

structure(list(Isodecoder = c("Val", "Leu", "Ile", "Arg", "Ser", 
"Ala"), Anticodon = c("AAC", "AAG", "AAT", "ACG", "AGA", "AGC"
), baseMean = c(16330.2876073118, 280025.429470444, 47222.9145508434, 
639520.228162813, 591.699871418142, 194550.678700214), log2FC = c(0.241812205124871, 
0.062182487534649, -0.912898805851376, 0.414430653693951, -0.547252664766089, 
-0.576581451007797), lfcSE = c(0.330123826005643, 0.0835105091511506, 
0.193443262487333, 0.258705563545134, 0.211263611125761, 0.249830083859411
), stat = c(0.732489405719955, 0.744606734729653, -4.71920703834881, 
1.60193947132354, -2.59037825705024, -2.30789439806721), pvalue = c(0.463869906720502, 
0.45650949539292, 2.36765754572021e-06, 0.109168994996796, 0.00958705250629343, 
0.0210050107216242), FDR = c(0.585940934804845, 0.585940934804845, 
1.7165121174307e-05, 0.201542759994085, 0.030678568020139, 0.0591953931677063
), Label = c("Val-AAC", "Leu-AAG", "Ile-AAT", "Arg-ACG", "Ser-AGA", 
"Ala-AGC"), wobble = c("AT_wobble", "AT_wobble", "AT_wobble", 
"AT_wobble", "AT_wobble", "AT_wobble")), row.names = c(NA, 6L
), class = "data.frame")

and I am plotting these as barplots within a larger loop. my barplot function is:

tRNA_barplot <- ggplot(data = df2, aes(x = Label, y = log2FC, fill = wobble, label = ifelse(FDR < 0.05, "*", "ns"))) 
    geom_bar(stat = "identity") 
    geom_text(vjust = 0, nudge_y = ifelse(df2$log2FC < 0, -1, 1), size = ifelse(df2$FDR < 0.05, 6, 4)) 
    xlab("Isoacceptor") 
    ylab(expression(paste(Log[2],"FC",sep=""))) 
    #ylim(c(-5, 5)) 
    theme_bw() 
    theme(axis.text.x = element_text(size = 12, angle = 45, hjust = 1), plot.margin = margin(0.5,0.5,0.5,2, "cm"), axis.title = element_text(size = 12)) 
    ggtitle(iso, "Fed vs Starved - tRNA LFCs")

I've used fill = wobble to colour by wobble position. in barplots where both AT_wobble and GC_wobble are present ... AT_wobble is coloured red and GC_wobble is coloured blue/turquoise. How do I ensure GC_wobble remain blue/turquoise in barplots where it is plotted alone.

by the way ... by blue and red I mean the standard R colour output when not specified

Attached are examples of what i am talking about:

enter image description here

enter image description here

Leu is perfect but I would like plots like Pro to remain blue/turquoise for GC_wobble.

Also, I've specified that all FDR > 0.05 should have ns printed ontop of the barplot [ifelse(FDR < 0.05, "*", "ns")]. Is there a way to print the actual FDR number to 4 decimal places? I've tried numerous ways but they're all really messy.

Thanks!

CodePudding user response:

You need to add scale_fill_manual. Look below.

tRNA_barplot <- ggplot(data = df2, aes(x = Label, y = log2FC, fill = wobble, label = ifelse(FDR < 0.05, "*", "ns"))) 
  geom_bar(stat = "identity") 
  geom_text(vjust = 0, nudge_y = ifelse(df2$log2FC < 0, -1, 1), size = ifelse(df2$FDR < 0.05, 6, 4)) 
  xlab("Isoacceptor") 
  ylab(expression(paste(Log[2],"FC",sep=""))) 
  #ylim(c(-5, 5)) 
  theme_bw() 
  theme(axis.text.x = element_text(size = 12, angle = 45, hjust = 1), plot.margin = margin(0.5,0.5,0.5,2, "cm"), axis.title = element_text(size = 12)) 
  scale_fill_manual(values = c("AT_wobble" = 5))

enter image description here

CodePudding user response:

Here is one approach to the two part question:

Part 1: Using the scales package you can determine the code for any default ggplot colour. I've set this out explicitly in a separate line to make it obvious, you could simplify by including this directly in the call to scale_fill_manual

Part 2: print the actual FDR number using a call to geom_text. Note adjustment to y scale to allow FDR text to avoid clashing with the plot boundary.

Some other points:
a) geom_col() is simpler than geom_bar(stat = "identity")
b) the call to expression() does not need to include paste, check out ?plotmath
c) adjust asterisk size using an ifelse conditional statement for the call to size

 
library(ggplot2)

col_GC_wobble <- scales::hue_pal()(2)[[2]]

scales::hue_pal()(2)[2]
#> [1] "#00BFC4"

ggplot(df2, aes(x = Label, y = log2FC, fill = wobble)) 
  geom_col() 
  geom_text(aes(label = ifelse(FDR < 0.05, "*", round(FDR, 4))),
            vjust = ifelse(df2$log2FC < 0, 1, -0.2),
            size = ifelse(df2$FDR < 0.05, 10, 4)) 
  scale_fill_manual(values = col_GC_wobble) 
  scale_y_continuous(expand = expansion(mult = 0.1)) 
  xlab("Isoacceptor") 
  ylab(expression(Log[2]~FC)) 
  theme_bw() 
  theme(axis.text.x = element_text(size = 12, angle = 45, hjust = 1),
        plot.margin = margin(0.5,0.5,0.5,2, "cm"), 
        axis.title = element_text(size = 12)) 
  ggtitle("Fed vs Starved - tRNA LFCs")

Created on 2021-09-18 by the reprex package (v2.0.0)

  • Related