EnhancedVolcano.R 7.49 KB
Newer Older
fsoubes's avatar
fsoubes committed
1 2 3
# Author: Kevin Blighe
# Title: Publication-ready volcano plots with enhanced colouring and labelign
# Github: https://github.com/kevinblighe/EnhancedVolcano
fsoubes's avatar
fsoubes committed
4
# Modified by Franck Soubès (add 74-85;  modify 98-107, add 3 parameters displayLab, findfamily, topgenes)
fsoubes's avatar
fsoubes committed
5 6 7 8 9 10 11 12

EnhancedVolcano <- function(
    toptable,
    lab,
    x,
    y,
    selectLab = NULL,
    displaylab = NULL,
fsoubes's avatar
fsoubes committed
13
    findfamily = NULL,
fsoubes's avatar
fsoubes committed
14 15 16 17 18 19 20 21 22 23 24 25
    topgenes = NULL,
    xlim = c(min(toptable[,x], na.rm=TRUE),
        max(toptable[,x], na.rm=TRUE)),
    ylim = c(0, max(-log10(toptable[,y]), na.rm=TRUE) + 5),
    xlab = bquote(~Log[2]~ "fold change"),
    ylab = bquote(~-Log[10]~italic(P)),
    axisLabSize = 16,
    pCutoff = 0.05,
    pLabellingCutoff = pCutoff,
    FCcutoff = 2.0,
    title = "",
    titleLabSize = 16,
fsoubes's avatar
fsoubes committed
26 27
    transcriptPointSize = 2.8,
    transcriptLabSize = 5.0,
fsoubes's avatar
fsoubes committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    col = c("grey30", "forestgreen", "royalblue", "red2"),
    colAlpha = 1/2,
    legend = c("NS","Log2 FC","P","P & Log2 FC"),
    legendPosition = "top",
    legendLabSize = 10,
    legendIconSize = 3.0,
    DrawConnectors = FALSE,
    widthConnectors = 0.5,
    colConnectors = "black",
    cutoffLineType = "longdash",
    cutoffLineCol = "black",
    cutoffLineWidth = 0.4)
    
