ggplot2::facet_grid of geom_bar not showing all data when used with purrr::map

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


ggplot2::facet_grid of geom_bar not showing all data when used with purrr::map



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


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




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





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









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to scale/resize CVPixelBufferRef in objective C, iOS

Stripe::AuthenticationError No API key provided. Set your API key using “Stripe.api_key = ”

SVG with two text elements. When one resizes due to textLength - how to resize the other one to the same character size