tidyverse/ggplot2

Automatically fill in x for univariate boxplot

Closed this issue · 12 comments

i.e. make this work

ggplot(mtcars, aes(y = disp)) + geom_boxplot()

Presumably the output would look the same as if x was the same for all y then?

ggplot(mtcars, aes(x = '1', y = disp)) + geom_boxplot()

Ideally, the output should not be exactly like this:

library(ggplot2)
ggplot(mtcars, aes(x = "1", y = disp)) + geom_boxplot()

There is no reason for the labeling of the x-axis in such a plot (neither the x nor the 1 should be there.)

I like the simplified syntax but now the x axis has a numeric scale, which seems unnecessary and potentially confusing. Could there be no labelling as @rpruim suggests? ggplot(mtcars, aes(x = "", y = disp)) + geom_boxplot() almost does this apart from the x label.

ggplot(mtcars, aes(y = disp)) + geom_boxplot()

image

So we're back to ggplot(mtcars, aes(y = disp, x = factor(""))) + geom_boxplot() + theme(axis.ticks.x = element_blank())

No need for the x aesthetic now (and better to omit it):

library(ggplot2)
ggplot(mtcars, aes(y = disp)) + geom_boxplot() + 
  theme(axis.ticks.x = element_blank(), axis.text.x = element_blank())

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

I suppose it would be nice to avoid the theme stuff -- but how often does one use a single boxplot anyway?

@rpruim

Adding the aesthetic x = factor("") is one way to remove the undesired vertical grid lines which appear in your example.

library(ggplot2)
ggplot(mtcars, aes(y = disp, x = factor(""))) + geom_boxplot()

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

I prefer the extra grid lines to "factor("")" and I prefer not specifying an aesthetic that isn't needed. But one way or another there is one more thing to turn off: either vertical grid lines (if you don't want them) or the x label and if the missing x were treated more like a factor, that would save one step.

But low priority (to me) unless it is easy.

The default x value provided by geom_boxplot() is 0. I think it should be factor(0) or factor(""). Boxplots don't normally have a continuous x axis.

@rpruim The vertical grid lines are a consequence of x being treated as numeric. My interpretation of dannyparsons' comment is that a more appropriate default would be to treat x as a factor instead. Oh and as I write this, clauwilke seems to think so too. It should be easy to fix.

Possibly this commit: 42c86d5

and these lines:

data$x <- data$x %||% 0

params$width <- params$width %||% (resolution(data$x %||% 0) * 0.75)

If easy to fix, I'm all for it.

lock commented

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/