DATA MINING
Desktop Survival Guide by Graham Williams |
|||||
Stacks Versus Lines |
As pointed out at http://peltiertech.com/WordPress/2008/08/27/stacked-vs-clustered/a line chart might be better than a stacked bar chart. Depends on situation. Example from Hadley Wickham on r-help 28 Aug 2008. The example from the above URL might be clearer to illustrate the point.
quarter <- as.factor(sample(c("Q1", "Q2", "Q3", "Q4"), 100, replace = TRUE)) year <- as.factor(sample(c(seq(from=2000, to=2008)), 100, replace = TRUE)) category <- as.factor(sample(c(seq(from=1, to=4)), 100, replace = TRUE)) test <- data.frame(quarter, year, category) tabdf <- as.data.frame(with(test, table(category, quarter, year))) library(ggplot2) qplot(year, Freq, data=tabdf, geom="line", colour = category, facets = quarter ~ . , group = interaction(category, quarter)) # OR qplot(year, Freq, data=tabdf, geom="line", colour = quarter, facets = category ~ . , group = interaction(category, quarter)) # If you _really_ want stacking: qplot(year, Freq, data=tabdf, geom="area", fill = quarter, facets = category ~ . , group = interaction(category, quarter)) # OR qplot(year, Freq, data=tabdf, geom="bar", stat="identity", fill = quarter, facets = category ~ .) # OR finally, like what you originally asked for qplot(quarter, Freq, data=tabdf, geom="bar", stat="identity", fill = category, facets = . ~ year) |