geom_bar - modifying gap between bars

12,512 views
Skip to first unread message

Jon Olav Vik

unread,
Oct 14, 2010, 10:02:34 AM10/14/10
to ggplot2
Can I eliminate the gap between neighbouring stacks of bars in this
example?

dplot <- ggplot(diamonds, aes(clarity, fill = cut))
dplot + geom_bar(position = "stack")

Any help appreciated,
Jon Olav

Brandon Hurr

unread,
Oct 14, 2010, 10:10:43 AM10/14/10
to Jon Olav Vik, ggplot2
It's pretty ugly, but...

dplot + geom_bar(width=1)

I think the default is 0.9. 

B


--
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

Jon Olav Vik

unread,
Oct 14, 2010, 12:31:33 PM10/14/10
to ggplot2
> On Thu, Oct 14, 2010 at 15:02, Jon Olav Vik <jono...@gmail.com> wrote:
> > Can I eliminate the gap between neighbouring stacks of bars in this
> > example?
>
> > dplot <- ggplot(diamonds, aes(clarity, fill = cut))
> > dplot + geom_bar(position = "stack")

On Oct 14, 4:10 pm, Brandon Hurr <bhiv...@gmail.com> wrote:
> It's pretty ugly, but...
>
> dplot + geom_bar(width=1)

Thank you! I knew I'd seem such an option before, but couldn't find it
in ?geom_bar 8-)


Even with the "width" argument, I needed to debug a bit before it
worked. I describe my case here for posterity:

My actual data frame was a molten crosstabulation, like this:

> df <- read.table(header=TRUE, textConnection("
Species variable value
Apple Fat 0.1
Orange Fat 0.2
Apple Protein 0.2
Orange Protein 0.4
Apple Carbo 0.7
Orange Carbo 0.4
"))

The final solution was this:

> ggplot(df) + geom_bar(aes(x=Species, y=value, fill=variable), width=1)

This line below is what I started with. I didn't realize that
stat="identity" is superfluous when both x and y are given. Also,
stacking seems to be the default.

> ggplot(df) + geom_bar(aes(x=Species, y=value, fill=variable), stat="identity", position="stack")

Adding "width" to the latter command has no effect; the argument is
just ignored.

> ggplot(df) + geom_bar(aes(x=Species, y=value, fill=variable), stat="identity", position="stack", width=1)

I guess the reason I don't need stat_identity is that I have only one
row per "Species" and "variable". The default aggregation seems to be
averaging (ggplot(rbind(df, df)) + ... looks identical to ggplot(df)
+ ...), so that the data pass through unchanged.

Thank you very much for your help!

Regards,
Jon Olav
Reply all
Reply to author
Forward
0 new messages