Commit ebad17f8 authored by arsenij.ustjanzew's avatar arsenij.ustjanzew
Browse files

scatter plot outsourced as function

parent 92b66b3e
#' Renders a scatter plot
#'
#' @param labels A list with sample names, that should be of the same length as x and y.
#' @param x Numeric values mapped to the x-axis. In case of a nested list, a dropdown menu will be provided in the interactive mode.
#' @param y Numeric values mapped to the y-axis. In case of a nested list, a dropdown menu will be provided in the interactive mode.
#' @param colour_by A factor that will be mapped to colours. In case of a nested list, a dropdown menu will be provided in the interactive mode.
#'
#' @return A list with 1. the plotly object & 2. the data frame used in the plot
#' @export
.scatter_plot <- function(labels = NULL, x, y, colour_by = NULL, checkbox = FALSE, selected_label = NULL){
# create data.frame for plot (fill colour_by & labels with dummy data if NULL)
dummy_values <- FALSE
plot_list <- list()
if (!is.null(labels)) plot_list <- rlist::list.append(plot_list, labels) else plot_list <- rlist::list.append(plot_list, c(1:length(x[[1]])))
plot_list <- rlist::list.append(plot_list, c(x, y))
if (!is.null(colour_by)) {
plot_list <- rlist::list.append(plot_list, colour_by)
} else {
plot_list <- rlist::list.append(plot_list, c(1:length(x[[1]])))
dummy_values <- TRUE
}
plot_df <- do.call("data.frame", plot_list)
# assign variables
labels <- plot_df[[1]]
x_value <- plot_df[[2]]
y_value <- plot_df[[3]]
colour_by <- plot_df[[4]]
x_title <- names(plot_df[2])
y_title <- names(plot_df[3])
if(dummy_values) {
colour_by <- NULL
}
# if(is.null(labels)) {
# labels <- NULL
# }
if(is.null(checkbox)) {
checkbox <- FALSE
}
# find point for annotation
a <- NULL
if (!is.null(labels)) {
if (checkbox) {
colour_by <- NULL
point_index <- match(selected_label, plot_df[[1]])
point <- plot_df[point_index,]
a <- list(
x = point[,2],
y = point[,3],
text = point[,1],
xref = "x",
yref = "y",
showarrow = T,
arrowhead = 7,
arrowcolor = "red",
ax = 20,
ay = -40
)
}
}
# plotly
p <- plotly::plot_ly(plot_df,
x = x_value,
y = y_value,
color = colour_by,
text = labels
)
p <- plotly::add_markers(p)
p <- plotly::layout(p,
xaxis = list(title = x_title),
yaxis = list(title = y_title),
annotations = a)
return(p)
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment