Skip to content

Commit cdcde91

Browse files
committed
Transforming the functionality to use only base R
1 parent fa2db1e commit cdcde91

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

R/position-dodge.R

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,37 @@ pos_dodge <- function(df, width, n = NULL, stackOverlap = "no") {
178178
df$xmax <- df$x + d_width / n / 2
179179

180180
if (stackOverlap == "byExtent") {
181-
tmp = df %>% group_by(group) %>% mutate(ymaxx = cumsum(ymax)) %>% mutate(ymin = ymaxx-ymax, ymax = ymaxx)
182-
df$ymin = tmp$ymin
183-
df$ymax = tmp$ymax
181+
# The code chunk below is just to implement the following line without tidyverse functions, as ggplot2 can be imported without that
182+
# df %>% group_by(group) %>% mutate(ymaxx = cumsum(ymax)) %>% mutate(ymin = ymaxx-ymax, ymax = ymaxx)
183+
184+
df$ymaxx = NA # Initialize the variable. This will store the desired top of the group
185+
groupIDs = unique(df$group) # Collect the unique groupIDs. Thi
186+
for (gid in groupIDs) {
187+
df$ymaxx[df$group == gid] = cumsum(df$ymax[df$group == gid])
188+
}
189+
# Create the new y placements
190+
df$ymin = df$ymaxx-df$ymax
191+
df$ymax = df$ymaxx
192+
193+
df$ymaxx = NULL # Remove the extra variable
194+
184195
} else if (stackOverlap == "byCenter") {
185-
tmp = df %>% group_by(group) %>% mutate(extent = ymax-ymin, ymaxx = cumsum((ymax+ymin)/2)) %>% mutate(ymin = ymaxx-extent/2, ymax = ymaxx+extent/2)
186-
df$ymin = tmp$ymin
187-
df$ymax = tmp$ymax
196+
# Similarly to above, the complicated code below is just to do the next line without tidyverse
197+
# df %>% group_by(group) %>% mutate(extent = ymax-ymin, ymaxx = cumsum((ymax+ymin)/2)) %>% mutate(ymin = ymaxx-extent/2, ymax = ymaxx+extent/2)
198+
199+
df$ymaxx = NA # Initialize the variable. This will store the desired top of the group
200+
df$extent = NA # Initialize the variable storing the extent of the geom
201+
groupIDs = unique(df$group) # Collect the unique groupIDs. Thi
202+
for (gid in groupIDs) {
203+
df$ymaxx[df$group == gid] = cumsum((df$ymax[df$group == gid] + df$ymin[df$group == gid])/2)
204+
}
205+
df$extent = df$ymax - df$ymin
206+
# Create the new y placements
207+
df$ymin = df$ymaxx-df$extent/2
208+
df$ymax = df$ymaxx+df$extent/2
209+
210+
df$ymaxx = NULL # Remove the extra variable
211+
df$extent = NULL # Remove the extra variable
188212
}
189213

190214
df

0 commit comments

Comments
 (0)