clDistPlot.R 2.9 KB
Newer Older
dmattek's avatar
Mod:  
dmattek committed
1 2 3 4
modClDistPlotUI =  function(id, label = "Plot Fractions WIthin Clusters") {
  ns <- NS(id)
  
  tagList(
dmattek's avatar
dmattek committed
5 6
    radioButtons(ns("rBAxisLabelsRotate"), "X-axis labels:",
                 c("horizontal" = 0,
dmattek's avatar
dmattek committed
7
                   "45 deg" = 45,
dmattek's avatar
dmattek committed
8
                   "90 deg" = 90)),
dmattek's avatar
Mod:  
dmattek committed
9 10 11 12 13 14 15
    actionButton(ns('butPlotClDist'), 'Plot!'),
    plotOutput(ns('outPlotClDist'), height = '800px', width = 'auto'),
    downPlotUI(ns('downPlotClDist'), "Download PDF")
  )
}


dmattek's avatar
Mod:  
dmattek committed
16 17 18 19 20 21 22

# Params:
# in.data - data prepared with data4clDistPlot f-n
# in.cols - table with 1st column as cluster number, 2nd column colour assignments
#           prepared with getClColHier
# in.fname - file name for plot download
modClDistPlot = function(input, output, session, in.data, in.cols = NULL, in.fname = 'clDist.pdf') {
dmattek's avatar
Mod:  
dmattek committed
23 24 25 26 27 28 29 30 31 32 33 34 35
  
  ns <- session$ns
 
  # Barplot with distribution of clusters across conditions
  plotClDist = function() {
    cat(file = stderr(), 'plotClDist: in\n')
    
    loc.dt = in.data()
    if (is.null(loc.dt)) {
      cat(file = stderr(), 'plotClDist: dt is NULL\n')
      return(NULL)
    }
    
36 37 38 39 40
    # Two statements: "position_fill(reverse = TRUE)" and "guide_legend(reverse = T)"
    # result in  stacked bar plot with categories ordered from the bottom to top of the stacked bar
    p.out = ggplot(loc.dt[], aes(x = group, y = nCells)) +
      geom_bar(aes(fill = as.factor(cl)), stat = 'identity', position = position_fill(reverse = TRUE)) +
      guides(fill = guide_legend(reverse = T))
dmattek's avatar
Mod:  
dmattek committed
41 42 43 44 45
    
    if(is.null(in.cols))
      p.out = p.out + scale_fill_discrete(name = "Cluster no.")
    else
      p.out = p.out + scale_fill_manual(name = "Cluster no.", 
46 47 48 49
                                        values = in.cols()$cl.col) #,
                                        #breaks = in.cols()$cl.no,
                                        #labels = in.cols()$cl.no,
                                        #limits = in.cols()$cl.no)
dmattek's avatar
Mod:  
dmattek committed
50 51
    
    
dmattek's avatar
dmattek committed
52 53 54 55
    loc.rads = as.numeric(input$rBAxisLabelsRotate) * pi / 180
    loc.hjust = 0.5*(1-sin(loc.rads))
    loc.vjust = 0.5*(1-cos(loc.rads))
    
dmattek's avatar
Mod:  
dmattek committed
56
    p.out = p.out + 
dmattek's avatar
Mod:  
dmattek committed
57 58 59
      scale_y_continuous(labels = percent) +
      ylab("percentage of cells\n") +  
      xlab("") +  
60 61 62 63 64
      LOCggplotTheme(in.font.base = PLOTFONTBASE, 
                     in.font.axis.text = PLOTFONTAXISTEXT, 
                     in.font.axis.title = PLOTFONTAXISTITLE, 
                     in.font.strip = PLOTFONTFACETSTRIP, 
                     in.font.legend = PLOTFONTLEGEND) + 
dmattek's avatar
dmattek committed
65
      theme(
dmattek's avatar
dmattek committed
66
        axis.text.x = LOCrotatedAxisElementText(as.numeric(input$rBAxisLabelsRotate))
dmattek's avatar
dmattek committed
67
    )
dmattek's avatar
dmattek committed
68

dmattek's avatar
Mod:  
dmattek committed
69 70 71 72
    return(p.out)
    
  }
  
dmattek's avatar
Mod:  
dmattek committed
73
  #  display bar plot
dmattek's avatar
Mod:  
dmattek committed
74 75 76 77 78 79 80 81 82 83 84 85
  output$outPlotClDist <- renderPlot({
    locBut = input$butPlotClDist
    
    if (locBut == 0) {
      cat(file = stderr(), 'outPlotClDist: Go button not pressed\n')
      
      return(NULL)
    }
    
    plotClDist()
  })
  
dmattek's avatar
Mod:  
dmattek committed
86
  # bar Plot - download pdf
dmattek's avatar
Mod:  
dmattek committed
87 88 89
  callModule(downPlot, "downPlotClDist", in.fname, plotClDist, TRUE)
  
}