visualization_functions.R 5 KB
Newer Older
1
#' Render a scatter plot
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#'
#' @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)
}
arsenij.ustjanzew's avatar
arsenij.ustjanzew committed
79

arsenij.ustjanzew's avatar
arsenij.ustjanzew committed
80
#' Function to create a dataframe for plotly_barplot.
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

#' @param group_by A list with factorial values, by which observations can optionally be grouped.
#' @param x (Optional) A named list with the observations for the barplot.
#'
#' @return An object of class \code{list} containig the dataframe 'df', the vector 'x' with values for the x-axis, the vector 'y' with values for the y-axis, the vector 'names' (can be NULL), the boolean value 'showlegend'.
#' @export
create_barplot_df <- function(group_by, x = NULL){
  if(is.null(x)){
    tab <- table(group_by)
    df <- as.data.frame(tab)
    x <- df[2]
    y <- df[1]
    names <- NULL
    showlegend <- F
    return(list("df" = df, "x" = x, "y" = y, "names" = names, "showlegend" = showlegend))
  } else {
    tab <- table(group_by, x)
    ptab <- prop.table(tab, margin = 1)
    df <- as.data.frame(ptab)
    x <- df[3]
    y <- df[1]
    names <- df[2]
    showlegend <- T
    return(list("df" = df, "x" = x, "y" = y, "names" = names, "showlegend" = showlegend))
  }
}

108
#' Render a bar plot with plotly.
arsenij.ustjanzew's avatar
arsenij.ustjanzew committed
109
#'
110
#' @param ... these arguments are of either the form value or tag = value and should be valid for the 'plotly::plot_ly()' method.
arsenij.ustjanzew's avatar
arsenij.ustjanzew committed
111
112
113
#' @param showlegend (Optional) Boolean value that describes if the legend should be shown.
#' @param title_x (Optional) A title that describes the observations.
#' @param title_group_by (Optional) A title that describes the grouping factor.
arsenij.ustjanzew's avatar
arsenij.ustjanzew committed
114
#'
115
#' @return An object of class \code{plotly}.
arsenij.ustjanzew's avatar
arsenij.ustjanzew committed
116
#' @export
117
118
plotly_barplot <- function(..., showlegend = NULL, title_x = NULL, title_group_by = NULL){
  p <- plotly::plot_ly(..., type = "bar", orientation = "h", opacity = 0.7) %>%
119
120
121
122
123
    plotly::layout(xaxis = list(title = title_x, showline = T),
                   yaxis = list(title = title_group_by, showline = T, showticklabels = T),
                   barmode = 'stack',
                   showlegend = showlegend)
  p
arsenij.ustjanzew's avatar
arsenij.ustjanzew committed
124
}
125

126
#' Render a box plot with plotly.
127
#'
128
129
130
131
#' @param x Numeric observations for the boxplot.
#' @param group_by A factor, by which observations can optionally be grouped.
#' @param title A title that describes the observations.
#' @param group_by_title A title that describes the grouping factor.
132
#'
133
#' @return An object of class \code{plotly}.
134
#' @export
135
136
137
138
139
plotly_boxplot <- function(x, group_by = NULL, title = "", group_by_title = NULL){
  plotly::plot_ly(x = x, y = group_by, type = "box", name = title) %>%
    plotly::layout(xaxis = list(title = title, showline = T),
                   yaxis = list(title = group_by_title, showline = T, showticklabels = T),
                   showlegend = T)
140
}