R/heatmap.R

Defines functions keggHeatmap heatmapKegg

<<<<<<< HEAD
library(enrichGene)
library(DT)
library(dplyr)
library('tidyverse')
library('limma')
library('DT')
library('ggplot2')
library('plotly')
library('AnnotationDbi')
library('org.Mm.eg.db')

listagenes <- read.csv("~/Escritorio/proteomics/genes.csv", row.names = 1, stringsAsFactors = F )
kgg <- readRDS("R/kgg.Rds")

colourCount <- length(unique(kgg$DE)) # number of levels
getPalette <- colorRampPalette(RColorBrewer::brewer.pal(9, "YlOrRd"))



kgg %>%
  filter(Pathway %in% rutas) %>%
  dplyr::select(Pathway, genes,DE) %>%
  separate_rows(genes) %>%
  mutate(Pathway = fct_inorder(Pathway)) %>%
  mutate(Pathway = fct_rev(Pathway)) %>%
  mutate(genes = fct_infreq(genes)) %>%
  mutate(DE = factor(DE)) %>%
  ggplot(aes_(~genes, ~Pathway)) +
  geom_tile(aes_(fill = ~DE), color = 'black', size =0.2) +
  xlab(NULL) + ylab(NULL) +
  theme_minimal() +
  theme(panel.grid.major = element_line(colour = "gray88", size = 0.8),
        axis.text.x=element_text(angle = 90, hjust = 1, vjust = 0.5, size=5))+
  scale_fill_manual(values = getPalette(colourCount))


enrichdf<-kgg
keggHeatmap <- function(enrichdf, nrows=10){
    if (!is.data.frame(enrichdf)) {
        stop("enrichdf should be data.frame")
    }
     enrichdf <- enrichdf %>% arrange(desc(DE))
    enrichdf <- enrichdf[seq_len(nrows), ]
    colourCount <- length( unique( enrichdf[[orderby]] ) ) # number of levels
    getPalette <- colorRampPalette(c("red","blue"))
    p <- enrichdf %>%
        dplyr::select(Pathway, genes, DE) %>%
        separate_rows(genes) %>%
        mutate(Pathway = fct_inorder(Pathway)) %>%
        mutate(Pathway = fct_rev(Pathway)) %>%
        mutate(genes = fct_infreq(genes))
        q <- p %>% mutate(DE = factor(DE)) %>%
        ggplot(aes_( ~ genes, ~ Pathway)) +
        geom_tile(aes_(fill = ~ DE ), color = 'black', size = 0.2) +
        xlab(NULL) + ylab(NULL) +
        theme_minimal() +
        theme(
            panel.grid.major = element_line(colour = "gray88", size = 0.8),
            axis.text.x = element_text(
                angle = 90,
                hjust = 1,
                vjust = 0.5,
                size = 5)) +
        scale_fill_manual(values = getPalette(colourCount) )
    q
}

keggHeatmap(kgg)












=======
#kdt dataframe generado por kegg2DT()
#nr filas a representar
heatmapKegg <- function(kdt, nr){
    kdt <- kdt[nr, ]
    colourCount <- length(unique(kdt$DE)) # number of levels
    getPalette <- colorRampPalette(RColorBrewer::brewer.pal(9, "YlOrRd"))
    kdt %>% dplyr::select(Pathway, genes,DE) %>%
        separate_rows(genes) %>%
        mutate(Pathway = fct_inorder(Pathway)) %>%
        mutate(Pathway = fct_rev(Pathway)) %>%
        mutate(genes = fct_infreq(genes)) %>%
        mutate(DE = factor(DE)) %>%
        ggplot(aes_(~genes, ~Pathway)) +
        geom_tile(aes_(fill = ~DE), color = 'black', size =0.2) +
        xlab(NULL) + ylab(NULL) +
        theme_minimal() +
        theme(panel.grid.major = element_line(colour = "gray88", size = 0.8),
              axis.text.x=element_text(angle = 90, hjust = 1, vjust = 0.5, size=5))+
        # scale_fill_continuous(low="blue", high="red", name = "N")
        # scale_fill_brewer(palette = "YlOrRd")
        scale_fill_manual(values = getPalette(colourCount))

}
>>>>>>> a1fa5973cd03ec19db33ae5f7df4674f49917743
fpsanz/enrichGene documentation built on Feb. 20, 2020, 3:20 a.m.