diff --git a/instat/static/InstatObject/R/data_object_R6.R b/instat/static/InstatObject/R/data_object_R6.R index c1fc22afc1b..f9602d7477b 100644 --- a/instat/static/InstatObject/R/data_object_R6.R +++ b/instat/static/InstatObject/R/data_object_R6.R @@ -1531,25 +1531,36 @@ data_object$set("public", "remove_column_colours", function() { } ) -data_object$set("public","graph_one_variable", function(columns, numeric = "geom_boxplot", categorical = "geom_bar", output = "facets", free_scale_axis = FALSE, ncol = NULL, polar = FALSE,...) { - if(!all(columns %in% self$get_column_names())) stop("Not all columns found in the data") - if(!output %in% c("facets", "combine", "single")) stop("output must be one of: facets, combine or single") - numeric_geom <- match.fun(numeric) +data_object$set("public", "graph_one_variable", function(columns, numeric = "geom_boxplot", categorical = "geom_bar", output = "facets", free_scale_axis = FALSE, ncol = NULL, polar = FALSE, ...) { + if(!all(columns %in% self$get_column_names())) { + stop("Not all columns found in the data") + } + if(!output %in% c("facets", "combine", "single")) { + stop("output must be one of: facets, combine or single") + } + if(!numeric %in% c("box_jitter", "violin_jitter", "violin_box")) { + numeric_geom <- match.fun(numeric) + } + else { + numeric_geom <- numeric + } cat_geom <- match.fun(categorical) - curr_data <- self$get_data_frame() column_types <- c() for(col in columns) { # TODO this could be method to avoid needing to get full data frame in this method # Everything non numeric is treated as categorical - if(is.numeric(curr_data[[col]])) column_types <- c(column_types, "numeric") - else column_types <- c(column_types, "cat") + if(is.numeric(curr_data[[col]])) { + column_types <- c(column_types, "numeric") + } + else { + column_types <- c(column_types, "cat") + } } - if(output == "facets") { column_types <- unique(column_types) if(length(column_types) > 1) { - if(output == "facets") warning("Cannot do facets with graphs of different types. Combine graphs will be used instead.") + warning("Cannot do facets with graphs of different types. Combine graphs will be used instead.") output <- "combine" } } @@ -1562,20 +1573,37 @@ data_object$set("public","graph_one_variable", function(columns, numeric = "geom curr_geom <- cat_geom curr_geom_name <- categorical } - else stop("Cannot plot columns of type:", column_types[i]) - - curr_data <- self$get_data_frame(stack_data = TRUE, measure.vars=columns) - if(curr_geom_name == "geom_boxplot" || curr_geom_name == "geom_point") { - g <- ggplot(data = curr_data, mapping = aes(x = "", y=value)) + else { + stop("Cannot plot columns of type:", column_types[i]) + } + curr_data <- self$get_data_frame(stack_data = TRUE, measure.vars = columns) + if(curr_geom_name == "geom_boxplot" || curr_geom_name == "geom_point" || curr_geom_name == "geom_jitter" || curr_geom_name == "box_jitter" || curr_geom_name == "violin_jitter" || curr_geom_name == "violin_box") { + g <- ggplot(data = curr_data, mapping = aes(x = "", y = value)) + xlab("") } else { - g <- ggplot(data = curr_data, mapping = aes(x = value)) + g <- ggplot(data = curr_data, mapping = aes(x = value)) + ylab("") + if(curr_geom_name == "box_jitter") { + g <- g + geom_boxplot() + geom_jitter() + } + else if(curr_geom_name == "violin_jitter") { + g <- g + geom_violin() + geom_jitter() + } + else if(curr_geom_name == "violin_box") { + g <- g + geom_violin() + geom_boxplot() + } + } + + if(free_scale_axis) { + g <- g + facet_wrap(facets = ~ variable, scales = "free", ncol = ncol) + } + else { + g <- g + facet_wrap(facets = ~ variable, scales = "free_x", ncol = ncol) } - if(free_scale_axis) return(g + curr_geom() + facet_wrap(facets= ~variable, scales = "free", ncol = ncol) + ylab("")) - else return(g + curr_geom() + facet_wrap(facets= ~variable, scales = "free_x", ncol = ncol) + ylab("")) - if(polar) g <- g + coord_polar(theta = "x") - return(g) + if(polar) { + g <- g + coord_polar(theta = "x") + } + return(g) } else { graphs <- list() @@ -1589,23 +1617,39 @@ data_object$set("public","graph_one_variable", function(columns, numeric = "geom curr_geom <- cat_geom curr_geom_name <- categorical } - else stop("Cannot plot columns of type:", column_types[i]) - - if(curr_geom_name == "geom_boxplot" || curr_geom_name == "geom_point") { - g <- ggplot(data = curr_data, mapping = aes_(x = "", y = as.name(column))) + else { + stop("Cannot plot columns of type:", column_types[i]) + } + if(curr_geom_name == "geom_boxplot" || curr_geom_name == "geom_point" || curr_geom_name == "box_jitter" || curr_geom_name == "violin_jitter" || curr_geom_name == "violin_box") { + g <- ggplot(data = curr_data, mapping = aes_(x = "", y = as.name(column))) + xlab("") + } + else { + g <- ggplot(data = curr_data, mapping = aes_(x = as.name(column))) + ylab("") + } + if(curr_geom_name == "box_jitter") { + g <- g + geom_boxplot() + geom_jitter() + } + else if(curr_geom_name == "violin_jitter") { + g <- g + geom_violin() + geom_jitter() + } + else if(curr_geom_name == "violin_box") { + g <- g + geom_violin() + geom_boxplot() } else { - g <- ggplot(data = curr_data, mapping = aes_(x = as.name(column))) + g <- g + curr_geom() } - current_graph <- g + curr_geom() - if(polar && column_types[i] == "cat") current_graph <- current_graph + coord_polar(theta = "x") - graphs[[i]] <- current_graph + if(polar && column_types[i] == "cat") { + g <- g + coord_polar(theta = "x") + } + graphs[[i]] <- g i = i + 1 } if(output == "combine") { return(gridExtra::grid.arrange(grobs = graphs, ncol = ncol)) } - else return(graphs) + else { + return(graphs) + } } } )