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

Danby

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!

Peter

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ggplot2 histogram: how do I add textual annotation onto histogram bars using ggplot2

Using GraphicsMagick or ImageMagick, how do I replace transparency with a fill colour?

ggplot2 - How can I have two colour schemes when using facet_wrap?

How do I fill the points of my box plot to be black in ggplot2?

How do I map the colour (not fill) of a ggplot polygon to a factor variable?

How can I plot the relative proportions of two groups using a fill aesthetic in ggplot2?

How do I fill a bar plot with a specific colour palette according to the variables?

How do I tell R to fill the circle dots with colour on a scatter plot?

How can I overlay points and lines onto a contour plot with ggplot2?

Why is tkinter using a fill colour of black instead of the colour I specify?

ggplot2 - how to fill nested polygons with colour?

How to plot a colour wheel by using ggplot?

How to plot barchart onto ggplot2 map

How do I reorder this bar plot in R using ggplot2?

How do I change the color of my bar plot using ggplot2 package of R?

How do I write a function for a plot in ggplot2 using correctly aes?

How do I get rid of my double legend in forest plot using GGPLOT2?

How do I plot just the residuals in plotly or ggplot2?

How to do a fancy box-plot using ggplot2?

How can i plot an fda object using ggplot2?

How to fill another colour for range of distribution on a plot

How do I colour genes in groups and have gene labels above the arrows in ggplot2?

How do I change the bucket fill colour in Gimp Image Editor?

How to preserve scale_fill_color from first plot using shapefiles in ggplot2

How do I specify the encoding in an print() statement?

How to italicize t and p values using stat_pvalue_manual() in ggplot2

How do I make a colour plot with undefined regions?

How do i colour the max value on a plot() in R?

How to plot a barplot using ggplot2