R/raincloud_2x3_repmes.R

Defines functions raincloud_2x3_repmes

Documented in raincloud_2x3_repmes

#' 2 x 3 repeated measures raincloud
#'
#' This function visualizes a 2x2 repeated measures raincloud.
#' @name raincloud_2x3_repmes
#' @param data_2x2 <data.frame> the array of datapoints to be plotted
#' @param size <numeric> data size
#' @param alpha <numeric> data alpha
#' @importFrom ggplot2 ggplot aes geom_point geom_line position_nudge scale_x_continuous theme_classic ggsave
#' @importFrom gghalves geom_half_boxplot geom_half_violin
#' @importFrom magrittr %>%
#' @importFrom dplyr group_by mutate
#' @return figure_2x2x2
#'
#'
#' @examples
#' \dontrun{
#' # Using an example dataset
#' raincloud_2x3_repmes <- function(data_2x2,
#'                                  size = 1.5,
#'                                  alpha = .6,
#'                                  ort = 'v')
#'}
#'@export

globalVariables(c("ggplot", "aes", "geom_point", "geom_line",
                  "geom_half_boxplot", "geom_half_violin",
                  "position_nudge", "%>%", "position"))

raincloud_2x3_repmes <- function(data_2x2,
                                 size = 1.5,
                                 alpha = .6,
                                 ort = 'v') {
if (ort == 'v') {
  figure_2x2x2 <- ggplot(data = data_2x2) +

      #Add geom_() objects
      geom_point(data = data_2x2 %>% dplyr::filter(x_axis =="1"), aes(x = jit, y = y_axis, color = group, fill = group), size = size,
                 alpha = alpha) +
      geom_point(data = data_2x2 %>% dplyr::filter(x_axis =="1.01"), aes(x = jit, y = y_axis, color = group, fill = group), size = size,
                 alpha = alpha) +
      geom_point(data = data_2x2 %>% dplyr::filter(x_axis =="2"), aes(x = jit, y = y_axis, color = group, fill = group), size = size,
                 alpha = alpha) +
      geom_point(data = data_2x2 %>% dplyr::filter(x_axis =="2.01"), aes(x = jit, y = y_axis, color = group, fill = group), size = size,
                 alpha = alpha) +
      geom_point(data = data_2x2 %>% dplyr::filter(x_axis =="3"), aes(x = jit, y = y_axis, color = group, fill = group), size = size,
                 alpha = alpha) +
      geom_point(data = data_2x2 %>% dplyr::filter(x_axis =="3.01"), aes(x = jit, y = y_axis, color = group, fill = group), size = size,
                 alpha = alpha) +

      #geom_line(aes(x=jit, y = y_axis, group=id), color = 'lightgray') +
      #geom_line(data = data_2x2 %>% dplyr::filter(x_axis %in% c("1", "2")), aes(x = jit, y = y_axis, group = id), color = line_color, alpha = alpha) +
      #geom_line(data = data_2x2 %>% dplyr::filter(x_axis %in% c("3", "4")), aes(x = jit, y = y_axis, group = id), color = line_color, alpha = alpha) +

      geom_half_boxplot(
        data = data_2x2 %>% dplyr::filter(x_axis=="1"), aes(x=x_axis, y = y_axis, color = group, fill = group), position = position_nudge(x = .2),
        side = "r",outlier.shape = NA, center = TRUE, errorbar.draw = FALSE, width = .2, alpha = alpha) +

      geom_half_boxplot(
        data = data_2x2 %>% dplyr::filter(x_axis=="1.01"), aes(x=x_axis, y = y_axis, color = group, fill = group), position = position_nudge(x = .3),
        side = "r",outlier.shape = NA, center = TRUE, errorbar.draw = FALSE, width = .2, alpha = alpha) +

      geom_half_boxplot(
        data = data_2x2 %>% dplyr::filter(x_axis=="2"), aes(x=x_axis, y = y_axis, color = group, fill = group), position = position_nudge(x = .2),
        side = "r",outlier.shape = NA, center = TRUE, errorbar.draw = FALSE, width = .2, alpha = alpha) +

      geom_half_boxplot(
        data = data_2x2 %>% dplyr::filter(x_axis=="2.01"), aes(x=x_axis, y = y_axis, color = group, fill = group), position = position_nudge(x = .3),
        side = "r",outlier.shape = NA, center = TRUE, errorbar.draw = FALSE, width = .2, alpha = alpha) +

    geom_half_boxplot(
      data = data_2x2 %>% dplyr::filter(x_axis=="3"), aes(x=x_axis, y = y_axis, color = group, fill = group), position = position_nudge(x = .2),
      side = "r",outlier.shape = NA, center = TRUE, errorbar.draw = FALSE, width = .2, alpha = alpha) +

    geom_half_boxplot(
      data = data_2x2 %>% dplyr::filter(x_axis=="3.01"), aes(x=x_axis, y = y_axis, color = group, fill = group), position = position_nudge(x = .3),
      side = "r",outlier.shape = NA, center = TRUE, errorbar.draw = FALSE, width = .2, alpha = alpha) +


      geom_half_violin(
        data = data_2x2 %>% dplyr::filter(x_axis=="1"),aes(x = x_axis, y = y_axis, color = group, fill = group), position = position_nudge(x = .45),
        side = "r", alpha = alpha) +

      geom_half_violin(
        data = data_2x2 %>% dplyr::filter(x_axis=="1.01"),aes(x = x_axis, y = y_axis, color = group, fill = group), position = position_nudge(x = .45),
        side = "r", alpha = alpha) +

      geom_half_violin(
        data = data_2x2 %>% dplyr::filter(x_axis=="2"),aes(x = x_axis, y = y_axis, color = group, fill = group), position = position_nudge(x = .45),
        side = "r", alpha = alpha) +

      geom_half_violin(
        data = data_2x2 %>% dplyr::filter(x_axis=="2.01"),aes(x = x_axis, y = y_axis, color = group, fill = group), position = position_nudge(x = .45),
        side = "r", alpha = alpha) +

     geom_half_violin(
      data = data_2x2 %>% dplyr::filter(x_axis=="3"),aes(x = x_axis, y = y_axis, color = group, fill = group), position = position_nudge(x = .45),
      side = "r", alpha = alpha) +

     geom_half_violin(
      data = data_2x2 %>% dplyr::filter(x_axis=="3.01"),aes(x = x_axis, y = y_axis, color = group, fill = group), position = position_nudge(x = .45),
      side = "r", alpha = alpha)

} else if (ort == 'h') {

  figure_2x2x2 <- ggplot(data = data_2x2) +

    #Add geom_() objects
    geom_point(data = data_2x2 %>% dplyr::filter(x_axis =="1"), aes(x = jit, y = y_axis, color = group, fill = group), size = size,
               alpha = alpha) +
    geom_point(data = data_2x2 %>% dplyr::filter(x_axis =="1.01"), aes(x = jit, y = y_axis, color = group, fill = group), size = size,
               alpha = alpha) +
    geom_point(data = data_2x2 %>% dplyr::filter(x_axis =="2"), aes(x = jit, y = y_axis, color = group, fill = group), size = size,
               alpha = alpha) +
    geom_point(data = data_2x2 %>% dplyr::filter(x_axis =="2.01"), aes(x = jit, y = y_axis, color = group, fill = group), size = size,
               alpha = alpha) +
    geom_point(data = data_2x2 %>% dplyr::filter(x_axis =="3"), aes(x = jit, y = y_axis, color = group, fill = group), size = size,
               alpha = alpha) +
    geom_point(data = data_2x2 %>% dplyr::filter(x_axis =="3.01"), aes(x = jit, y = y_axis, color = group, fill = group), size = size,
               alpha = alpha) +

    #geom_line(aes(x=jit, y = y_axis, group=id), color = 'lightgray') +
    #geom_line(data = data_2x2 %>% dplyr::filter(x_axis %in% c("1", "2")), aes(x = jit, y = y_axis, group = id), color = line_color, alpha = alpha) +
    #geom_line(data = data_2x2 %>% dplyr::filter(x_axis %in% c("3", "4")), aes(x = jit, y = y_axis, group = id), color = line_color, alpha = alpha) +

    geom_half_boxplot(
      data = data_2x2 %>% dplyr::filter(x_axis=="1"), aes(x=x_axis, y = y_axis, color = group, fill = group), position = position_nudge(x = .2),
      side = "r",outlier.shape = NA, center = TRUE, errorbar.draw = FALSE, width = .2, alpha = alpha) +

    geom_half_boxplot(
      data = data_2x2 %>% dplyr::filter(x_axis=="1.01"), aes(x=x_axis, y = y_axis, color = group, fill = group), position = position_nudge(x = .3),
      side = "r",outlier.shape = NA, center = TRUE, errorbar.draw = FALSE, width = .2, alpha = alpha) +

    geom_half_boxplot(
      data = data_2x2 %>% dplyr::filter(x_axis=="2"), aes(x=x_axis, y = y_axis, color = group, fill = group), position = position_nudge(x = .2),
      side = "r",outlier.shape = NA, center = TRUE, errorbar.draw = FALSE, width = .2, alpha = alpha) +

    geom_half_boxplot(
      data = data_2x2 %>% dplyr::filter(x_axis=="2.01"), aes(x=x_axis, y = y_axis, color = group, fill = group), position = position_nudge(x = .3),
      side = "r",outlier.shape = NA, center = TRUE, errorbar.draw = FALSE, width = .2, alpha = alpha) +

    geom_half_boxplot(
      data = data_2x2 %>% dplyr::filter(x_axis=="3"), aes(x=x_axis, y = y_axis, color = group, fill = group), position = position_nudge(x = .2),
      side = "r",outlier.shape = NA, center = TRUE, errorbar.draw = FALSE, width = .2, alpha = alpha) +

    geom_half_boxplot(
      data = data_2x2 %>% dplyr::filter(x_axis=="3.01"), aes(x=x_axis, y = y_axis, color = group, fill = group), position = position_nudge(x = .3),
      side = "r",outlier.shape = NA, center = TRUE, errorbar.draw = FALSE, width = .2, alpha = alpha) +


    geom_half_violin(
      data = data_2x2 %>% dplyr::filter(x_axis=="1"),aes(x = x_axis, y = y_axis, color = group, fill = group), position = position_nudge(x = .45),
      side = "r", alpha = alpha) +

    geom_half_violin(
      data = data_2x2 %>% dplyr::filter(x_axis=="1.01"),aes(x = x_axis, y = y_axis, color = group, fill = group), position = position_nudge(x = .45),
      side = "r", alpha = alpha) +

    geom_half_violin(
      data = data_2x2 %>% dplyr::filter(x_axis=="2"),aes(x = x_axis, y = y_axis, color = group, fill = group), position = position_nudge(x = .45),
      side = "r", alpha = alpha) +

    geom_half_violin(
      data = data_2x2 %>% dplyr::filter(x_axis=="2.01"),aes(x = x_axis, y = y_axis, color = group, fill = group), position = position_nudge(x = .45),
      side = "r", alpha = alpha) +

    geom_half_violin(
      data = data_2x2 %>% dplyr::filter(x_axis=="3"),aes(x = x_axis, y = y_axis, color = group, fill = group), position = position_nudge(x = .45),
      side = "r", alpha = alpha) +

    geom_half_violin(
      data = data_2x2 %>% dplyr::filter(x_axis=="3.01"),aes(x = x_axis, y = y_axis, color = group, fill = group), position = position_nudge(x = .45),
      side = "r", alpha = alpha) +

    coord_flip()

}

    return(figure_2x2x2)
}
jorvlan/openvis documentation built on Dec. 6, 2020, 3:06 a.m.