visualize significant upregultaed genes and downregultaed genes with different colors (EnhancedVolcano)
1
0
Entering edit mode
gaom001 ▴ 10
@gaom001-24019
Last seen 3.7 years ago

Hi, I have a DEGs file (DEAdf) with log2FC and padjust. I am trying to look at significant dowreguated and upregulated genes in enhanced volcano plot according to my cutoff. To visulize the up and down regulated genes, I customized them into different colors (keyvals), but it only came out with 2 colors.

I attached my code below:

> keyvals <- ifelse(
>         (DEAdf$log2FC>1& DEAdf$padjust>=0.05) | (DEAdf$log2FC<-1&DEAdf$padjust>=0.05) , 'forestgreen',
>         ifelse(
>                 DEAdf$log2FC<-1 & DEAdf$padjust<0.05, 'royalblue',
>                 ifelse(
>                         DEAdf$log2FC >1 & DEAdf$padjust<0.05, 'red2',       
>                         ifelse(
>                                 DEAdf$log2FC >= -1 & DEAdf$log2FC <= 1 & DEAdf$padjust<0.05 ,'yellow',  
>         'grey30'))))
> 
> names(keyvals)[keyvals == 'grey30'] <- 'No significant'
> names(keyvals)[keyvals == 'forestgreen'] <- 'Log2 FC'
> names(keyvals)[keyvals == 'yellow'] <- 'Adjusted p-value'
> names(keyvals)[keyvals == 'royalblue'] <- 'Adjusted p-value & Log2 FC
> <-1' names(keyvals)[keyvals == 'red2'] <- 'Adjusted p-value & Log2 FC
> > 1'
> 
> library(EnhancedVolcano) mypval=0.05 myfc=2 mypadj=0.05
> mylog2fc=log2(myfc)
> 
> EnhancedVolcano(DEAdf,
>                 lab = DEAdf$idnames,
>                 x = 'log2FC',
>                 y = 'padjust',
>                 xlim =  c(min(DEAdf$log2FC, na.rm=TRUE), max(DEAdf$log2FC, na.rm=TRUE)),
>                 ylim=c(0, max(-log10(DEAdf$padjust), na.rm=TRUE) + 1),
>                 title="",
>                 subtitle=comparing,
>                 pCutoff=mypadj,
>                 FCcutoff=mylog2fc,
>                 pointSize = 2.0,
>                 labSize = 3.0,
>                 colCustom = keyvals,
>                 colAlpha = 1,
>                 xlab = bquote(~Log[2]~ 'fold change'),
>                 ylab = bquote(~-Log[10]~adjusted~italic(P)),
>                 legendPosition = 'top',
>                 legendLabSize = 10,
>                 legendIconSize = 5.0,
>                 drawConnectors = F, # Add lines to show more labels
>                 widthConnectors = 0.2,
>                 colConnectors = 'grey30') dev.off()

enter image description here

enhancedvolcano • 2.5k views
ADD COMMENT
0
Entering edit mode

I think my keyvals have some problems:

keyvals= ifelse(
        DEAdf$log2FC>1& DEAdf$padjust<0.05, 'red2',
        ifelse(
                DEAdf$log2FC<-1 & DEAdf$padjust<0.05, 'royalblue',
                ifelse(
                        DEAdf$log2FC<=-1 & DEAdf$padjust>=0.05, 'forestgreen', 
                        ifelse(
                                DEAdf$log2FC>=-1 & DEAdf$padjust<0.05, 'yellow',
        'grey30'))))
ADD REPLY
0
Entering edit mode
Kevin Blighe ★ 3.9k
@kevin
Last seen 2 days ago
Republic of Ireland

The problem is likely with if statements like this:

DEAdf$log2FC < -1 & DEAdf$padjust<0.05, 'royalblue',

You would have to instead write:

DEAdf$log2FC < (1 * -1) & DEAdf$padjust < 0.05, 'royalblue',

Otherwise, R interprets the <- as an assingation.

Kevin

-----------

Edit:

To clarify, user has used DEAdf$log2FC<-1 ..., which will cause an error. Either of these will work fine:

DEAdf$log2FC < -1 ...
DEAdf$log2FC<(-1) ...
ADD COMMENT
1
Entering edit mode

You don't need to do all that to ensure that R can disambiguate between assignment and comparison. Simply being careful to add spaces at the right place, which is good coding practice anyway, will do the same.

> 3 < -1
[1] FALSE
> 3<-1
Error in 3 <- 1 : invalid (do_set) left-hand side to assignment

So cleaning up the code like this

keyvals <-  ifelse(
        DEAdf$log2FC > 1 & DEAdf$padjust < 0.05, 'red2',
        ifelse(
                DEAdf$log2FC < -1 & DEAdf$padjus < 0.05, 'royalblue',
                ifelse(
                        DEAdf$log2FC <= -1 & DEAdf$padjust >= 0.05, 'forestgreen', 
                        ifelse(
                                DEAdf$log2FC >= -1 & DEAdf$padjust < 0.05, 'yellow',
        'grey30'))))

Is A) much more readable, and B) will actually do what the OP wants.

ADD REPLY
0
Entering edit mode

Thanks James

ADD REPLY
1
Entering edit mode

Thank you, guys, I made it. Just did a little modification from James' Code. (DEAdf$log2FC <= -1 & DEAdf$padjust >= 0.05)|(DEAdf$log2FC >=1 & DEAdf$padjust >= 0.05), 'forestgreen',

keyvals <-  ifelse(
        DEAdf$log2FC > 1 & DEAdf$padjust < 0.05, 'red2',
        ifelse(
                DEAdf$log2FC < -1 & DEAdf$padjus < 0.05, 'royalblue',
                ifelse(
                        (DEAdf$log2FC <= -1 & DEAdf$padjust >= 0.05)|(DEAdf$log2FC >=1 & DEAdf$padjust >= 0.05), 'forestgreen', 
                        ifelse(
                                DEAdf$log2FC >= -1 & DEAdf$padjust < 0.05, 'yellow',
                                'grey30'))))

enter image description here

ADD REPLY
1
Entering edit mode

R won't interpret DEAdf$log2FC < -1 as an assignment ... it will correctly be parsed as a "less than -1" expression so we wouldn't have to do the DEAdf$log2FC < (1 * -1) gymnastics.

R will, however, interpret a "no space" statement as assignment, like the DEAdf$log2FC<-1 that the OP has in the original post ...

ADD REPLY
1
Entering edit mode

Exactly, that is the problem (although I didn't make it clear in my original answer). There is a difference between <-1 and < -1

ifelse(-2<-1, 'y', 'n')
Error in -2 <- 1 : target of assignment expands to non-language object

ifelse(-2 < -1, 'y', 'n')
[1] "y"

As of right now, I do not know what the user has tried and where

ADD REPLY

Login before adding your answer.

Traffic: 684 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6