Commit 1f25995b authored by arsenij.ustjanzew's avatar arsenij.ustjanzew
Browse files

outcourced violin_plot() method & changed the rmd of features_by_factors

parent 0061a275
......@@ -3,3 +3,4 @@
export(.bar_plot)
export(.box_plot)
export(.scatter_plot)
export(.violin_plot)
......@@ -23,10 +23,20 @@ features_by_factors <- function(object, x, y, title = "Features by factor") {
env$x_selection <- FALSE
env$y_selection <- FALSE
# Check existence of x and y
if(is.null(x)) stop("x is required.")
if(is.null(y)) stop("y is required.")
# Create lists if needed
if(!is.list(x)) x <- list(x = x)
if(!is.list(y)) y <- list(y = y)
# should I use magrittr::%<>% ?
# name the lists
library(magrittr)
if(is.null(names(x))) x %<>% magrittr::set_names("x")
if(is.null(names(y))) y %<>% magrittr::set_names("y")
# Check validity
if(!all(sapply(y, is.numeric))) stop("y should only contain numeric values.")
if(!all(sapply(x, is.factor))) stop("x should only contain factorial values.")
......
......@@ -146,3 +146,33 @@
)
return(list("plot" = p, "df" = df))
}
#' Render a violin plot
#'
#' @param x A list with the x-axis values. If it is a nested list, a dropdown-field will be provided in the interactive mode.
#' @param y A list with the y-axis values. If it is a nested list, a dropdown-field will be provided in the interactive mode.(Needs to be categorial. Horizontal violinplots are not possible.)
#'
#' @return A list with 1. the plotly object & 2. the data frame used in the plot
#' @export
.violin_plot <- function(x, y){
df <- data.frame(x, y)
# plotly
p <- plotly::plot_ly(df,
x = df[[1]],
y = df[[2]],
split = df[[1]],
type = 'violin',
box = list(
visible = T
),
meanline = list(
visible = T
)
)
p <- plotly::layout(p,
xaxis = list(title = names(df[1])),
yaxis = list(title = names(df[2])),
zeroline = F)
return(list("plot" = p, "df" = df))
}
......@@ -11,30 +11,24 @@ is_shiny <- identical(knitr::opts_knit$get("rmarkdown.runtime"), "shiny")
```{r, eval=!is_shiny}
x_value <- {{ env_id }}$x[[1]]
x_title <- names({{ env_id }}$x)
y_value <- {{ env_id }}$y[[1]]
y_title <- names({{ env_id }}$y)
p <- plotly::plot_ly(data.frame(x_value, y_value),
x = x_value,
y = y_value,
split = x_value,
type = 'violin',
box = list(
visible = T
),
meanline = list(
visible = T
)
)
p <- plotly::layout(p,
xaxis = list(title = x_title),
yaxis = list(title = y_title,
zeroline = F))
p
# set variables
x <- {{ env_id }}$x[1]
y <- {{ env_id }}$y[1]
# x_title <- names({{ env_id }}$x)
# y_title <- names({{ env_id }}$y)
# .bar_plot function
lp <- i2dash.scrnaseq::.violin_plot(x = x, y = y)
df <- lp$df
# download_link
i2dash::embed_var(df)
# show plot
p <- lp$p
p
```
```{r, eval=is_shiny}
......
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/visualization_functions.R
\name{.violin_plot}
\alias{.violin_plot}
\title{Render a box plot}
\usage{
.violin_plot(x, y)
}
\arguments{
\item{x}{A list with the x-axis values. If it is a nested list, a dropdown-field will be provided in the interactive mode.}
\item{y}{A list with the y-axis values. If it is a nested list, a dropdown-field will be provided in the interactive mode.(Needs to be categorial. Horizontal violinplots are not possible.)}
}
\value{
A list with 1. the plotly object & 2. the data frame used in the plot
}
\description{
Render a box plot
}
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