Suggestion: use stat_identity as default for geom_bar

431 views
Skip to first unread message

JiHO

unread,
Jan 18, 2012, 5:10:50 PM1/18/12
to ggplot2
Hi all,

geom_bar with its non-"identity" statistic confuses novice users and
forces them to learn about the stat argument (and statistics in
general in the grammar of graphics) which greatly complicates a first
approach to ggplot.

I suggested this in the past (and I know it would break existing code)
but I think geom_bar should default to stat="identity". At least, this
should be the case when y is supplied, which would limit the backward
compatibility problem. The requirement for stat="bin" is reduced by
the existence of geom_histogram anyway.

What does a "barplot" mean to most people? To me it just means a graph
with vertical bars, not necessarily a histogram, hence my expectation
that y should be specified and give the height of the bars.

JiHO
---
http://maururu.net

Winston Chang

unread,
Jan 19, 2012, 1:04:03 AM1/19/12
to JiHO, ggplot2
This sounds like a good suggestion to me. I recall being a bit confused in the beginning about when geom_bar would display counts, and when it would display the y values.

Here are some examples:

# Display counts. (stat="bin" is the default)
ggplot(mtcars, aes(x=factor(cyl))) + geom_bar(stat="bin")

# Error, because using stat="identity" without specifying y
# (It is good that it throws an error)
ggplot(mtcars, aes(x=factor(cyl))) + geom_bar(stat="identity")

# If we add y mapping, it will stack y values, which can also be confusing
ggplot(mtcars, aes(x=factor(cyl), y=mpg)) + geom_bar(stat="identity")


# Error, because using stat="bin" with multiple y values.
# This behavior can be confusing.
ggplot(mtcars, aes(x=factor(cyl), y=mpg)) + geom_bar(stat="bin")

# This works and gives the same output for "bin" and "identity":
# Pull out one instance each of cyl==4, 6, and 8
ggplot(mtcars[c(1,3,5),], aes(x=factor(cyl), y=mpg)) + geom_bar(stat="bin")
ggplot(mtcars[c(1,3,5),], aes(x=factor(cyl), y=mpg)) + geom_bar(stat="identity")


One the whole, I'd say that using stat="identity" as the default is slightly less confusing than stat="bin". But even with this change, it can still be a bit confusing when there are multiple y values at a single x, because the bars get stacked, and by default it looks like a single bar because they don't have visible outlines.

I don't think that the default stat should be conditional on whether a y mapping is provided -- I think the small benefit would be outweighed by the added complexity and inconsistent behavior.

-Winston

Hadley Wickham

unread,
Jan 19, 2012, 7:55:03 AM1/19/12
to Winston Chang, JiHO, ggplot2
See the discussion here:

https://github.com/hadley/ggplot2/issues/161 and here
https://github.com/hadley/ggplot2/issues/82

Basically:

* there are two types of bar charts and unfortunately we give them the
same name. It's equally valid to expect that geom_bar will produce
either of them

* Unless you can provide compelling evidence that one is much more
common that the other, or suggest better names for both, I'm unwilling
to change the current behaviour because I think it will cause just as
many problems (but in the other direction)

Hadley

> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

JiHO

unread,
Jan 19, 2012, 7:11:56 PM1/19/12
to Hadley Wickham, Winston Chang, ggplot2
On Thu, Jan 19, 2012 at 13:55, Hadley Wickham <had...@rice.edu> wrote:
> See the discussion here:
>
> https://github.com/hadley/ggplot2/issues/161 and here
> https://github.com/hadley/ggplot2/issues/82

Yeah, that's me again.

> * there are two types of bar charts and unfortunately we give them the
> same name.  It's equally valid to expect that geom_bar will produce
> either of them
>
> * Unless you can provide compelling evidence that one is much more
> common that the other,

I cannot provide general, literature-based evidence but I can say that
every single student I've taught:
- uses geom_histogram rather than geom_bar when asked to "represent
the distribution of a piece of data"
- is puzzled by the behaviour of geom_bar when he/she first tries it
I know this for a fact because these are two necessary steps in the
labs they do with me.

I insist that the first question does not mention the word "histogram"
but just "distribution" (because I would also accept a density plot),
but histogram is what they naturally think of and when they search the
documentation for it, they find geom_histogram.
The second question is about the total abundance of several species in
a dataset in three different locations, so they are usually drawn
towards a bar chart with three bars of different colour per species
(Excel habits I guess). Which either throws an error when the don't
pre-compute the sum of abundances or works (but abusively IMHO -- see
previous email) when they do.

> or suggest better names for both, I'm unwilling
> to change the current behaviour because I think it will cause just as
> many problems (but in the other direction)

I don't think the names could be improved (certainly not
geom_histogram and I can't think of something else for geom_bar).
But an additional argument to keep those names but change the
behaviour might be that:
- barplot() in base R produces bars of specified height (i.e. as
geom_bar with stat="identity")
- hist() in base R produces a histogram

It would be good to have the opinion of a larger sample of people on
this matter. Thanks in advance,

JiHO
---
http://maururu.net

JiHO

unread,
Jan 19, 2012, 7:24:50 PM1/19/12
to Hadley Wickham, Winston Chang, ggplot2
On Fri, Jan 20, 2012 at 01:11, JiHO <jo.l...@gmail.com> wrote:
> I cannot provide general, literature-based evidence but I can say that
> every single student I've taught:
> - uses geom_histogram rather than geom_bar when asked to "represent
> the distribution of a piece of data"
> - is puzzled by the behaviour of geom_bar when he/she first tries it
> I know this for a fact because these are two necessary steps in the
> labs they do with me.

