Skip to content

position_dodge2() should handle both point and interval geoms #2480

@frostell

Description

@frostell

Displaying raw data together with box plots can be useful.

If using both 'x' and 'fill' aesthetic geom_boxplot() handles NA's elegantly through the new position_dodge2(), but unfortunately geom_point() with a matching 'colour' aesthetic is misaligned with the boxes both when using position_dodge() and position_dodge2().

This problem only occurs when handling NA's, and position_dodge() and position_dodge2() gets it wrong in different ways:

library('tidyverse')

dat <- data.frame("value" = rnorm(n=30, mean=2, sd=0.5),
                  "group" = LETTERS[1:3],
                  "x" = factor(1:2))

dat$value[dat$group=="A"&dat$x=="1"] <- NA

ggplot(dat, aes(x=x, y=value)) +
  geom_boxplot(aes(fill=group), alpha=0.3) +
  geom_point(aes(colour=group), position=position_dodge(width=0.75), size=3, alpha=0.5)

ggplot(dat, aes(x=x, y=value)) +
  geom_boxplot(aes(fill=group), alpha=0.3) +
  geom_point(aes(colour=group), position=position_dodge2(width=0.75), size=3, alpha=0.5)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions