0
votes

Setting the ggplot2 binwidth in geom_histogram while using scale_x_log10 produces a weird histogram.

I want to adjust the binwidth without the workaround found here.

One reason I don't want to use the workaround is that I don't like it; it seems like there ought to be a better way built into ggplot. The other reason is that it didn't work when I tried it on my data set.

I'm using facet_wrap, so the solution needs to work with that, but the example code I'm using is stripped down to the minimum.

When I allow the default binwidth, I get a decent histogram:

library(ggplot2)
data(diamonds)
ggplot(data=diamonds, aes(x=price/carat)) +
  geom_histogram() +
  scale_x_log10()# +
  # facet_wrap(~cut, ncol=1, scales='free_y')

Histogram with default binwidth.

But, when I set the binwidth, I get a uniform distribution filling the entire graph (or a single bin?) no matter what the binwidth (except when binwidth=1, which produces what look like two bins, or a bimodal uniform distribution?):

ggplot(data=diamonds, aes(x=price/carat)) +
  geom_histogram(binwidth=10) +
  scale_x_log10()# +
  # facet_wrap(~cut, ncol=1, scales='free_y')

Histogram with binwidth passed.

Setting breaks produces the same filled square with new breaks. Setting limits the clears the graph.

Setting the binwidth from within ggplot() itself leaves the graph unchanged from default binwidths, presumably because geom_histogram overrides it. And, scale_x_log10 doesn't accept binwidth.

It works to set binwidth while using scale_x_continuous instead of scale_x_log10.

1

1 Answers

2
votes

Try entering a fraction of the total width, such that the binwidth relates to number of bins as something like 1/(n_bins - 1).

library(ggplot2)

data(diamonds)
ggplot(data=diamonds, aes(x=price/carat)) +
  geom_histogram(binwidth = 1/50) +
  scale_x_log10()