RShiny interactive volcano plots
1
1
Entering edit mode
7.1 years ago
chetana ▴ 60

Hi everyone,

I'm a newbie to Rshiny and I'm trying to make a Rshiny app using this link mentioned here (https://bioinformatics-core-shared-training.github.io/shiny-bioinformatics/interactive-plots), it uses ggplot+plotly to make the plot interactive. I want to try it on my timepoint data and have different timepoints as options for selectinput(). But the problem is that for each timepoint there would be different X (Foldchange ) and Y (-log10Pvalue) axes. I'm not sure how to implement this on my data using the example in the above mentioned link. Any suggestion regarding this approach will be really appreciated. Thanks in advance for your help.

R Rshiny Differential-expression • 5.1k views
ADD COMMENT
0
Entering edit mode

What does this have to do with bioinformatics, please?

ADD REPLY
2
Entering edit mode

I'm happy to explain, Michael. Bioinformatics is an interdisciplinary field and according to me if we're trying to solve a biological problem with a programmatic approach I consider it to be a bioinformatics problem. Since I'm working with biological data/ expression data I think it is relevant to call it a bioinformatics issue. Please let me know if you have any other concerns. Thanks!

ADD REPLY
0
Entering edit mode

Sorry, this was not evident from the original post.

ADD REPLY
0
Entering edit mode

I fixed the URL in your post here

As for your problem, its hard to know for sure without seeing an example of your data, but I would suggest adding an extra column in your dataframe for a time-point factor level (e.g. timepoint) for each entry, and then include faceting as described here;

ggplot(differentialExpressionResults, aes(x = logFC, y = minusLog10Pvalue, colour = probe.type)) +
      geom_point() +
      xlab("log fold change") +
      ylab("-log10(P-value)") +
      theme(legend.position = "bottom") + 
      facet_grid(. ~ timepoint)

This should give you multiple plots side-by-side, one for each factor level.

Alternatively, you could adjust the color, fill, or group to use the new timepoint factor level, though this would be more confusing to look at since all points for all timepoint's would be displayed in the same plot.

ADD REPLY
0
Entering edit mode

Thanks for your reply Steve and sorry if I was not clear asking the question. Here I include the headers of the dataframe as an example as the data is really huge.

probe gene_symbol Timepoint0_FC Timepoint0_P Timepoint3_FC Timepoint3_P Timepoint7_F Timepoint7_P

Here FC is the foldchange and P is the adj. p. value for each timepoint mentioned there ie 0, 3, 7,.., 24. I'm actually working with the second part of the code mentioned in the link and named as Plotly, where the author uses plotly and ggplot to make the interactive volcano plot (https://bioinformatics-core-shared-training.github.io/shiny-bioinformatics/interactive-plots). I kind of got the script to work with just one timepoint.

I don't intend to show all the timepoint data at once but want to implement in the control widgets as a select box feature shown in this link (https://shiny.rstudio.com/tutorial/written-tutorial/lesson3/) implemented using selectinput() where we can list out the options that the user wishes to chose. For example if the user selects timepoint_3 the plot should display FC vs P of timepoint 3 which is extracted from the dataframe above. This is my thought behind it, but I'm not sure how to implement it. Thanks for looking into it.

ADD REPLY
0
Entering edit mode
`ui <- fluidPage(
 titlePanel("Volcano Plotly"),
 fluidRow(
   column(
     width = 10,
       plotlyOutput("volcanoPlot", height = "500px")
    ),
      column(
         width = 8,
           dataTableOutput("selectedProbesTable")
         )
     )
)
server <- function(input, output) {
 differentialExpressionResults <-
read.table("Annotated_Time24_ICL004.txt", stringsAsFactors = FALSE, header = TRUE) %>%
mutate(
  minusLog10Pvalue = -log10(P),
  tooltip = ifelseis.na(gene_symbol), probe, paste(gene_symbol, " (", probe, ")", sep = ""))
) %>%
sample_n(16667)      
output$volcanoPlot <- renderPlotly({

plot <- differentialExpressionResults %>%
  ggplot(aes(x = logFC,
             y = minusLog10Pvalue,
             colour = Pattern_number,
             text = tooltip,
             key = row.names(differentialExpressionResults))) +
  geom_point() +
  xlab("log fold change") +
  ylab("-log10(P-value)")

plot %>%
  ggplotly(tooltip = "tooltip") %>%
  layout(dragmode = "select")
})

output$selectedProbesTable <- renderDataTable({

  eventData <- event_data("plotly_selected")

  selectedData <- differentialExpressionResults %>% slice(0)
if (!is.null(eventData)) selectedData <- differentialExpressionResults[eventData$key,]

selectedData %>%
  transmute(
    probe,
    gene = gene_symbol,
    Pattern_number,
    `log fold change` = signif(logFC, digits = 2),
    `p-value` = signif(P, digits = 2)
    )
 },
  options = list(dom = "tip", pageLength = 10, searching = FALSE)
  )
}

 shinyApp(ui, server, options = list(height = 600))`

Here is the code that I used for just one timepoint.

ADD REPLY
0
Entering edit mode

Please use ADD COMMENT/ADD REPLY when responding to existing posts to keep threads logically organized.

You can always add new information to your original/existing posts by editing. Submit Answers should only be used for answers for the original question.

ADD REPLY
3
Entering edit mode
7.1 years ago
Jake Warner ▴ 840

Assuming the rest of the app works you can use the selectInput() to change the data. It looks like the DE test is loaded at the read.table call? If that's true you can do:

ui <- 
## your code before ...
    selectInput(inputId= 'DEtest', label='Pick a timepoint!', choices = c("3","7","24"...etc))
## your code after ...

server <-
    ## your code before ...

    ## make filename reactive
    inputFile = reactive({
        paste0("Annotated_Time", input$DEtest,"_ICL004.txt")
    })

    output$volcanoPlot <- renderPlotly({

    ## I moved these into the rednerPlotly function
    differentialExpressionResults <- read.table(inputFile(), stringsAsFactors = FALSE, header = TRUE) %>%
    mutate(
      minusLog10Pvalue = -log10(P),
      tooltip = ifelseis.na(gene_symbol), probe, paste(gene_symbol, " (", probe, ")", sep = ""))
    ) %>%
    sample_n(16667)  

    plot <- differentialExpressionResults %>%
      ggplot(aes(x = logFC,
             y = minusLog10Pvalue,
             colour = Pattern_number,
             text = tooltip,
             key = row.names(differentialExpressionResults))) +
            geom_point() +
            xlab("log fold change") +
            ylab("-log10(P-value)")

    plot %>%
      ggplotly(tooltip = "tooltip") %>%
      layout(dragmode = "select")
    })

## your code after

Note I moved the read.table() and ensuing functions into the renderPloty so they are reactive. Otherwise you have to wrap them in reactive({})

Edit: filled out function and I shouldn't have used file as a variable name so is now inputFile.

ADD COMMENT
0
Entering edit mode

Hi Jacob, it worked. Thank you so much.

ADD REPLY
0
Entering edit mode

I moved @Jacob's comment to an answer. You can accept it (green check mark) to provide closure to this thread.

ADD REPLY
0
Entering edit mode

Cheers. Since it's an answer now I cleaned it up a bit for the Googles.

ADD REPLY

Login before adding your answer.

Traffic: 1738 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6