Skip to content
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

preserve = "single" has no effect for continuous axes #2783

Closed
karawoo opened this issue Jul 27, 2018 · 2 comments
Closed

preserve = "single" has no effect for continuous axes #2783

karawoo opened this issue Jul 27, 2018 · 2 comments

Comments

@karawoo
Copy link
Member

karawoo commented Jul 27, 2018

#2774 made me realize that the preserve = "single" argument to position_dodge() and position_dodge2() has no effect when the x axis is continuous:

library("ggplot2")

df <- data.frame(
  x = c(4, 5, 6, 4, 6),
  y = c("A", "A", "A", "B", "B")
)

ggplot(df, aes(x = x)) +
  geom_bar(position = position_dodge2(preserve = "single")) +
  facet_wrap(~ y)

Compare to the version with a discrete x axis:

ggplot(df, aes(x = factor(x))) +
  geom_bar(position = position_dodge2(preserve = "single")) +
  facet_wrap(~ y)

Created on 2018-07-27 by the reprex package (v0.2.0).

Like @clauswilke said on the other issue, I'm not sure what the expectation should be for continuous axes, but maybe we should at least document that preserve only works for discrete axes.

@tungttnguyen
Copy link

Saw this on SO. Not sure if this is related. If not please move it wherever appropriate.

geom_text not properly positioned when using position_dodge(preserve=“single”) in bar plot
https://stackoverflow.com/q/51813898/786542

v1 <- LETTERS[1:2]
v2 <- letters[1:2]
v3 <- c("x","y")
g <- expand.grid(v1,v2,v3)
val=c(sample(10,8))
df<- data.frame(g,val)
df<- df[-8,]


    df %>% ggplot() +
      geom_bar(aes(x=Var2, y=val, fill=Var3, group=Var3), 
               stat="identity", 
               position=position_dodge(preserve="single"))+
      geom_text(aes(x=Var2, y=val+1, label=val, group=Var3), 
                position=position_dodge(width=1))+
  facet_grid(Var1~Var2, scale="free_x")

Using position_dodge2 alignes the bar with the labelling (however, the bar is the then centered and not aligned with the bars in the other facets).

df %>% ggplot() +
  geom_bar(aes(x=Var2, y=val, fill=Var3, group=Var3), stat="identity", 
           position=position_dodge2(preserve="single"))+
  geom_text(aes(x=Var2, y=val+1, label=val, group=Var3), 
            position=position_dodge2(width=1))+
  facet_grid(Var1~Var2, scale="free_x")

@teunbrand
Copy link
Collaborator

I think this has been fixed somewhere along the way, so I'm gonna mark this as completed.

library(ggplot2)

df <- data.frame(
  x = c(4, 5, 6, 4, 6),
  y = c("A", "A", "A", "B", "B")
)

ggplot(df, aes(x = x)) +
  geom_bar(position = position_dodge2(preserve = "single")) +
  facet_wrap(~ y)

Created on 2023-06-25 with reprex v2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants