How exactly do you expect to stack a bar with a negative height?
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/
On Wed, Oct 6, 2010 at 11:09 PM, Raphael <rhv....@gmail.com> wrote:
> Well, it's a negative value more than a negative height in my view.
>
> Perhaps I shouldn't but I was expecting something similar to Excel. In
> Excel, all four elements would be stacked. The negative "Home D" value
> would begin at y=- 0.435 and extend to y = 0. The other three elements
> would stack on top of that. In the end, the top of the graph would
> have y = sum of all elements - Home D = 14.198 - 0.435. Effectively,
> the length of the bar is the sum of the elements (14.198) but the max
> y value is 14.198 - 0.435.
>
> It seems to me like you're suggesting that negative values can't be
> stacked in ggplot?
This can be done quite easily actually. You just need to plot the
positive and negative values in separate layers, like this:
cog$pn <- factor(sign(cog$C2G), labels=c("-", "+"))
stackedchart <- ggplot(cog, aes(x=Quarter, y=C2G,
fill=factor(Segment)))
stackedchart <- stackedchart + layer(
geom="bar",
stat="identity",
position="stack",
data=subset(cog, pn == "+"))
stackedchart <- stackedchart + layer(
geom="bar",
stat="identity",
position="stack",
data=subset(cog, pn == "-"))
stackedchart + geom_hline(yintercept=0)
>
> In fact... there are negative values in all Segment levels when I look
> at the entire dataset (2008 Q1 - 2014 Q4) but "Home D" is the only
> level that "disappears". It's almost as if whenever "Home D" is
> negative, ggplot uses it's negative value as the starting y-value for
> the other three stacked values??? That's what it seems like, at
> least.
It's there, underneath the others. Try
stackedchart <- ggplot(cog, aes(x=Quarter, y=C2G,
fill=factor(Segment)))
stackedchart <- stackedchart + layer(
geom="bar",
stat="identity",
position="stack",
alpha=.5)
stackedchart
and you will see it.
Best,
Ista
--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org