使用alpha来减少R包ggplot2中的过度绘图时,运行时明显膨胀

我正在尝试显示一组中等大小的数据,using (var db = _stagingConnectionFactory.Open()) { var query = db.From<tblactivityImportCalculationQueue>(); query = query.Where("DATEDIFF(minute,LastUpdated,GETDATE()) > 30"); query.ToMergedParamsSelectStatement(); } 。为了减少过度绘图,我使用了cshtml: @(Html.Kendo().Grid<Debtors>() .Name("Debtors") .Columns(columns => { columns.Bound(c => c).Title("Name").ClientTemplate("#=showName(data)#"); columns.Bound(c => c.Busname); ... }) ... ) js: function showName(data) { var returnName = ""; if (data.Lname) { returnName = data.Lname; if (data.Fname) { returnName += "," + data.Fname; if (data.Mi) { returnName += " " + data.Mi; } } } return returnName; } 。这大大减慢了R生成图形所需的时间。这是我的规格,

nrow(df)=7810

这是正在发生的事情的一个例子,

alpha=.3

有人知道这个脚本的运行速度变慢了吗?

alfud0324 回答:使用alpha来减少R包ggplot2中的过度绘图时,运行时明显膨胀

仅Alpha值与减速无关。 Alpha值与形状相结合似乎会减慢速度。

shape = 4渲染的“ x”之类的复杂矢量形状在与alpha值一起使用时似乎显着减慢了渲染时间。如果您不致力于shape = 4,则使用shape = 16之类的东西可以加快速度,同时使用所需的alpha值。下面的示例:

library(dplyr)
library(ggplot2)
df <- tibble(x = rnorm(n = 7810),y = rnorm(n = 7810))

p1 <- function() {
  p <- ggplot(df) +
    geom_point(aes(x,y),shape=4,size=.5)
  print(p)
}

p2 <- function() {
  p <- ggplot(df) +
    geom_point(aes(x,size=.5,alpha = 0.3)
  print(p)
}

p3 <- function() {
  p <- ggplot(df) +
    geom_point(aes(x,shape=16,alpha = 0.3)
  print(p)
}

p4 <- function() {
  p <- ggplot(df) +
    geom_point(aes(x,shape=22,alpha = 0.3)
  print(p)
}

test <- microbenchmark::microbenchmark(no_alpha = p1(),alpha = p2(),alpha_circle = p3(),alpha_square = p4(),times = 10)

print(test)
Unit: milliseconds
         expr        min         lq       mean     median         uq       max neval
     no_alpha   837.5163   851.7994  1025.0569   910.3687  1173.8753  1403.087    10
        alpha 41456.3393 41708.0781 45831.6033 42589.4998 45219.8180 59578.347    10
 alpha_circle   429.7718   536.9076   719.5507   549.7952   555.9002  1780.282    10
 alpha_square   800.1380   806.5523   882.0163   815.6232   842.4669  1450.395    10

编辑:

我们可以使用microbenchmarkpurrr来查看哪些形状导致最快的绘图时间。

library(purrr)
library(microbenchmark)

df <- tibble(x = rnorm(n = 7810),y = rnorm(n = 7810))

s <- tibble(shape = c(0:24))

plot_fun <- function(shape) {
  p <- ggplot(df) +
    geom_point(aes(x,shape = shape,alpha = 0.3)
  print(p)
}


test_fun <- function(shape) {
  microbenchmark(plot_fun(shape = shape),times = 10)
}

s <- s %>%
  mutate(test = map(.$shape,~test_fun(shape = .x)))

s %>% 
  tidyr::unnest(test) %>%
  mutate(time = microbenchmark:::convert_to_unit(time,"ms")) %>%
  ggplot() +
  geom_boxplot(aes(x = shape,y = time,group = shape),outlier.shape = NA) +
  scale_x_continuous(breaks = c(0:24)) +
  scale_y_log10() +
  coord_flip()

timing

看起来形状值0、1和15到22提供的绘制时间比其余值要快。

本文链接:https://www.f2er.com/3062753.html

大家都在问