Ggplot unexplained result

I started making a reproducible example to ask another question and can't even get past that. Anyway, I'm trying to put categorical data into a faceted bar. So I made my own dataset using CO3 (code below). It's just that the x conspiracy itself seems normal: enter image description here

but then it gets funky when I try to make a line. Show everything the same. enter image description here

This does not make any sense as it indicates that each subgroup is of equal proportions due to the fact that the ftable

data is not supported :

                   Type Quebec Mississippi
outcome Treatment                         
none    nonchilled           7           6
        chilled              4           7
some    nonchilled           6           4
        chilled              5           5
lots    nonchilled           5           4
        chilled              6           3
tons    nonchilled           3           7
        chilled              6           6


What am I doing wrong?

CO3 <- data.frame(CO2[, 2:3], outcome=factor(sample(c('none', 'some', 'lots', 'tons'), 
           nrow(CO2), rep=T), levels=c('none', 'some', 'lots', 'tons')))
x <- ggplot(CO3, aes(x=outcome)) + geom_bar(aes(x=outcome))
x  + facet_grid(Treatment~., margins=TRUE)

with(CO3, ftable(outcome, Treatment, Type))


EDIT: This problem that Brian describes is easy to find when you need to collect data. To get to the next version of ggplot (I assume Hadley is aware of this issue), I created a silly little convenience function to quickly add an id column to a dataframe:

IDer <- function(dataframe,"id"){
    DF <- data.frame(c=1:nrow(dataframe), dataframe)
    colnames(DF)[1] <-




source to share

1 answer

In version 0.9.0, ggplot2

there is a bug in relation facet_grid()

and duplicate strings. See

A workaround is to add a dummy column to break up the duplication.

CO3$dummy <- 1:nrow(CO3)

ggplot(CO3, aes(x=outcome)) + 
  geom_bar(aes(x=outcome)) + 
  facet_grid(Treatment~., margins=TRUE)


enter image description here



All Articles