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>
On 4/23/2012 9:24 AM, Matthias Rieber wrote:> 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.There is nothing wrong with using bars with this sort of data. There is a bug in the faceting code of 0.9.0 that will be fixed in 0.9.1 (see https://github.com/hadley/ggplot2/issues/443 ) which caused duplicate rows of data to be dropped when there was faceting. That is what you are seeing in the second example; row 6 is identical to row 7 and is dropped before plotting. One easy workaround until 0.9.1 comes out is to add unique column to the data that is otherwise ignored: 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), dummy=1:8)> Matthias-- Brian S. Diggs, PhD Senior Research Associate, Department of Surgery Oregon Health & Science University