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:
but then it gets funky when I try to make a line. Show everything the same.
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?
library(ggplot2)
set.seed(10)
CO3 <- data.frame(CO2[, 2:3], outcome=factor(sample(c('none', 'some', 'lots', 'tons'),
nrow(CO2), rep=T), levels=c('none', 'some', 'lots', 'tons')))
CO3
x <- ggplot(CO3, aes(x=outcome)) + geom_bar(aes(x=outcome))
x
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.name="id"){
DF <- data.frame(c=1:nrow(dataframe), dataframe)
colnames(DF)[1] <- id.name
return(DF)
}
IDer(mtcars)
source to share
In version 0.9.0, ggplot2
there is a bug in relation facet_grid()
and duplicate strings. See https://github.com/hadley/ggplot2/issues/443
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)
source to share