如果数据丢失,geom_bar的宽度一致

有没有办法geom_bar()在下面的时间序列示例中丢失数据的情况下设置恒定宽度?我试过设置width在aes()没有运气。在代码示例下方的图中比较5月'11至6月'11的条形宽度。


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)


潇潇雨雨
浏览 720回答 3
3回答

拉丁的传说

最简单的方法是补充数据集,以便每个组合都存在,即使它具有NA其值。举一个更简单的例子(因为你的有很多不需要的功能):dat <- data.frame(a=rep(LETTERS[1:3],3),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b=rep(letters[1:3],each=3),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v=1:9)[-2,]ggplot(dat, aes(x=a, y=v, colour=b)) +&nbsp; geom_bar(aes(fill=b), stat="identity", position="dodge")这显示了您要避免的行为:在组“B”中,没有组“a”,因此条形更宽。补充dat用的所有组合一个数据帧a,并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)) +&nbsp; geom_bar(aes(fill=b), stat="identity", position="dodge")&nbsp;&nbsp;

慕桂英4014372

ggplot2 3.0.0中引入的一些新选项position_dodge()和新选项可以提供帮助。position_dodge2()您可以使用preserve = "single"in position_dodge()来将宽度基于单个元素,因此所有条形的宽度将相同。ggplot(data = d, aes(x = Month, y = Quota, color = "Quota")) +&nbsp;&nbsp; &nbsp; &nbsp;geom_line(size = 1) +&nbsp;&nbsp; &nbsp; &nbsp;geom_col(data = d[c(-1:-5),], aes(y = Sepal.Width, fill = Species),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; position = position_dodge(preserve = "single") ) +&nbsp;&nbsp; &nbsp; &nbsp;scale_fill_manual(values = colours)使用position_dodge2()事物居中的方式进行更改,将每组条形图集中在每个x轴位置。它有一些padding内置,所以padding = 0用来删除。ggplot(data = d, aes(x = Month, y = Quota, color = "Quota")) +&nbsp;&nbsp; &nbsp; &nbsp;geom_line(size = 1) +&nbsp;&nbsp; &nbsp; &nbsp;geom_col(data = d[c(-1:-5),], aes(y = Sepal.Width, fill = Species),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; position = position_dodge2(preserve = "single", padding = 0) ) +&nbsp;&nbsp; &nbsp; &nbsp;scale_fill_manual(values = colours)

临摹微笑

我有同样的问题,但正在寻找一个适用于pipe(%>%)的解决方案。使用tidyr::spread和tidyr::gather来自tidyverse诀窍。我使用与@Brian Diggs相同的数据,但是当转换为宽时,大写变量名称不会以双变量名结尾:library(tidyverse)dat <- data.frame(A = rep(LETTERS[1:3], 3),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; B = rep(letters[1:3], each = 3),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; V = 1:9)[-2, ]dat %>%&nbsp;&nbsp; spread(key = B, value = V, fill = NA) %>% # turn data to wide, using fill = NA to generate missing values&nbsp; gather(key = B, value = V, -A) %>% # go back to long, with the missings&nbsp; ggplot(aes(x = A, y = V, fill = B)) +&nbsp; geom_col(position = position_dodge())编辑:实际上,与管道结合的问题实际上有一个更简单的解决方案。使用tidyr::complete在一行中给出相同的结果:dat %>%&nbsp;&nbsp; complete(A, B) %>%&nbsp;&nbsp; ggplot(aes(x = A, y = V, fill = B)) +&nbsp; geom_col(position = position_dodge())
打开App,查看更多内容
随时随地看视频慕课网APP