negative weights in geom_bar

62 views
Skip to first unread message

andrea quin

unread,
Dec 23, 2009, 6:06:54 PM12/23/09
to ggplot2
Hi

I was using ggplot version 0.8.3 to make histograms with negative
counts by using a negative weight but have just upgraded to 0.8.5 and
it doesn't now work, instead giving me an error. The reason I am
doing this is so I can have back-to-back histograms with certain
levels of a factor positive and certain ones negative.

A simple example:

ggplot(diamonds, aes(color, fill=cut)) + geom_bar(aes(weight = -1))

gives the error

Error in `[<-.data.frame`(`*tmp*`, var, value = list(`NA` = NULL)) :
missing values are not allowed in subscripted assignments of data
frames

More fully, I'd like to run something like

ggplot(diamonds, aes(color, fill=cut)) + geom_bar(subset = .(cut ==
"Fair" | cut == "Good"), aes(weight = -1)) + geom_bar(subset = .(cut
== "Premium" | cut == "Ideal"), aes(weight = 1))

Any clues?

Thanks!

Luciano Selzer

unread,
Dec 23, 2009, 9:13:36 PM12/23/09
to andrea quin, ggplot2
Hi Andrea, maybe you can look a post at learn R blog. Maybe it will help you.


Luciano


2009/12/23 andrea quin <andrea...@gmail.com>

--
You received this message because you are subscribed to the ggplot2 mailing list.
To post to this group, send email to ggp...@googlegroups.com
To unsubscribe from this group, send email to
ggplot2+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/ggplot2

andrea quin

unread,
Dec 23, 2009, 10:57:24 PM12/23/09
to ggplot2
I have seen that thanks Luciano. Unfortunately they use pre-
summarized data - it would be nice if I could make ggplot do the
work...

Andrea

On Dec 24, 3:13 pm, Luciano Selzer <luciano.sel...@gmail.com> wrote:
> Hi Andrea, maybe you can look a post at learn R blog. Maybe it will help
> you.http://learnr.wordpress.com/2009/09/24/ggplot2-back-to-back-bar-charts/
>

> <http://learnr.wordpress.com/2009/09/24/ggplot2-back-to-back-bar-charts/>
> Luciano
>

hadley wickham

unread,
Dec 24, 2009, 4:10:08 AM12/24/09
to Luciano Selzer, andrea quin, ggplot2
> Hi Andrea, maybe you can look a post at learn R blog. Maybe it will help
> you.
> http://learnr.wordpress.com/2009/09/24/ggplot2-back-to-back-bar-charts/

Yes, I'd definitely recommend that technique - negative weights are
not meaningful.

Hadley

--
http://had.co.nz/

andrea quin

unread,
Dec 24, 2009, 6:56:08 AM12/24/09
to ggplot2
OK, you've beaten me into submission... and you are right, it is
enough. For completeness of the thread, here is a solution to my own
little problem:

diam.tab = with(diamonds, table(cut,color))
diam.summ = as.data.frame(diam.tab)
ggplot(diam.summ, aes(color, fill=cut)) + geom_bar(subset = .(cut ==
"Fair" | cut == "Good"| cut == "Very Good"), aes(y = -Freq)) + geom_bar
(subset = .(cut == "Premium" | cut == "Ideal"), aes(y = Freq))

Thanks

Andrea

Reply all
Reply to author
Forward
0 new messages