发生数据丢失时geom_bar的宽度一致 [英] Consistent width for geom_bar in the event of missing data
问题描述
如果在下面的时间序列示例中缺少数据,有没有办法为 geom_bar()
设置恒定宽度?我已经尝试在 aes()
中设置 width
,但没有运气。
颜色< - c( #FF0000,#33CC33,#CCCCCC,#FFA500,#000000)
iris $ Month< - rep(seq(from = as.Date(2011-01-01 ),to = as.Date(2011-10-01),by =month),15)
颜色< -c(#FF0000,#33CC33 ,#CCCCCC,#FFA500,#000000)
iris $ Month < - rep(seq(from = as.Date(2011-01-01))to = as.Date (2011-10-01),by =month),15)
d< -aggregate(iris $ Sepal.Length,by = list(iris $ Month,iris $ Species),sum)
d $ quota< -seq(from = 2000,to = 60000,by = 2000)
colnames(d)< -c(Month,Species,Sepal.Width, )
d $ Sepal.Width <-d $ Sepal.Width * 1000
g1 < - ggplot(data = d,aes(x = Month,y = Quota,color =Quota)) + geom_line(size = 1)
g1 + geom_bar(data = d [c(-1:-5),],aes(x = Month,y = Sepal.Width,width = 10,group = fill = Species),stat =identity,position =dodge)+ scale_fill_manual(values = colors)
NA
。举一个简单的例子(因为你有很多不需要的特性):$ $ p $
dat< - data.frame(a = rep (信件[1:3],3),
b = rep(信件[1:3],每个= 3),
v = 1:9)[ - 2,]
ggplot(dat,aes(x = a,y = v,color = b))+
geom_bar(aes(fill = b),stat =identity,position =dodge)
a 和 b $ c $所有组合的数据框来补充
dat
c>:
dat.all< - rbind(dat,cbind(expand.grid(a = levels(dat $ a ),b = levels(dat $ b)),v = NA))
ggplot(dat.all,aes(x = a,y = v,color = b))+
geom_bar(aes(fill = b),stat =identity,position =dodge)
Is there a way to set a constant width for geom_bar()
in the event of missing data in the time series example below? I've tried setting width
in aes()
with no luck. Compare May '11 to June '11 width of bars in the plot below the code example.
colours <- c("#FF0000", "#33CC33", "#CCCCCC", "#FFA500", "#000000" )
iris$Month <- rep(seq(from=as.Date("2011-01-01"), to=as.Date("2011-10-01"), by="month"), 15)
colours <- c("#FF0000", "#33CC33", "#CCCCCC", "#FFA500", "#000000" )
iris$Month <- rep(seq(from=as.Date("2011-01-01"), to=as.Date("2011-10-01"), by="month"), 15)
d<-aggregate(iris$Sepal.Length, by=list(iris$Month, iris$Species), sum)
d$quota<-seq(from=2000, to=60000, by=2000)
colnames(d) <- c("Month", "Species", "Sepal.Width", "Quota")
d$Sepal.Width<-d$Sepal.Width * 1000
g1 <- ggplot(data=d, aes(x=Month, y=Quota, color="Quota")) + geom_line(size=1)
g1 + geom_bar(data=d[c(-1:-5),], aes(x=Month, y=Sepal.Width, width=10, group=Species, fill=Species), stat="identity", position="dodge") + scale_fill_manual(values=colours)
The easiest way is to supplement your data set so that every combination is present, even if it has NA
as its value. Taking a simpler example (as yours has a lot of unneeded features):
dat <- data.frame(a=rep(LETTERS[1:3],3),
b=rep(letters[1:3],each=3),
v=1:9)[-2,]
ggplot(dat, aes(x=a, y=v, colour=b)) +
geom_bar(aes(fill=b), stat="identity", position="dodge")
This shows the behavior you are trying to avoid: in group "B", there is no group "a", so the bars are wider. Supplement dat
with a dataframe with all the combinations of a
and b
:
dat.all <- rbind(dat, cbind(expand.grid(a=levels(dat$a), b=levels(dat$b)), v=NA))
ggplot(dat.all, aes(x=a, y=v, colour=b)) +
geom_bar(aes(fill=b), stat="identity", position="dodge")
这篇关于发生数据丢失时geom_bar的宽度一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!