{
    if(!requireNamespace("ggplot2")) {
        stop("Please install ggplot2 first.", call.=FALSE)
    }

    if(!requireNamespace("ggrepel")) {
        stop("Please install ggrepel first.", call.=FALSE)
    }

    if(!is.numeric(toptable[,x])) {
        stop(paste(x[i], " is not numeric!", sep=""))
    }

    if(!is.numeric(toptable[,y])) {
        stop(paste(x[i], " is not numeric!", sep=""))
    }

    requireNamespace("ggplot2")
    requireNamespace("ggrepel")
    requireNamespace("dplyr")
    i <- xvals <- yvals <- Sig <- NULL
fsoubes's avatar
fsoubes committed
62
    
fsoubes's avatar
fsoubes committed
63
    toptable <- as.data.frame(toptable)
fsoubes's avatar
fsoubes committed
64 65 66 67 68
    toptable$GeneName <- sapply(toptable$GeneName, function(v) {
      if (is.character(v)) return(toupper(v))
      else return(v)
    })

fsoubes's avatar
fsoubes committed
69 70 71 72 73 74 75 76 77 78 79 80 81
    toptable$Sig <- "NS"
    toptable$Sig[(abs(toptable[,x]) > FCcutoff)] <- "FC"
    toptable$Sig[(toptable[,y]<pCutoff)] <- "P"
    toptable$Sig[(toptable[,y]<pCutoff) &
        (abs(toptable[,x])>FCcutoff)] <- "FC_P"
    toptable$Sig <- factor(toptable$Sig,
        levels=c("NS","FC","P","FC_P"))
  
    
    
    if(is.na(topgenes) && !is.na(displaylab) ){
      selectLab <- as.character(displaylab)
    }
fsoubes's avatar
fsoubes committed
82 83 84 85 86
    else if(is.na(topgenes) && is.na(displaylab) && !is.na(findfamily) )
      selectLab <- as.character(findfamily)
    else if(is.na(topgenes)&& is.na(displaylab)&& is.na(findfamily)){
      selectLab <- ""
    }
fsoubes's avatar
fsoubes committed
87 88
    else{
      toptable$abs <- unlist(abs(toptable[x]))
fsoubes's avatar
fsoubes committed
89 90 91 92
      myval <- toptable %>%   dplyr::filter(Sig =="FC_P") %>% dplyr::select(GeneName,X,abs)  %>% top_n(.,topgenes)
      myvalueind <- myval$X
      selectLab <- as.character(myval$GeneName)
      
fsoubes's avatar
fsoubes committed
93
    }
fsoubes's avatar
fsoubes committed
94
  
fsoubes's avatar
fsoubes committed
95 96 97 98 99 100
      
    if (min(toptable[,y], na.rm=TRUE) == 0) {
        warning("One or more P values is 0. Converting to minimum possible value...", call. = FALSE)
        toptable[which(toptable[,y] == 0), y] <- .Machine$double.xmin
    }
    
fsoubes's avatar
fsoubes committed
101 102 103 104 105
    toptable$lab <-  sapply(toptable$GeneName, function(v) {
      if (is.character(v)) return(toupper(v))
      else return(v)
    })
    
fsoubes's avatar
fsoubes committed
106 107 108
    toptable$xvals <- toptable[,x]
    toptable$yvals <- toptable[,y]
    
fsoubes's avatar
fsoubes committed
109 110 111 112 113 114 115 116 117 118 119 120
    
    
    
    
   if (!is.null(selectLab)) {
    if(!is.na(topgenes) && is.na(displaylab)&& is.na(findfamily)){
    names.new <- rep("", length(toptable$lab))
    indices <- which(toptable$X %in% myvalueind)
    names.new[indices] <- as.character(toptable$GeneName[indices])
    toptable$lab <- names.new
    }
    else {
fsoubes's avatar
fsoubes committed
121
        names.new <- rep("", length(toptable$lab))
fsoubes's avatar
fsoubes committed
122
        indices <- which(toptable$GeneName %in% selectLab)
fsoubes's avatar
fsoubes committed
123 124
        names.new[indices] <- as.character(toptable$GeneName[indices])
        toptable$lab <- names.new
fsoubes's avatar
fsoubes committed
125
      }
fsoubes's avatar
fsoubes committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    }

    
    tot = subset(toptable,
           toptable[,y]<pLabellingCutoff &
             abs(toptable[,x])>FCcutoff)[,"lab"]
   
    plot <- ggplot2::ggplot(toptable,
            ggplot2::aes(x=xvals, y=-log10(yvals))) +

        ggplot2::geom_point(ggplot2::aes(color=factor(Sig)),
            alpha=colAlpha, size=transcriptPointSize) +

        ggplot2::scale_color_manual(values=c(NS=col[1],
            FC=col[2],
            P=col[3],
            FC_P=col[4]),
            labels=c(NS=legend[1],
            FC=paste(legend[2], sep=""),
            P=paste(legend[3], sep=""),
            FC_P=paste(legend[4], sep=""))) +

        ggplot2::theme_bw(base_size=24) +

        ggplot2::theme(
            legend.background=ggplot2::element_rect(),
            plot.title=ggplot2::element_text(angle=0,
                size=titleLabSize,
                face="bold",
                vjust=1),

            panel.grid.major=ggplot2::element_blank(),
            panel.grid.minor=ggplot2::element_blank(),

            axis.text.x=ggplot2::element_text(angle=0,
                size=axisLabSize,
                vjust=1),
            axis.text.y=ggplot2::element_text(angle=0,
                size=axisLabSize,
                vjust=1),
            axis.title=ggplot2::element_text(size=axisLabSize),

            legend.position=legendPosition,
            legend.key=ggplot2::element_blank(),
            legend.key.size=ggplot2::unit(0.5, "cm"),
            legend.text=ggplot2::element_text(
                size=legendLabSize),
            title=ggplot2::element_text(
                size=legendLabSize),
            legend.title=ggplot2::element_blank()) +

        ggplot2::guides(colour = ggplot2::guide_legend(
            override.aes=list(size=legendIconSize))) +

        ggplot2::xlab(xlab) +
        ggplot2::ylab(ylab) +

        ggplot2::xlim(xlim[1], xlim[2]) +
        ggplot2::ylim(ylim[1], ylim[2]) +

        ggplot2::ggtitle(title) +

        ggplot2::geom_vline(xintercept=c(-FCcutoff, FCcutoff),
            linetype=cutoffLineType,
            colour=cutoffLineCol,
            size=cutoffLineWidth) +

        ggplot2::geom_hline(yintercept=-log10(pCutoff),
            linetype=cutoffLineType,
            colour=cutoffLineCol,
            size=cutoffLineWidth)

    if (DrawConnectors == TRUE) {
        plot <- plot + ggrepel::geom_text_repel(max.iter = 100,
            data=subset(toptable,
                toptable[,y]<pLabellingCutoff &
                    abs(toptable[,x])>FCcutoff),
            ggplot2::aes(label=subset(toptable,
                toptable[,y]<pLabellingCutoff &
                    abs(toptable[,x])>FCcutoff)[,"lab"]),
                size = transcriptLabSize,
                segment.color = colConnectors,
                segment.size = widthConnectors,
                vjust = 1.0)
    } else if (DrawConnectors == FALSE && !is.null(selectLab)) {
        plot <- plot + ggplot2::geom_text(data=subset(toptable,
                toptable[,y]<pLabellingCutoff &
                    abs(toptable[,x])>FCcutoff),
            ggplot2::aes(label=subset(toptable,
                toptable[,y]<pLabellingCutoff &
                    abs(toptable[,x])>FCcutoff)[,"lab"]),
                size = transcriptLabSize,
		check_overlap = T,
                vjust = 1.0)
    } else if (DrawConnectors == FALSE && is.null(selectLab)) {
        plot <- plot + ggplot2::geom_text(data=subset(toptable,
                toptable[,y]<pLabellingCutoff &
                    abs(toptable[,x])>FCcutoff),
            ggplot2::aes(label=subset(toptable,
                toptable[,y]<pLabellingCutoff &
                    abs(toptable[,x])>FCcutoff)[,"lab"]),
                size = transcriptLabSize,
                check_overlap = F,
                vjust = 1.0)
    }

    return(plot)
}