See the example below:
library(ggplot2)
m.vec <- c("2009 11", "2009 12", "2010 01")
df <- data.frame(m = rep(m.vec, each=10), cate = rep(LETTERS[1:3],
10), summ = abs(rnorm(30)))
df <- ddply(df, c("m", "cate"), summarise, summ = sum(summ))
df <- subset(df, !(m == "2009 12" & cate == "B"))
ggplot(df, aes(m, summ)) + geom_bar(position="dodge", stat="identity")
+ facet_wrap(~ cate)
The display in graph B is incorrect; the original data contains no
observations for "2009 12", but instead of leaving a blank here, the
bars for "2009 11" and "2010 1" are spread over the space where the
blank should be. How to fix this?
-jrara
If you replace your subset line with
df[df$m=="2009 12" & df$cate == "B","summ"] <- NA
you should get a plot with the space left in. Not sure if this is the
best way, but it works.
--Brian Diggs
-jrara
2010/2/3 Brian Diggs <dig...@ohsu.edu>:
> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> To post to this group, send email to ggp...@googlegroups.com
> To unsubscribe from this group, send email to
> ggplot2+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/ggplot2
I get the data from a customer in the form on df1 (I can't help it) in
the example below
library(ggplot2)
m.vec <- c("2009 11", "2009 12", "2010 01")
df <- data.frame(m = rep(m.vec, each=10), cate = rep(LETTERS[1:3],
10), summ = abs(rnorm(30)))
df <- ddply(df, c("m", "cate"), summarise, summ = sum(summ))
df1 <- subset(df, !(m == "2009 12" & cate == "B"))
df1
m cate summ
1 2009 11 A 1.284907
2 2009 11 B 3.231534
3 2009 11 C 1.409339
4 2009 12 A 1.322530
6 2009 12 C 1.927723
7 2010 01 A 3.309807
8 2010 01 B 3.473588
9 2010 01 C 4.041790
ggplot(df1, aes(m, summ)) + geom_bar(position="dodge",
stat="identity") + facet_wrap(~ cate)
2010/2/3 James Howison <ja...@freelancepropaganda.com>:
library(ggplot2)
m.vec <- c("2009 11", "2009 12", "2010 01")
df <- data.frame(m = rep(m.vec, each=10), cate = rep(LETTERS[1:3],
10), summ = abs(rnorm(30)))
df <- ddply(df, c("m", "cate"), summarise, summ = sum(summ))
df1 <- subset(df1, !(m == "2009 12" & cate == "B"))
#This wrong
ggplot(df1, aes(m, summ)) + geom_bar(position="dodge",
stat="identity") + facet_wrap(~ cate)
#Now I should using this kind of tweak
tb <- with(df1, table(m, cate))
tb.r <- rownames(tb)[which(tb == 0, arr.ind=T)[1]]
tb.c <- colnames(tb)[which(tb == 0, arr.ind=T)[2]]
df2 <- rbind(df1, c(tb.r, tb.c, NA))
df2$summ <- as.numeric(df2$summ)
#This is right
ggplot(df2, aes(m, summ)) + geom_bar(position="dodge",
stat="identity") + facet_wrap(~ cate)
-jrara
2010/2/3 johannes rara <johann...@gmail.com>:
Try this:
ggplot(df1, aes(m, weight = summ)) + geom_bar() + facet_wrap(~ cate)
Hadley
> Hadley, a little explanation on the logic of the different behaviors? I
> seem to be a bit too dense this morning. Maybe some more tea.
To be honest, I'm not completely sure - I think it comes down to how
the widths are calculated. stat_bin knows to make bins a fixed width
(0.9) whereas it isn't set by stat_identity so probably uses 90% of
the resolution of the data.
Hadley