Introduction
The vignette benchmarks plotjs against various other
plotting systems for R, namely plotly. Two basic plots will
be used for this benchmark, a basic scatter plot and a grouped line
plot. I am uncertain if JavaScript execution time is counted by
microbenchmark. Even if we assume it isn’t, the benchmark
results are informative because it’s always better if the R process gets
tied up for less time per plot. First, let’s load the visualization
packages to compare:
library(plotjs)
library(plotly)
#> Loading required package: ggplot2
#>
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#>
#> last_plot
#> The following object is masked from 'package:stats':
#>
#> filter
#> The following object is masked from 'package:graphics':
#>
#> layout
library(ggplot2)Scatter Plot
First, we will benchmark the creation of simple scatter plots using
data from the gapminder package. To begin, we will define
functions to create similar scatterplots using each of the packages to
be compared. The plots themselves are not important but are shown to
demonstrate that they work and produce roughly similar plots.
library(gapminder)
gapminder <- gapminder
plot_base <- function(x){
plot(x = x$gdpPercap, y = x$lifeExp)
}
plot_base(gapminder)
plot_plotjs <- function(x){
plotjs(x = x$gdpPercap, y = x$lifeExp, sci.x = TRUE)
}
plot_plotjs(gapminder)
plot_plotly <- function(x){
plot_ly(data = x, x = ~gdpPercap, y = ~lifeExp, type = "scatter")
}
plot_plotly(gapminder)
#> No scatter mode specifed:
#> Setting the mode to markers
#> Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
plot_ggplotly <- function(x){
g <- ggplot(x, aes(x = gdpPercap, y = lifeExp)) + geom_point() + theme_minimal()
ggplotly(g)
}
plot_ggplotly(gapminder)
plot_ggplot <- function(x){
ggplot(x, aes(x = gdpPercap, y = lifeExp)) + geom_point() + theme_minimal()
}
plot_ggplot(gapminder)
Now, these functions are benchmarked:
library(microbenchmark)
m <- microbenchmark(base = plot_base(gapminder),
plotjs = plot_plotjs(gapminder),
plotly = plot_plotly(gapminder),
ggplotly = plot_ggplotly(gapminder),
ggplot = plot_ggplot(gapminder),
unit = "ms",
times = 50)
m
#> Unit: milliseconds
#> expr min lq mean median uq max
#> base 24.243720 61.029375 60.7712529 61.3285775 61.721425 64.879847
#> plotjs 0.076883 0.114964 0.1599492 0.1313800 0.150151 1.590076
#> plotly 0.331369 0.406007 0.5553723 0.5453075 0.641436 2.216734
#> ggplotly 163.537707 170.994476 179.6019770 181.5625975 184.671927 204.551888
#> ggplot 33.788794 37.762696 42.2488308 39.3883040 42.950153 152.003613
#> neval
#> 50
#> 50
#> 50
#> 50
#> 50On my main development machine, plotjs was the quickest by an order
of magnitude. This can vary, but plotly is roughly 20 times
slower, and ggplotly() is hundreds of times slower.
However, plotly was still quick enough that the performance
difference with plotjs would be imperceptible to users.
plot(m)
Let’s look at kernel density plots of the time distributions for
plotjs and plotly.
Let’s use a two-sample Wilcoxon test to compare the means of
execution time for plotjs and plotly. A t-test would not be suitable
because we cannot assume normality. The null hypothesis is that
plotjs and plotly will have the same mean
execution time for these scatter plots.
w <- wilcox.test(m$time[m$expr == "plotjs"],
m$time[m$expr == "plotly"],
alternative = "less",
paired = FALSE)
w
#>
#> Wilcoxon rank sum test with continuity correction
#>
#> data: m$time[m$expr == "plotjs"] and m$time[m$expr == "plotly"]
#> W = 49, p-value < 2.2e-16
#> alternative hypothesis: true location shift is less than 0Can we reject the null hypothesis?
ifelse(w$p.value < .05, "yes", "no")
#> [1] "yes"Grouped Line plots
Making line plots colored by group is a common plotting task that
could potentially expose some slowness in plotjs. We will
make line plots of the total GDP by continent by year. First, we must
summarize the data and define functions for making this lineplot with
various packages.
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
gdp_cont <- gapminder %>%
mutate(gdp = pop * gdpPercap) %>%
group_by(continent, year) %>%
summarize(total_gdp = sum(gdp))
#> `summarise()` has regrouped the output.
#> ℹ Summaries were computed grouped by continent and year.
#> ℹ Output is grouped by continent.
#> ℹ Use `summarise(.groups = "drop_last")` to silence this message.
#> ℹ Use `summarise(.by = c(continent, year))` for per-operation grouping
#> (`?dplyr::dplyr_by`) instead.
plot_title <- "Total GDP by Continent 1952 - 2007"
plotjs_line <- function(x){
plotjs(x$year, x$total_gdp, col.group = x$continent, sci.y = TRUE,
type = "l", main = plot_title, xlab = "Year", ylab = "GDP",
legend.title = "Continent")
}
plotjs_line(gdp_cont)
ggplot_line <- function(x){
ggplot(x, aes(x = year, y = total_gdp, col = continent, group = continent)) +
geom_line() +
theme_minimal() +
labs(title = plot_title, x = "Year", y = "GDP")
}
ggplot_line(gdp_cont)
ggplotly_line <- function(x){
p <- ggplot(x, aes(x = year, y = total_gdp, col = continent)) +
geom_line() +
theme_minimal() +
labs(title = plot_title, x = "Year", y = "GDP")
ggplotly(p)
}
ggplotly_line(gdp_cont)
plotly_line <- function(x){
plot_ly(data = x, x = ~year, y = ~total_gdp, split = ~continent,
type = "scatter", color = ~continent, mode = "lines")
}
plotly_line(gdp_cont)Now let’s benchmark these line plot functions:
m2 <- microbenchmark(plotjs = plotjs_line(gdp_cont),
ggplotly = ggplotly_line(gdp_cont),
plotly = plotly_line(gdp_cont),
ggplot = ggplot_line(gdp_cont),
unit = "ms",
times = 50)
m2
#> Unit: milliseconds
#> expr min lq mean median uq max
#> plotjs 0.440342 0.482269 0.6078713 0.5795355 0.630937 2.655834
#> ggplotly 173.114470 178.409165 184.1830153 180.2388540 183.813737 330.256258
#> plotly 0.343531 0.388084 0.5099320 0.4410925 0.559394 2.358809
#> ggplot 35.238107 36.346625 38.3464542 37.2546840 40.530920 43.770543
#> neval
#> 50
#> 50
#> 50
#> 50
plot(m2)
Let’s perform the same test as before:
w2 <- wilcox.test(m2$time[m2$expr == "plotjs"],
m2$time[m2$expr == "plotly"],
alternative = "less",
paired = FALSE)
w2
#>
#> Wilcoxon rank sum test with continuity correction
#>
#> data: m2$time[m2$expr == "plotjs"] and m2$time[m2$expr == "plotly"]
#> W = 1941, p-value = 1
#> alternative hypothesis: true location shift is less than 0Can we reject the null hypothesis that plotjs and plotly have the same mean?
ifelse(w2$p.value < .05, "yes", "no")
#> [1] "no"Conclusions
Although benchmark results will vary on different systems, the results on my development machine indicate that plotjs is faster than plotly (and others) for both the scatter plot and grouped line plot tested. Although statistically significant, the difference in performance between plotjs and plotly would almost certainly never be perceptible to users.
Both plotjs and direct use of plotly potentially offer perceptible
performance improvements over using ggplotly() to generate
interactive visualizations. Shiny developers may find this information
useful.