From d7e75e6129f4c22bfdeabd9735f6a0447494ef75 Mon Sep 17 00:00:00 2001 From: frostell Date: Wed, 14 Mar 2018 01:12:15 +0100 Subject: [PATCH 1/2] small update to position-dodge2.r --- R/position-dodge2.r | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/R/position-dodge2.r b/R/position-dodge2.r index 59c1de42dd..4df81e81e2 100644 --- a/R/position-dodge2.r +++ b/R/position-dodge2.r @@ -69,7 +69,14 @@ pos_dodge2 <- function(df, width, n = NULL, padding = 0.1) { df$xmin <- df$x df$xmax <- df$x } - + + # to enable functionality when input is not a stat, save copy and reduce df + df_full <- df + if (!all(c("middle") %in% names(df))) { + df <- df[!is.na(df$y), ] + df <- df[!duplicated(df$group), ] + } + # xid represents groups of boxes that share the same position df$xid <- find_x_overlaps(df) @@ -85,7 +92,7 @@ pos_dodge2 <- function(df, width, n = NULL, padding = 0.1) { n <- table(df$xid) df$new_width <- (df$xmax - df$xmin) / as.numeric(n[df$xid]) } else { - df$new_width <- (df$xmax - df$xmin) / n + df$new_width <- (df$xmax - df$xmin) / max(table(df$xid)) } df$xmin <- df$x - (df$new_width / 2) @@ -111,6 +118,20 @@ pos_dodge2 <- function(df, width, n = NULL, padding = 0.1) { # x values get moved to between xmin and xmax df$x <- (df$xmin + df$xmax) / 2 + # now that x-positions have been found based on groups, df_full can be populated + df_full$xid <- NA + df_full$newx <- NA + df_full$new_width <- NA + + for(group in df$group){ + df_full$x[df_full$group==group] <- df$x[df$group==group] + df_full$xid[df_full$group==group] <- df$xid[df$group==group] + df_full$newx[df_full$group==group] <- df$newx[df$group==group] + df_full$new_width[df_full$group==group] <- df$new_width[df$group==group] + } + + df <- df_full + # If no elements occupy the same position, there is no need to add padding if (!any(duplicated(df$xid))) { return(df) From febc6909657cb5f29352558c484bcc7ae6532daf Mon Sep 17 00:00:00 2001 From: frostell Date: Wed, 14 Mar 2018 11:58:51 +0100 Subject: [PATCH 2/2] position-dodge2 update --- R/position-dodge2.r | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/position-dodge2.r b/R/position-dodge2.r index 4df81e81e2..7a29e0d53b 100644 --- a/R/position-dodge2.r +++ b/R/position-dodge2.r @@ -69,10 +69,10 @@ pos_dodge2 <- function(df, width, n = NULL, padding = 0.1) { df$xmin <- df$x df$xmax <- df$x } - + # to enable functionality when input is not a stat, save copy and reduce df df_full <- df - if (!all(c("middle") %in% names(df))) { + if (!all(c("ymin") %in% names(df))) { df <- df[!is.na(df$y), ] df <- df[!duplicated(df$group), ] }