为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

潇潇雨雨 2019-09-02 17:12:11
有没有办法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 * 1000g1 <- 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)
查看完整描述

3 回答

?
拉丁的传说

TA贡献1789条经验 获得超8个赞

最简单的方法是补充数据集,以便每个组合都存在,即使它具有NA其值。举一个更简单的例子(因为你的有很多不需要的功能):


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")


这显示了您要避免的行为:在组“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)) +

  geom_bar(aes(fill=b), stat="identity", position="dodge")  


查看完整回答
反对 回复 2019-09-02
?
慕桂英4014372

TA贡献1871条经验 获得超13个赞

ggplot2 3.0.0中引入的一些新选项position_dodge()和新选项可以提供帮助。position_dodge2()


您可以使用preserve = "single"in position_dodge()来将宽度基于单个元素,因此所有条形的宽度将相同。


ggplot(data = d, aes(x = Month, y = Quota, color = "Quota")) + 

     geom_line(size = 1) + 

     geom_col(data = d[c(-1:-5),], aes(y = Sepal.Width, fill = Species), 

              position = position_dodge(preserve = "single") ) + 

     scale_fill_manual(values = colours)



使用position_dodge2()事物居中的方式进行更改,将每组条形图集中在每个x轴位置。它有一些padding内置,所以padding = 0用来删除。


ggplot(data = d, aes(x = Month, y = Quota, color = "Quota")) + 

     geom_line(size = 1) + 

     geom_col(data = d[c(-1:-5),], aes(y = Sepal.Width, fill = Species), 

              position = position_dodge2(preserve = "single", padding = 0) ) + 

     scale_fill_manual(values = colours)


查看完整回答
反对 回复 2019-09-02
?
临摹微笑

TA贡献1982条经验 获得超2个赞

我有同样的问题,但正在寻找一个适用于pipe(%>%)的解决方案。使用tidyr::spread和tidyr::gather来自tidyverse诀窍。我使用与@Brian Diggs相同的数据,但是当转换为宽时,大写变量名称不会以双变量名结尾:


library(tidyverse)


dat <- data.frame(A = rep(LETTERS[1:3], 3),

                  B = rep(letters[1:3], each = 3),

                  V = 1:9)[-2, ]

dat %>% 

  spread(key = B, value = V, fill = NA) %>% # turn data to wide, using fill = NA to generate missing values

  gather(key = B, value = V, -A) %>% # go back to long, with the missings

  ggplot(aes(x = A, y = V, fill = B)) +

  geom_col(position = position_dodge())

编辑:


实际上,与管道结合的问题实际上有一个更简单的解决方案。使用tidyr::complete在一行中给出相同的结果:


dat %>% 

  complete(A, B) %>% 

  ggplot(aes(x = A, y = V, fill = B)) +

  geom_col(position = position_dodge())


查看完整回答
反对 回复 2019-09-02
  • 3 回答
  • 0 关注
  • 719 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信