Skip to content

Commit

Permalink
Merge pull request #39 from africanmathsinitiative/master
Browse files Browse the repository at this point in the history
update
  • Loading branch information
FrancoisJRenaud authored Nov 23, 2016
2 parents 1a2a618 + 996af95 commit 2701bf8
Showing 1 changed file with 72 additions and 28 deletions.
100 changes: 72 additions & 28 deletions instat/static/InstatObject/R/data_object_R6.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Expand All @@ -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()
Expand All @@ -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)
}
}
}
)
Expand Down

0 comments on commit 2701bf8

Please sign in to comment.