ggplot2 + geom_bar()缺少吧bug - ggplot2 + geom_bar() missing bars bug -开发者知识库

ggplot2 + geom_bar()缺少吧bug - ggplot2 + geom_bar() missing bars bug -开发者知识库,第1张

I have encountered a strange "bug" when I use purrr::map to plot a series of stacked bar charts with ggplot2::facet_grid and not all the data gets displayed (some of it gets discarded as missing values for some reason), seemingly at random. Each facet in these plots should have bars stacked to 100.

當我使用purrr :: map繪制一系列帶有ggplot2 :: facet_grid的堆積條形圖時,我遇到了一個奇怪的“bug”,並沒有顯示所有數據(其中一些因某些原因被丟棄為缺失值),貌似隨意的。這些圖中的每個面都應該有堆疊到100的條。

library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 3.5.1
#> Warning: package 'dplyr' was built under R version 3.5.1
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date

data_bot2 <- structure(list(date = structure(c(1316390400, 1316390400, 1316390400, 
                  1316390400, 1316390400, 1316390400, 1316390400, 1316390400, 1316390400, 
                  1316390400, 1316390400, 1316390400, 1316390400, 1316390400, 1316390400, 
                  1316390400, 1316390400, 1316390400, 1316390400, 1316390400, 1316390400, 
                  1316390400, 1316390400, 1316390400, 1316390400, 1316390400, 1316390400, 
                  1316390400, 1316390400, 1316390400, 1316390400, 1316390400, 1316390400, 
                  1316390400, 1316390400, 1320796800, 1320796800, 1320796800, 1320796800, 
                  1320796800, 1320796800, 1320796800, 1316390400, 1316390400, 1316390400, 
                  1316390400, 1316390400, 1316390400, 1316390400, 1320796800, 1320796800, 
                  1320796800, 1320796800, 1320796800, 1320796800, 1320796800, 1316390400, 
                  1316390400, 1316390400, 1316390400, 1316390400, 1316390400, 1316390400, 
                  1320796800, 1320796800, 1320796800, 1320796800, 1320796800, 1320796800, 
                  1320796800, 1316390400, 1316390400, 1316390400, 1316390400, 1316390400, 
                  1316390400, 1316390400, 1320796800, 1320796800, 1320796800, 1320796800, 
                  1320796800, 1320796800, 1320796800), class = c("POSIXct", "POSIXt"
                  ), tzone = "UTC"), fraction = c(76.85279188, 0, 2.984771574, 
                  3.736040609, 12.40609137, 0, 4.020304569, 84.01579713, 0, 0, 
                  6.110995635, 3.824568697, 0, 6.048638537, 86.14848032, 0, 1.868460389, 
                  3.836571998, 5.605381166, 0, 2.541106129, 78.49196539, 0, 0, 
                  11.42150803, 4.969097651, 0, 5.117428925, 90.9202454, 0, 0, 2.527607362, 
                  4.883435583, 0, 1.668711656, 56.36016139, 0, 28.62603525, 2.951794436, 
                  6.052240391, 0, 6.009768528, 54.99892957, 0, 40.69792336, 2.389210019, 
                  0.464568615, 0, 1.449368444, 60.97046414, 7.243319269, 10.33755274, 
                  8.509142053, 5.977496484, 0, 6.962025316, 78.43915344, 0, 6.084656085, 
                  0, 4.872134039, 0, 10.60405644, 65.43624161, 0, 26.41685309, 
                  2.125279642, 2.218493661, 0, 3.803131991, 74.36273611, 0, 15.22097928, 
                  3.300935265, 2.461947552, 0, 4.653401797, 47.54063753, 15.6217015, 
                  16.38167617, 12.64513405, 1.688832594, 0, 6.122018155), species = structure(c(1L, 
                  2L, 3L, 5L, 4L, 6L, 7L, 1L, 2L, 3L, 5L, 4L, 6L, 7L, 1L, 2L, 3L, 
                  5L, 4L, 6L, 7L, 1L, 2L, 3L, 5L, 4L, 6L, 7L, 1L, 2L, 3L, 5L, 4L, 
                  6L, 7L, 1L, 2L, 3L, 5L, 4L, 6L, 7L, 1L, 2L, 3L, 5L, 4L, 6L, 7L, 
                  1L, 2L, 3L, 5L, 4L, 6L, 7L, 1L, 2L, 3L, 5L, 4L, 6L, 7L, 1L, 2L, 
                  3L, 5L, 4L, 6L, 7L, 1L, 2L, 3L, 5L, 4L, 6L, 7L, 1L, 2L, 3L, 5L, 
                  4L, 6L, 7L), .Label = c("leaf", "stem", "ann", "og", "wc", "weed", 
                  "dead"), class = "factor"), cultivar = c("Alto", "Alto", "Alto", 
                  "Alto", "Alto", "Alto", "Alto", "Commando", "Commando", "Commando", 
                  "Commando", "Commando", "Commando", "Commando", "Alto", "Alto", 
                  "Alto", "Alto", "Alto", "Alto", "Alto", "Commando", "Commando", 
                  "Commando", "Commando", "Commando", "Commando", "Commando", "Alto", 
                  "Alto", "Alto", "Alto", "Alto", "Alto", "Alto", "Alto", "Alto", 
                  "Alto", "Alto", "Alto", "Alto", "Alto", "Commando", "Commando", 
                  "Commando", "Commando", "Commando", "Commando", "Commando", "Commando", 
                  "Commando", "Commando", "Commando", "Commando", "Commando", "Commando", 
                  "Alto", "Alto", "Alto", "Alto", "Alto", "Alto", "Alto", "Alto", 
                  "Alto", "Alto", "Alto", "Alto", "Alto", "Alto", "Commando", "Commando", 
                  "Commando", "Commando", "Commando", "Commando", "Commando", "Commando", 
                  "Commando", "Commando", "Commando", "Commando", "Commando", "Commando"
                  ), block = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                  2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                  2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                  2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                  2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), seed_rate = c("12kg", "12kg", 
                  "12kg", "12kg", "12kg", "12kg", "12kg", "12kg", "12kg", "12kg", 
                  "12kg", "12kg", "12kg", "12kg", "18kg", "18kg", "18kg", "18kg", 
                  "18kg", "18kg", "18kg", "18kg", "18kg", "18kg", "18kg", "18kg", 
                  "18kg", "18kg", "12kg", "12kg", "12kg", "12kg", "12kg", "12kg", 
                  "12kg", "12kg", "12kg", "12kg", "12kg", "12kg", "12kg", "12kg", 
                  "12kg", "12kg", "12kg", "12kg", "12kg", "12kg", "12kg", "12kg", 
                  "12kg", "12kg", "12kg", "12kg", "12kg", "12kg", "18kg", "18kg", 
                  "18kg", "18kg", "18kg", "18kg", "18kg", "18kg", "18kg", "18kg", 
                  "18kg", "18kg", "18kg", "18kg", "18kg", "18kg", "18kg", "18kg", 
                  "18kg", "18kg", "18kg", "18kg", "18kg", "18kg", "18kg", "18kg", 
                  "18kg", "18kg")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
                  -84L))

.

ybreaks <- seq(0, 100, 20)
xbreaks <- seq(floor_date(min(data_bot2$date), "years"), 
               ceiling_date(max(data_bot2$date), "years"), by="1 year")

data_bot2 %>% # FIXME not all data getting plotted for some reason??? Text coding??
  split(.$block) %>%
  map(~ggplot(.)  
        labs(x='Date', y='Dry weight %', fill='Species',
             title=paste('Botanical composition (cut to 4cm), Block', unique(.$block)))  
        theme(axis.text.x=element_text(angle=90, vjust=0.5, hjust=1))  
        geom_bar(mapping=aes(x=date, y=fraction, fill=species), stat='identity')  
        scale_fill_brewer(palette='Paired')  
        facet_grid(cultivar ~ seed_rate )  
        scale_x_datetime(breaks=xbreaks, labels=year(xbreaks), limits=c(xbreaks[1], tail(xbreaks, 1)))  
        scale_y_continuous(breaks=ybreaks, limits=c(ybreaks[1], tail(ybreaks, 1)))
  )
#> $`1`
#> Warning: Removed 2 rows containing missing values (geom_bar).

ggplot2 + geom_bar()缺少吧bug - ggplot2 + geom_bar() missing bars bug -开发者知识库,第2张

#> 
#> $`2`
#> Warning: Removed 5 rows containing missing values (geom_bar).

ggplot2 + geom_bar()缺少吧bug - ggplot2 + geom_bar() missing bars bug -开发者知识库,第3张

Created on 2018-07-25 by the reprex package (v0.2.0).

由reprex包創建於2018-07-25(v0.2.0)。

1 个解决方案

#1


0  

I've posted an issue in Github

我在Github發布了一個問題

It seem to be an issue with geom_col and geom_bar, not dates or facets as such. Note the missing bars in this reprex.

它似乎是geom_col和geom_bar的一個問題,而不是日期或方面。請注意此代表中缺少的條形圖。

library(ggplot2)
ggplot(mtcars)  
  geom_col(mapping=aes(x=disp, y=mpg))  
  geom_point(mapping=aes(x=disp, y=mpg))

ggplot2 + geom_bar()缺少吧bug - ggplot2 + geom_bar() missing bars bug -开发者知识库,第4张

Created on 2018-08-08 by the reprex package (v0.2.0).

由reprex包(v0.2.0)於2018-08-08創建。

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复