geom_point() warning with negative position_dodge width

When I use geom_plot() with position_dodge() for the position argument, I get a warning position_dodge requires non-overlapping x intervals if I use a negative value for width in position_dodge().
my intention for using a negative width value was to display the grouping variable in reversed order.

is there a way to avoid this warning (maybe a different approach to reoder the grouping variable?)?

see the following reprex

library(tidyverse)

df <- tribble(~dimension, ~group, ~value,
              "dim 1", "A", 0.5,
              "dim 1", "B", 0.75,
              "dim 2", "A", 0.4,
              "dim 2", "B", 0.4)

plot <- ggplot(data = df,
       mapping = aes(x = dimension,
                     y = value,
                     color = group)) 

# no warning
plot +  
 geom_point(size = 2, 
            position = position_dodge(width = 0.5))


# warning
plot +  
  geom_point(size = 2, 
             position = position_dodge(width = -0.5))
#> Warning: position_dodge requires non-overlapping x intervals

Created on 2021-01-15 by the reprex package (v0.3.0)

I found a solution my self.
Instead of using the a negative value for the width argument in position_dodge() to reverse the order, one could reorder the factor passed to the colorargument in aes() for example with fct_rev().

library(tidyverse)

df <- tribble(~dimension, ~group, ~value,
              "dim 1", "A", 0.5,
              "dim 1", "B", 0.75,
              "dim 2", "A", 0.4,
              "dim 2", "B", 0.4)

plot <- ggplot(data = df,
               mapping = aes(x = dimension,
                             y = value,
                             color = fct_rev(group))) 

plot +  
    geom_point(size = 2, 
               position = position_dodge(width = 0.5))

If desired, you can define a new colour for the legend.

plot +  
    geom_point(size = 2, 
               position = position_dodge(width = 0.5)) +
    guides(color = guide_legend(
        title = "group",
        reverse = TRUE)) 

(disclaimer: from a visualisation point of view, this may not make sense. The reprex is only meant to show how it is done technically.)

Reading the warning for negative width values in position_dodge(), I drew attention to this in a Github issue (https://github.com/tidyverse/ggplot2/issues/3610#issuecomment-767480561)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.