Upgrade to new Gitlab Version 13.9 on Saturday 19th April 20:00. Expect an interruption of about 30 to 60 minutes

clDistPlot.R 3.22 KB
Newer Older
dmattek's avatar
dmattek committed
1 2 3 4 5 6 7 8
#
# Time Course Inspector: Shiny app for plotting time series data
# Author: Maciej Dobrzynski
#
# This module is for plotting stacked bar plot with percentages of time series in clusters
#

# UI ----
dmattek's avatar
dmattek committed
9
modClDistPlotUI =  function(id, label = "Plot distribution of clusters per groupd") {
dmattek's avatar
Mod:  
dmattek committed
10 11 12
  ns <- NS(id)
  
  tagList(
dmattek's avatar
dmattek committed
13 14
    radioButtons(ns("rBAxisLabelsRotate"), "X-axis labels:",
                 c("horizontal" = 0,
dmattek's avatar
dmattek committed
15
                   "45 deg" = 45,
dmattek's avatar
dmattek committed
16
                   "90 deg" = 90), inline = T),
dmattek's avatar
Mod:  
dmattek committed
17
    actionButton(ns('butPlotClDist'), 'Plot!'),
Maciej Dobrzynski's avatar
Maciej Dobrzynski committed
18
    plotOutput(ns('outPlotClDist'), height = PLOTBOXHEIGHT, width = 'auto'),
dmattek's avatar
Mod:  
dmattek committed
19 20 21 22 23
    downPlotUI(ns('downPlotClDist'), "Download PDF")
  )
}


dmattek's avatar
Mod:  
dmattek committed
24

dmattek's avatar
dmattek committed
25 26
# SERVER ----

dmattek's avatar
Mod:  
dmattek committed
27 28 29 30 31 32
# 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
33 34 35 36 37 38 39 40 41 42 43 44 45
  
  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)
    }
    
46 47 48 49 50
    # 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
51 52 53 54 55
    
    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.", 
56 57 58 59
                                        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
60 61
    
    
dmattek's avatar
dmattek committed
62 63 64 65
    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
66
    p.out = p.out + 
dmattek's avatar
Mod:  
dmattek committed
67
      scale_y_continuous(labels = percent) +
dmattek's avatar
dmattek committed
68
      ylab("Percentage of time series\n") +  
dmattek's avatar
dmattek committed
69
      xlab("Groups") +  
70 71 72 73 74
      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
75
      theme(
76 77
        axis.text.x = LOCrotatedAxisElementText(as.numeric(input$rBAxisLabelsRotate), 
                                                size = PLOTFONTAXISTEXT)
dmattek's avatar
dmattek committed
78
    )
dmattek's avatar
dmattek committed
79

dmattek's avatar
Mod:  
dmattek committed
80 81 82 83
    return(p.out)
    
  }
  
dmattek's avatar
Mod:  
dmattek committed
84
  #  display bar plot
dmattek's avatar
Mod:  
dmattek committed
85 86 87 88 89 90 91 92 93 94 95 96
  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
97
  # bar Plot - download pdf
dmattek's avatar
Mod:  
dmattek committed
98 99 100
  callModule(downPlot, "downPlotClDist", in.fname, plotClDist, TRUE)
  
}