Is there a way to produce facetted bar plots as rapidly as you can produce facetted box plots?
For instance this code will produce box plots facetted by a grouping variable:
library(tidyverse)
mtcars %>%
ggplot(aes(x = am, y = mpg)) +
geom_boxplot() +
facet_grid(~ cyl)
But the "same" code for a bar plot fails because geom_col()
expects to have means provided, which would take considerably more time to calculate:
mtcars %>%
ggplot(aes(x = am, y = mpg)) +
geom_col() +
facet_grid(~ cyl)
If there is a way to produce facetted bar plots at the speed of facetted box plots, I'd love to hear your methods.
There seems to be no notable difference between those two plots
library(tidyverse)
box_plot <- function() mtcars %>%
ggplot(aes(x = am, y = mpg)) +
geom_boxplot() +
facet_grid(~ cyl)
bar_plot <- function() mtcars %>%
ggplot(aes(x = am, y = mpg)) +
geom_col() +
facet_grid(~ cyl)
library(microbenchmark)
mb <- microbenchmark(box_plot(), bar_plot())
mb
# Unit: milliseconds
# expr min lq mean median uq max neval cld
# box_plot() 2.412589 2.431660 2.538586 2.475522 2.534515 9.966422 1000 a
# bar_plot() 2.543288 2.566173 2.670544 2.609146 2.671571 5.254653 1000 b
autoplot(mb)
A similar test with the full diamonds dataset yields the same result. I think the bottleneck is the graphical device that needs a lot of time to display a ggplot object.
box_p <- box_plot()
bar_p <- bar_plot()
# !!! this will need about 2 minutes to execute !!!
mb2 <- microbenchmark(plot(box_p), plot(bar_p), times = 100)
mb2
# Unit: milliseconds
# expr min lq mean median uq max neval cld
# plot(box_p) 405.0375 513.7360 532.1827 524.1259 536.8206 740.7494 100 b
# plot(bar_p) 454.6047 469.3411 489.3676 479.3709 497.9246 717.7909 100 a
autoplot(mb2)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.