ggplot2+;geom_bar()正在消失的bar错误

ggplot2+;geom_bar()正在消失的bar错误,r,ggplot2,R,Ggplot2,当我使用purrr::map绘制一系列带有ggplot2::facet_grid的堆叠条形图时,我遇到了一个奇怪的“错误”,并没有显示所有数据(由于某种原因,其中一些数据被丢弃为缺失值),看起来是随机的。这些图中的每个面应具有堆叠到100的条形图 library(tidyverse) #> Warning: package 'ggplot2' was built under R version 3.5.1 #> Warning: package 'dplyr' was built u

当我使用
purrr::map
绘制一系列带有
ggplot2::facet_grid
的堆叠条形图时,我遇到了一个奇怪的“错误”,并没有显示所有数据(由于某种原因,其中一些数据被丢弃为缺失值),看起来是随机的。这些图中的每个面应具有堆叠到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))


由(v0.2.0)于2018年7月25日创建。

如果我删除比例控制,它将绘制所有数据。比例连续限制过于复杂,将使数据略高于100。scale_x_datetime似乎也会删除数据。当只有一个数据点时,scale_x_datetime限制似乎会删除数据。可能是错误。也许在GitHub上提交一个问题?问题似乎是在scale_x_datetime中计算条宽,然后在RStudio中显示的方式。将绘图保存为pdf文件会显示条形图,但它们非常薄。大概是因为datetime以秒为单位,x轴刻度以年为单位。
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).
#> 
#> $`2`
#> Warning: Removed 5 rows containing missing values (geom_bar).