Skip to content

Aligning geoms with preserve = "single" appears impossible #2712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Ax3man opened this issue Jun 24, 2018 · 3 comments
Closed

Aligning geoms with preserve = "single" appears impossible #2712

Ax3man opened this issue Jun 24, 2018 · 3 comments

Comments

@Ax3man
Copy link
Contributor

Ax3man commented Jun 24, 2018

I want to plot a geom without with together with a geom that does have width, e.g. points and boxplots.

Not all factor combinations are present.

The default of adjusting the width (preserve = 'total') works fine. But when maintaining the box widths with preserve = 'single' I can no longer do the same for the points.

library(ggplot2)

ggplot(mtcars, aes(factor(gear), mpg, fill = factor(am))) + 
  geom_boxplot(position = position_dodge(preserve = 'total')) +
  geom_point(position = position_dodge(0.75, preserve = 'total'))

ggplot(mtcars, aes(factor(gear), mpg, fill = factor(am))) + 
  geom_boxplot(position = position_dodge(preserve = 'single')) +
  geom_point(position = position_dodge(0.75, preserve = 'total'))

ggplot(mtcars, aes(factor(gear), mpg, fill = factor(am))) + 
  geom_boxplot(position = position_dodge(preserve = 'single')) +
  geom_point(position = position_dodge(0.75, preserve = 'single'))
#> Warning in max(table(panel$xmin)): no non-missing arguments to max;
#> returning -Inf

Created on 2018-06-24 by the reprex package (v0.2.0).

@batpigandme
Copy link
Contributor

I don't think this (below) is what you're going for, but you might want to try playing around with position_dodge2().

library(ggplot2)

ggplot(mtcars, aes(factor(gear), mpg, fill = factor(am))) + 
  geom_boxplot(position = position_dodge2(0.75, preserve = 'single')) +
  geom_point(position = position_dodge2(0.75, preserve = 'single'))

Created on 2018-06-24 by the reprex package (v0.2.0).

@Ax3man
Copy link
Contributor Author

Ax3man commented Jun 25, 2018

Yes ok, this solves it:

ggplot(mtcars, aes(factor(gear), mpg, fill = factor(am))) + 
  geom_boxplot(position = position_dodge2(0.75, preserve = 'single')) +
  geom_point(position = position_dodge(0.75, preserve = 'total'))

rplot08

I didn't expect that one needs to pass different positions to the two geoms for them to line up, but I guess it makes sense to always use dodge2 for geoms that have width and dodge for geoms that don't.

@Ax3man Ax3man closed this as completed Jun 25, 2018
@lock
Copy link

lock bot commented Dec 22, 2018

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Dec 22, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants