Zipf_plot() : 如何在一张图中比较两个对象?

Zipf_plot() : How to compare two objects in one graph?

我正在尝试使用 tm 包中的 Zipf_plot 函数来比较两个不同的文档术语矩阵 - 我不是 R 专家.. 也许你可以告诉我,是否有办法同时满足此功能?

Zipf_plot(x, type = "l", ... )

我知道,有可能将两者(或更多)合二为一window:

par(mfrow=c())

但我非常感谢在一张图中包含两个或更多 dtms 的解决方案。

提前致谢! :-)

您可以尝试par(new=T)或尝试根据您的需要调整功能,例如:

library(tm)
data("acq")
data("crude")
m1 <- DocumentTermMatrix(acq)
m2 <- DocumentTermMatrix(crude)
Zipf_plot(m1, col = "red")
par(new=T)
Zipf_plot(m2, col="blue")
Zipf_plot_multi <- function (xx, type = "l", cols = rainbow(length(xx)), ...) {
    stopifnot(is.list(xx) & length(xx)==length(cols))
    for (idx in seq_along(xx)) {
      x <- xx[[idx]]
      if (inherits(x, "TermDocumentMatrix")) 
          x <- t(x)
      y <- log(sort(slam::col_sums(x), decreasing = TRUE))
      x <- log(seq_along(y))
      m <- lm(y ~ x)
      dots <- list(...)
      if (is.null(dots$xlab)) 
          dots$xlab <- "log(rank)"
      if (is.null(dots$ylab)) 
          dots$ylab <- "log(frequency)"
      if (idx==1) {
        do.call(plot, c(list(x, y, type = type, col = cols[idx]), dots))
      } else {
        lines(x, y, col = cols[idx])
      }
      abline(m, col = cols[idx], lty = "dotted")
      print(coef(m))
    }
}
Zipf_plot_multi(list(m1, m2), xlim=c(0, 7), ylim=c(0,6))