[R] ggplot2 - geom_bar

Matthias Rieber ml-r-project at zu-con.org
Mon Apr 23 18:24:45 CEST 2012


Hello,

I've some problem with the ggplot2. Here's a small example:

--8<--
library(ggplot2)
molten <- data.frame(date=c('01','01','01','01',
                            '02','02','02','02'),
                     channel=c('red','red','blue','blue',
                               'red','red','blue','blue'),
                     product=c('product1','product2',
                               'product1','product2',
                               'product1','product2',
                               'product1','product2'),
                     value=c(1,1,1,1,
                             1,1,1,1))

str(molten)
molten

ggplot(molten, aes(date, weight = value, fill = channel)) +
  geom_bar(colour = I('black')) + facet_grid(product ~ .)
-->8--

This gives the expected result (at least I expect this, see attachment
case1). When I change molten to:

molten <- data.frame(date=c('01','01','01','01',
                            '02','02','02','02'),
                     channel=c('red','red','blue','blue',
                               'red','red','blue','blue'),
                     product=c('product1','product2',
                               'product1','product2',
                               'product1','product1',
                               'product1','product2'),
                     value=c(1,1,1,1,
                             1,1,1,1))

I get a strange result(see case2). I expect that for date=02 and
product=product1 the bar should show 2 'red', but it's just 1. So the
total sum is 7 instead of 8.

When I change molten again:

molten <- data.frame(date=c('01','01','01','01',
                            '02','02','02','02'),
                     channel=c('red','red','blue','blue',
                               'red','red','blue','blue'),
                     product=c('product1','product2',
                               'product1','product2',
                               'product1','product1',
                               'product1','product2'),
                     value=c(1,1,1,1,
                             1,2,1,1))

I get the expected result, where the bar show 3 'red' and the total sum
is 9.

Is it wrong to use geom_bar with that kind of data? I could avoid this
issue when I cast the data.frame, but I like to avoid that solution.


Matthias
-------------- next part --------------
A non-text attachment was scrubbed...
Name: case1.png
Type: image/png
Size: 14598 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120423/8d6faefc/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: case2.png
Type: image/png
Size: 14599 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120423/8d6faefc/attachment-0001.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: case3.png
Type: image/png
Size: 13109 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120423/8d6faefc/attachment-0002.png>


More information about the R-help mailing list