R/plot_melted_data.R

Defines functions plot_melted_data

#' Plot melted data. variable should contain samples, label should contain go_labels.
#' value should contain the metric values.
#' Additionaly, a column "metric" can be used to plot in facets (one column for each metric) multiple metrics
#' TODO: A column "ontology" should be added to plot in facets (one row for each ontology).
#' A column "signif_binom" and "signif_hyper" containing TRUE/FALSE can be added to bold/italicize values when sample/goTerm pair pass these tests. 
#'
#' @export
plot_melted_data <- function(melted,
                             outpath){
    n_samples <- length(unique(melted$variable))
    n_go_terms <- length(unique(melted$label))
    if (is.null(melted$metric)){
        n_metrics <- 1
    } else {
        n_metrics <- length(unique(melted$metric))
    }
    n_char_samples <- max(nchar(as.character(melted$variable)))
    n_char_go_terms <- max(nchar(as.character(melted$label)))
    n_rows_metrics <- 2 # I may improve this looking at how much '\n' I have in 'metric' and multiply this number by 0.2 + 0.1 for spacing.

    width <- 0.25 * n_samples * n_metrics + 0.15 * n_char_go_terms
    height_top <- 0.2 * n_rows_metrics + 0.1
    height_heatmap <- 0.2 * n_go_terms
    height_bottom <- 0.2 + 0.15 * n_char_samples
    height <- height_top + height_heatmap + height_bottom 
    
    p <- ggplot2::ggplot(data = melted, ggplot2::aes(x = variable, y = label))
    p <- p + ggplot2::geom_tile(ggplot2::aes(fill = scaled))
    p <- p + ggplot2::geom_text(ggplot2::aes(color = scaled,
                           label = round(value, 1),
                           fontface = ifelse(signif_hyper,
                                             ifelse(signif_binom,
                                                    "bold.italic",
                                                    "bold"),
                                             ifelse(signif_binom,
                                                    "italic",
                                                    "plain")
                                             )
                           )
    )
    if ('metric' %in% colnames(melted)){
        p <- p + ggplot2::facet_grid(. ~ metric) #cols = ggplot2::vars(metric))
    }
    #p <- p + scale_fill_gradient(low = "white", high = "darkblue", guide=FALSE)
    #p <- p + scale_color_gradient2(low = "black", mid="yellow", high = "white", midpoint=0.5, guide=FALSE)
    #p <- p + scale_fill_gradientn(colors=c('#C3C3C5','#ABABDA','#5B5BC8','#2121D8','#0000BE'),guide=FALSE)
    #p <- p + scale_color_gradientn(colors=c('#FFBF00','#FFBF01','#FFD65C','#FFEFBE','#FFFEFB'),guide=FALSE)
    #p <- p + scale_fill_gradientn(colors=c('#CACACA','#667F93','#2A506D','#052B48','#001322'),guide=FALSE)
    #p <- p + scale_color_gradientn(colors=c('#351F00','#704000','#AA7A3A','#E4C499','#FFFFFF'),guide=FALSE)
    p <- p + ggplot2::scale_fill_gradientn(colors=c('#FFFFFF','#C3DAFF','#3986FF','#00307D','#00060E'),guide=FALSE)
    p <- p + ggplot2::scale_color_gradientn(colors=c('#160E00','#160E00','#FFB025','#FFE7BD','#FFFFFF'),guide=FALSE)

    # Does not work:
    # Error: Continuous value supplied to discrete scale
    #p <- p + scale_fill_brewer(palette = palette_name)
    p <- p + ggplot2::theme(axis.text.x  = ggplot2::element_text(angle=320, vjust=1, hjust=0),
                   legend.position = "bottom")
    p <- p + ggplot2::scale_y_discrete(position = "right")

    #p <- p + scale_fill_gradientn(limits=quantile(dm,c(0.01,0.99)))
    p <- p + ggplot2::xlab('Samples')
    p <- p + ggplot2::ylab('Gene Ontology Terms')
    #p <- p + labs(fill = fillLabel)
    ggplot2::ggsave(filename=outpath,
           plot=p, 
           width=width,
           height=height, 
           limitsize=F)
}
guillaumecharbonnier/greatr documentation built on Nov. 8, 2019, 8:06 p.m.