Just to back it up with numbers and context:
- question 1 comes in every "introduction to R" class that I do so
that's ~100-120 students these last 3 years.
- question 2 comes in a specific lab so that's more like ~40 students
over the same 3 years

These are MSc students, so usually with a fair background in
statistics. 60% of them were french, the rest came from EU mostly. Few
of them were native english speakers (but I taught in English). See
https://github.com/hadley/ggplot2/issues/161 about why this might be
important.

JiHO
---
http://maururu.net

Hadley Wickham

unread,
Jan 20, 2012, 8:08:44 AM1/20/12
to JiHO, Winston Chang, ggplot2

I have never had a student ask me about this. I think it is a
cultural difference - in Europe a bar chart typically means one thing,
and in New Zealand/USA it typically means something else.

It's not that I think my interpretation is right, but that if we
change it now, it will break a lot of code that currently works, and
break many people's expectations about what a barchart should do.

Hadley

JiHO

unread,
Jan 20, 2012, 5:48:08 PM1/20/12
to Hadley Wickham, Winston Chang, ggplot2
On Fri, Jan 20, 2012 at 14:08, Hadley Wickham <had...@rice.edu> wrote:
> I have never had a student ask me about this.  I think it is a
> cultural difference - in Europe a bar chart typically means one thing,
> and in New Zealand/USA it typically means something else.
>
> It's not that I think my interpretation is right, but that if we
> change it now, it will break a lot of code that currently works, and
> break many people's expectations about what a barchart should do.

That's probably it. Too bad for me you are not European then.

Still, the fact that geom_bar and geom_histogram both exist and do the
exact same thing would advocate for a change in geom_bar, wouldn't it?
(not that I am desperately trying to win an argument or anything...)

Or maybe we could detect the current locale of the user and have
geom_bar do something different based on ... just kidding ;)

JiHO
---
http://maururu.net

Dennis Murphy

unread,
Jan 20, 2012, 6:35:43 PM1/20/12
to JiHO, Hadley Wickham, Winston Chang, ggplot2
Hi:

I don't agree with your premise that geom_bar() and geom_histogram()
do the 'exact same thing'. This is a common misperception. Bar charts
are meant to visualize discrete distributions; a histogram is a crude
type of density estimator with (when correctly used) a fixed bandwidth
(i.e., binwidth) and a uniform kernel function. There's a good reason
why that little space between categories in a bar chart exists - it
signifies that the values are not meant to be interpreted on a
continuous numeric scale. Similarly, the adjacency of the bars in a
histogram is a subtle visual cue that the variable of interest is on a
continuous scale. What does a bar chart mean in Europe that does not
conform to this definition?

Dennis

JiHO

unread,
Jan 20, 2012, 7:09:00 PM1/20/12
to Dennis Murphy, Hadley Wickham, Winston Chang, ggplot2
On Sat, Jan 21, 2012 at 00:35, Dennis Murphy <djm...@gmail.com> wrote:
> I don't agree with your premise that geom_bar() and geom_histogram()
> do the 'exact same thing'.

In ggplot they do. That's what I meant:

library("ggplot2")
d <- data.frame(x=runif(20), y=runif(20), z=factor(round(runif(20, 0, 4))))
ggplot(d, aes(x=x)) + geom_bar()
ggplot(d, aes(x=x)) + geom_histogram()

ggplot(d, aes(x=z)) + geom_bar()
ggplot(d, aes(x=z)) + geom_histogram()

# and the problematic cases
ggplot(d, aes(x=x, y=y)) + geom_bar()
ggplot(d, aes(x=x, y=y)) + geom_bar(stat="identity")
ggplot(d, aes(x=z, y=y)) + geom_bar(colour="red") # not sure what is
done here...
ggplot(d, aes(x=z, y=y)) + geom_bar(stat="identity", colour="red")

> This is a common misperception. Bar charts
> are meant to visualize discrete distributions; a histogram is a crude
> type of density estimator with (when correctly used) a fixed bandwidth
> (i.e., binwidth) and a uniform kernel function. There's a good reason
> why that little space between categories in a bar chart exists - it
> signifies that the values are not meant to be interpreted on a
> continuous numeric scale. Similarly, the adjacency of the bars in a
> histogram is a subtle visual cue that the variable of interest is on a
> continuous scale.

In that case, geom_bar should only allow discrete x and
geom_histogram, continuous x. That would indeed make for a clean
separation between the two. But both would still use stat="bin" by
default.

> What does a bar chart mean in Europe that does not
> conform to this definition?

I wouldn't go as far as saying that this is the "European" definition
but the definition I most commonly see is purely geometric: a bar
chart is a char with bars on it (which start from 0 on well formed
ones). But these bars can be used to display the number of occurrences
of a category (i.e. a distribution) or just any, possibly continuous,
quantity. This is the way Excel does bar charts I think: it allows to
use bars in the same way points or a line would be used.

In this later case, I think that boxplots are almost always better
suited but some people insist on drawing bars, because it highlights
that the quantity rises from 0 or simply because it's customary. In a
recent paper the main authors insisted that I drew bars + error bars
for average + SD concentrations of chemical components, even though
that was a very crude representation of the variability (and the data
was far from normal so the meaning of the mean was dubious). But that
was what they usually saw in the journal they intended to publish in.

Again, I would not agree that this is the correct definition. Yours
appears much more solid. Mine is probably just an operational
derivative of the bar chart.

JiHO
---
http://maururu.net

Reply all
Reply to author
Forward
0 new messages