0

我正在尝试使用 tm 包中的 Zipf_plot 函数来比较两个不同的文档术语矩阵 - 而且我不是 R 专家.. 也许你可以告诉我,如果有一种方法可以同时适应这两个函数?

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

我知道,有可能将它们中的两个(或更多)放在一个窗口中:

par(mfrow=c())

但我真的很感激在一个图中有两个或多个 dtms 的解决方案。

提前致谢!:-)

4

1 回答 1

2

您可以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))

在此处输入图像描述

于 2017-05-10T11:57:10.803 回答