Port scatter plot to plotly

This commit is contained in:
Elias Projahn 2021-10-15 12:24:28 +02:00
parent fcd9b4ee05
commit 114193506b
3 changed files with 24 additions and 42 deletions

View file

@ -1,5 +1,5 @@
library(data.table) library(data.table)
library(ggplot2) library(plotly)
#' Draw a scatter plot containing gene positions. #' Draw a scatter plot containing gene positions.
#' #'
@ -8,10 +8,6 @@ library(ggplot2)
#' @param genes Genes to be displayed. #' @param genes Genes to be displayed.
#' @param distances Distance data to display. #' @param distances Distance data to display.
scatter_plot <- function(results, species, genes, distances) { scatter_plot <- function(results, species, genes, distances) {
if (nrow(genes) < 1) {
return(ggplot())
}
species_ids <- species[, id] species_ids <- species[, id]
data <- merge( data <- merge(
@ -20,30 +16,22 @@ scatter_plot <- function(results, species, genes, distances) {
by.x = "id", by.y = "gene" by.x = "id", by.y = "gene"
) )
ggplot(data) + data[name == "", name := "Unknown"]
scale_x_discrete(
name = "Species", plot_ly(
breaks = species$id, data = data,
labels = species$name x = ~species,
) + y = ~distance,
scale_y_continuous( color = ~id,
name = "Distance to telomeres [Mbp]", name = ~name,
limits = function(x) { type = "scatter",
if (x[2] < 15) { mode = "markers"
c(0, 15) ) |> layout(
} else { xaxis = list(
x title = "Species",
} tickvals = species_ids,
} ticktext = species[, name]
) + ),
scale_color_discrete(name = "Gene") + yaxis = list(title = "Distance to telomeres [Bp]")
geom_point( )
mapping = aes(
x = species,
y = distance / 1000000,
color = name
),
size = 5
) +
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))
} }

View file

@ -148,7 +148,7 @@ server <- function(input, output) {
) )
}) })
output$scatter <- renderPlot({ output$scatter <- renderPlotly({
results <- results() results <- results()
gene_ids <- results[input$genes_rows_selected, gene] gene_ids <- results[input$genes_rows_selected, gene]

14
ui.R
View file

@ -62,16 +62,10 @@ ui <- fluidPage(
p("This plot shows the selected genes' distance to the telomeres \ p("This plot shows the selected genes' distance to the telomeres \
across species. It visualizes how certain genes have \ across species. It visualizes how certain genes have \
evolutionary conserved positions."), evolutionary conserved positions."),
div( plotlyOutput(
style = "overflow-x: auto", "scatter",
div( width = "100%",
style = "min-width: 1400px", height = "600px"
plotOutput(
"scatter",
width = "100%",
height = "600px"
)
)
), ),
h3("Gene set enrichment analysis"), h3("Gene set enrichment analysis"),
checkboxInput( checkboxInput(