I have a numeric matrix, about 10M values and need just to show the distribution of values in a histogram. In base R, hist() does this quite fast. But if I want to use ggplot, it's much slower (I also have to melt the matrix first, but it's not the time-limiting step). Is there any way to make it fast with ggplot?
require(microbenchmark)
require(ggplot2)
mtx1 <- matrix(rnorm(6e4*150), nrow = 6e4)
df1 <- reshape2::melt(mtx1)
g_hist <- function(df){
print(ggplot(df, aes(x=value)) + geom_histogram(bins=30))
}
print(microbenchmark(
hist(mtx1),
g_hist(df1),
times=3L
), signif=3)
# Unit: milliseconds
# expr min lq mean median uq max neval
# hist(mtx1) 384 471 530 559 603 647 3
# g_hist(df1) 7710 8000 8190 8300 8440 8570 3
