Skip to content
Permalink
09c2934959
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
52 lines (50 sloc) 1.71 KB
#' Plot graphs
#' @description Plot different charts.
#' @param data Input data e.g. data frame
#' @param type Type of graph e.g. ("boxplot", "violinplot", "jitterplot", "barplot")
#' @param x Values for x-axis, e.g. values of a column to be passed to aes(x = ...) of \link{ggplot2}
#' @param y Values for y-axis, values of a column to be passed to aes(y = ...) of \link{ggplot2}
#' @param geom_plot_args A list of rguments to \link{ggplot2}
#' @import ggplot2
#' @import data.table
#' @examples
#' brainCalculatedMetadata <- calculate.metadata(metadata, brainRows)
#' @return
plot.graph <- function(data, type = "jitter", x, y, geom_plot_args = list()) {
#check if data is a data table
if (!is.data.table(data)) data <- data.table(data)
if(type == "boxplot") {
ggplot(data, aes(x = data[[x]], y = data[[y]])) +
do.call("geom_boxplot", geom_plot_args) +
xlab(x) +
ylab(y)
}
else if(type == "violinplot") {
ggplot(data, aes(x = data[[x]], y = data[[y]])) +
do.call("geom_violin", geom_plot_args) +
geom_jitter(size = 0.4, width = 0.15) +
stat_summary(fun.data = summary.violin, color = "black") +
xlab(x) +
ylab(y)
}
else if(type == "jitter") {
ggplot(data, aes(x = data[[x]], y = data[[y]])) +
do.call("geom_jitter", geom_plot_args) +
xlab(x) +
ylab(y)
}
else if(type == "barplot") {
ggplot(data, aes(x = data[[x]])) +
do.call("geom_bar", geom_plot_args) +
xlab(x) +
ylab(y)
}
}
#' Calculation of median + range
#' @param x Input from ggplot
summary.violin <- function(x) {
mu <- mean(x)
sigma1 <- mu-sd(x)
sigma2 <- mu+sd(x)
return(c(y=mu, ymin=sigma1, ymax=sigma2))
}