【问题标题】:How can I make this ggplot render more quickly?我怎样才能让这个 ggplot 渲染得更快?
【发布时间】:2021-07-09 03:02:14
【问题描述】:

以下是我正在使用的数据的代表。 geom_segment 调用使渲染非常缓慢。是否有其他方法可以更快地达到相同的结果?

library(ggplot2)
library(ggridges)

n <- 5000; l <- c(2, 5, 7, 9); sd_27 <- c(5.9, 11, 14, 17)
df <- data.frame(name = c(rep("A", n), rep("B", n), 
                          rep("C", n), rep("D", n)),
                 value = c(rpois(n, l[1]), rpois(n, l[2]),
                           rpois(n, l[3]), rpois(n, l[4])))

ggplot(df, aes(x = value, y = name, fill = name)) + geom_density_ridges(alpha = 0.8) +
  geom_segment(aes(x = l[[1]], y = "A", xend = l[[1]], yend = 2, color = "mean")) +
  geom_segment(aes(x = l[[2]], y = "B", xend = l[[2]], yend = 3, color = "mean")) +
  geom_segment(aes(x = l[[3]], y = "C", xend = l[[3]], yend = 4, color = "mean")) +
  geom_segment(aes(x = l[[4]], y = "D", xend = l[[4]], yend = 5, color = "mean")) +
  geom_segment(aes(x = sd_27[[1]], y = "A", xend = sd_27[[1]], yend = 2, color = "sd_27")) +
  geom_segment(aes(x = sd_27[[2]], y = "B", xend = sd_27[[2]], yend = 3, color = "sd_27")) +
  geom_segment(aes(x = sd_27[[3]], y = "C", xend = sd_27[[3]], yend = 4, color = "sd_27")) +
  geom_segment(aes(x = sd_27[[4]], y = "D", xend = sd_27[[4]], yend = 5, color = "sd_27"))

【问题讨论】:

    标签: r ggplot2 ggridges geom-segment


    【解决方案1】:

    不是通过单独的geom_segment 层添加每个段,您可以将段的所有数据放在一个数据帧中,并通过一个 geom_segment 添加段,根据microbenchmark 将渲染时间减少到大约一第五:

    geom_segment

    library(ggplot2)
    library(ggridges)
    
    set.seed(42)
    
    n <- 5000; l <- c(2, 5, 7, 9); sd_27 <- c(5.9, 11, 14, 17)
    df <- data.frame(name = c(rep("A", n), rep("B", n), 
                              rep("C", n), rep("D", n)),
                     value = c(rpois(n, l[1]), rpois(n, l[2]),
                               rpois(n, l[3]), rpois(n, l[4])))
    
    dl <- data.frame(x = l, y = LETTERS[1:4], yend = 2:5, color = "mean")
    dsd <- data.frame(x = sd_27, y = LETTERS[1:4], yend = 2:5, color = "sd_27")
    
    d <- do.call(rbind, list(dl, dsd))
    
    p1 <- function() {
      ggplot(df, aes(x = value, y = name, fill = name)) + 
        geom_density_ridges(alpha = 0.8) +
        geom_segment(data = d, aes(x = x, y = y, xend = x, yend = yend, color = color), inherit.aes = FALSE)
    }
    
    p2 <- function() {
      ggplot(df, aes(x = value, y = name, fill = name)) + geom_density_ridges(alpha = 0.8) +
        geom_segment(aes(x = l[[1]], y = "A", xend = l[[1]], yend = 2, color = "mean")) +
        geom_segment(aes(x = l[[2]], y = "B", xend = l[[2]], yend = 3, color = "mean")) +
        geom_segment(aes(x = l[[3]], y = "C", xend = l[[3]], yend = 4, color = "mean")) +
        geom_segment(aes(x = l[[4]], y = "D", xend = l[[4]], yend = 5, color = "mean")) +
        geom_segment(aes(x = sd_27[[1]], y = "A", xend = sd_27[[1]], yend = 2, color = "sd_27")) +
        geom_segment(aes(x = sd_27[[2]], y = "B", xend = sd_27[[2]], yend = 3, color = "sd_27")) +
        geom_segment(aes(x = sd_27[[3]], y = "C", xend = sd_27[[3]], yend = 4, color = "sd_27")) +
        geom_segment(aes(x = sd_27[[4]], y = "D", xend = sd_27[[4]], yend = 5, color = "sd_27"))
    }
    
    # Check plot
    p1()
    #> Picking joint bandwidth of 0.381
    

    # Compare running time
    microbenchmark::microbenchmark(p1()) 
    #> Unit: milliseconds
    #>  expr      min       lq     mean   median      uq      max neval
    #>  p1() 1.859514 1.917135 2.162416 1.936781 2.42122 5.056147   100
    microbenchmark::microbenchmark(p2())
    #> Unit: milliseconds
    #>  expr     min       lq     mean   median       uq      max neval
    #>  p2() 9.37298 9.669749 10.20821 9.774624 10.17852 22.42459   100
    

    【讨论】:

      猜你喜欢
      • 2021-02-17
      • 2019-12-05
      • 2015-11-22
      • 1970-01-01
      • 2011-06-14
      • 1970-01-01
      • 1970-01-01
      • 2011-07-20
      • 1970-01-01
      相关资源
      最近更新 更多