firmo23
firmo23

Reputation: 8404

Replace data labels in ggplot2 heatmap with actual column names in a shiny app

Hello I have a simple shiny app which creates a heatmap. The problem is that after I melt the initial dataset into "gen3" I get label names like gen3[,1]: DADA but I want to take gene_symbol: DATA instead (I want the same with the other labels as well). Is this possible with ggplot2? I assume I should use paste() somehow or manually change the labels.

#data
gene_symbol<-c("DADA","SDAASD","SADDSD","SDADD")
ASDDA<-c("normal","over","low","over")
ASDDb<-c("normal","over","low","over")
ASDDAf<-c("normal","over","low","over")
Gene_states2<-data.frame(gene_symbol,ASDDA,ASDDb,ASDDAf)

#ui.r
library(shiny)
library(ggplot2)
library(plotly)
library(extrafont)
library(dplyr)

fluidPage(

  # App title ----
  titlePanel(div("GENES HEATMAP",style = "color:blue")),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(width = 3

    ),
    # Main panel for displaying outputs ----
    mainPanel(

      tabsetPanel(type = "tabs",
                  tabPanel("Heatmap",

                           fluidRow(
                             tags$style(type="text/css",
                                        ".shiny-output-error { visibility: hidden; }",
                                        ".shiny-output-error:before { visibility: hidden; }"

                             ),
                             plotlyOutput("sc")
                           )


                  ))
      )))
#server.r
function(input, output,session) {

rt<-reactive({ req(input$file1)

csvdata <- read.csv(input$file1$datapath,
                    header = input$header

)

if(input$disp == "head"){
  head(csvdata)
} else{
  csvdata
} 

row.has.na <- apply(csvdata, 1, function(x){any(is.na(x))})
csvdata2 <- csvdata[!row.has.na,]

csvdata2

})

  output$sc<-renderPlotly({
    library(ggplot2); library(reshape2)
    gen3 <- melt(rt(), id.var = colnames(rt())[1])
    p1<-ggplot(gen3, aes(gen3[,1],gen3[,2])) + geom_tile(aes(fill = gen3[,3]),
                                                         colour = "white") + scale_fill_manual(values=c("yellow", "red", "blue"))+
      labs(x = "gene_symbol",y="sample",fill="value")+
      theme(title = element_text(family = "Calibri", 
                                 size = 10, 
                                 face = "bold"), 
            axis.title = element_text(family = "Calibri Light", 
                                      size = 16, 
                                      face = "bold", 
                                      color = "black"), 
            axis.text = element_text(family = "Calibri", 
                                     size = 11),
            axis.title.y = element_text(margin = margin(t = 0, r = 25, b = 0, l = 25)),
            panel.background = element_rect(fill = NA),
            panel.grid.major = element_line(colour = "grey50"))
    ggplotly(p1)%>%
      layout( autosize = F, width = 1450, height = 600,hoverlabel = list(bgcolor = "white",
                                                                         font = list(family = "Calibri", 
                                                                                     size = 9, 
                                                                                     color = "black")))

  })  

}

Upvotes: 0

Views: 1146

Answers (1)

Beeba
Beeba

Reputation: 642

In ggplot there is no need to specify the data.frame name in aesthetics (aes) after you've already told it which data.frame to use.

Simply do this: ggplot(gen3, aes(gene_symbol, variable)) + geom_tile(aes(fill= value) ... and so on.

If you don't want variable and value to show up in your hoverlabel, just change colnames of gen3 after you melt it to whatever you do want to show up.

Upvotes: 1

Related Questions