亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果數據丟失,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 關注
  • 794 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號