R/plot_barcode_tree.R

Defines functions plot_barcode_tree

Documented in plot_barcode_tree

#' Plot for barcode-tree
#'
#' Generate a plot created with ggplot2 to represent the evolution of connected components along threshold.
#'
#' @param components data frame; output from barcode_tree function
#' @param x_variable string defining the x axis of the plot. This dimension is reserved for the threshold.
#' @param y_varaible string defining the y axis of the plot. This dimension is reserved for the id of connected components.
#' @param group_varaible stirng defining the grouping variable of the data elements; \code{id} by default
#' @param size number; size of the line.
#' @param alpha number; opacity of the line.
#'
#' @author Daniel Alcaide, \email{daniel.alcaide@@esat.kuleuven.be}
#' @references Alcaide D, Aerts J. (2018) MCLEAN: Multilevel Clustering Exploration As Network. PeerJ Computer Science 4:e145 \url{https://doi.org/10.7717/peerj-cs.145}
#'
#' @importFrom ggplot2 ggplot
#' @importFrom ggplot2 geom_line
#' @importFrom ggplot2 theme_bw
#' @importFrom ggplot2 scale_x_continuous
#' @importFrom ggplot2 labs
#' @importFrom ggplot2 xlab
#' @importFrom ggplot2 summarise
#' @importFrom ggplot2 theme
#' @importFrom ggplot2 aes_string
#' @importFrom ggplot2 waiver
#' @importFrom ggplot2 theme_bw
#' @importFrom ggplot2 element_blank
#' @importFrom ggplot2 element_rect
#' @importFrom ggplot2 element_text
#'
#' @examples
#' data("synthetic_distances")
#' barcode_tree = barcode_tree( distance_matrix = synthetic_distances, sequence = seq(from=100,to=300,by=25))
#' plot_barcode_tree(barcode_tree)
#'
#' @export
plot_barcode_tree = function(components,
                             x_variable ="threshold",
                             y_varaible = "plot_components",
                             group_varaible = "id",
                             size = .15,
                             alpha = .7) {

  x_scale = c(min(components[x_variable]), max(components[x_variable]))
  # ---- Define breaks ----
  x_breaks = as.numeric(as.matrix(unique(components[x_variable]))[,1])

  ggplot2::ggplot(components, ggplot2::aes_string(x=x_variable, y=y_varaible, group=group_varaible)) +
    ggplot2::geom_line(size = size, alpha = alpha) +
    ggplot2::theme_bw() +
    ggplot2::scale_x_continuous(name = ggplot2::waiver(), breaks = x_breaks) +
    ggplot2::labs(title = "Connected components by threshold") +
    ggplot2::xlab("Threshold") +
    ggplot2::ylab("Connected components") +
    ggplot2::theme(panel.grid.minor = ggplot2::element_blank(),
          panel.grid.major.y = ggplot2::element_blank(),
          legend.position="none",
          axis.text.y = ggplot2::element_blank(),
          axis.ticks.y = ggplot2::element_blank(),
          text = ggplot2::element_text(size = 14),
          panel.background = ggplot2::element_rect(fill = "white", colour = "black"),
          axis.text.x = ggplot2::element_text(angle = 90, hjust = 1))

}
danielalcaide/mclean documentation built on May 28, 2019, 7:51 p.m.