在ggplot2中打包“lomb”

时间:2015-02-17 08:38:16

标签: r ggplot2

我正在使用包裹" lomb"计算Lomb-Scargle Periodograms,一种分析生物时间序列数据的方法。如果您告诉它,该包确实会创建一个图。但是,这些图不是太好了(与ggplot2图相比)。因此,我想用ggplot绘制结果。但是,我不知道如何访问绘制曲线的函数...

这是情节的示例代码:

TempDiff <- runif(4033, 3.0, 18) % just generate random numbers
Time2 <- seq(1,4033) % Time vector
Rand.LombScargle <- randlsp(repeats=10, TempDiff, times = Time2, from = 12, to = 36, 
                        type = c("period"), ofac = 10, alpha = 0.01, plot = T, 
                        trace = T, xlab="period", main = "Lomb-Scargle Periodogram")

我也试图找到一些关于函数randlsp本身的函数,但是找不到任何对我有用的东西......

 getAnywhere(randlsp)
 A single object matching ‘randlsp’ was found
 It was found in the following places
   package:lomb
   namespace:lomb
 with value

 function (repeats = 1000, x, times = NULL, from = NULL, to = NULL, 
 type = c("frequency", "period"), ofac = 1, alpha = 0.01, 
 plot = TRUE, trace = TRUE, ...) 
  {
    if (is.ts(x)) {
    x = as.vector(x)
  }
 if (!is.vector(x)) {
    times <- x[, 1]
    x <- x[, 2]
}
 if (plot == TRUE) {
    op <- par(mfrow = c(2, 1))
}
 realres <- lsp(x, times, from, to, type, ofac, alpha, plot = plot, 
    ...)
 realpeak <- realres$peak
 pks <- NULL
 if (trace == TRUE) 
    cat("Repeats: ")
 for (i in 1:repeats) {
    randx <- sample(x, length(x))
    randres <- lsp(randx, times, from, to, type, ofac, alpha, 
        plot = F)
    pks <- c(pks, randres$peak)
    if (trace == TRUE) {
        if (i/10 == floor(i/10)) 
            cat(i, " ")
    }
}
if (trace == TRUE) 
    cat("\n")
prop <- length(which(pks >= realpeak))
p.value <- prop/repeats
if (plot == TRUE) {
    mx = max(c(pks, realpeak)) * 1.25
    hist(pks, xlab = "Peak Amplitude", xlim = c(0, mx), main = paste("P-value: ", 
        p.value))
    abline(v = realpeak)
    par(op)
}
res = realres[-(8:9)]
res = res[-length(res)]
res$random.peaks = pks
res$repeats = repeats
res$p.value = p.value
class(res) = "randlsp"
return(invisible(res))

任何想法都将受到赞赏!

最佳, 恭

PS:这是一个有真实数据的情节示例。 Plot with real data

1 个答案:

答案 0 :(得分:3)

从任何返回的对象中获取ggplot图表的关键是将您需要的数据转换为某种data.frame。为此,您可以查看返回值的对象类型,并查看可以立即提取到data.frame

的数据类型
str(Rand.LombScargle) # get the data type and structure of the returned value

    List of 12
 $ scanned     : num [1:2241] 12 12 12 12 12 ...
 $ power       : num [1:2241] 0.759 0.645 0.498 0.341 0.198 ...
 $ data        : chr [1:2] "times" "x"
 $ n           : int 4033
 $ type        : chr "period"
 $ ofac        : num 10
 $ n.out       : int 2241
 $ peak        : num 7.25
 $ peak.at     : num [1:2] 24.6908 0.0405
 $ random.peaks: num [1:10] 4.99 9.82 7.03 7.41 5.91 ...
 $ repeats     : num 10
 $ p.value     : num 0.3
 - attr(*, "class")= chr "randlsp"

randlsp的情况下,它是一个列表,通常是统计函数返回的列表。大多数此类信息也可以从?randlsp获得。

看起来好像Rand.LombScargle$scannedRand.LombScargle$power包含第一张图所需的大部分内容:

在Periodogram上还有一条水平线,但它与randlsp返回的任何内容都不对应。查看您提供的源代码,看起来就像lsp()实际生成了Periodogram。

LombScargle <- lsp( TempDiff, times = Time2, from = 12, to = 36, 
    type = c("period"), ofac = 10, alpha = 0.01, plot = F)

str(LombScargle)

List of 12
 $ scanned  : num [1:2241] 12 12 12 12 12 ...
 $ power    : num [1:2241] 0.759 0.645 0.498 0.341 0.198 ...
 $ data     : chr [1:2] "Time2" "TempDiff"
 $ n        : int 4033
 $ type     : chr "period"
 $ ofac     : num 10
 $ n.out    : int 2241
 $ alpha    : num 0.01
 $ sig.level: num 10.7
 $ peak     : num 7.25
 $ peak.at  : num [1:2] 24.6908 0.0405
 $ p.value  : num 0.274
 - attr(*, "class")= chr "lsp"

我猜测,基于这些数据,该行表示显着性水平LombScargle$sig.level

将这些内容放在一起,我们可以创建数据,以便从ggplot传递到lsp

lomb.df <- data.frame(period=LombScargle$scanned, power=LombScargle$power)

# use the data frame to set up the line plot
g <- ggplot(lomb.df, aes(period, power)) + geom_line() + 
       labs(y="normalised power", title="Lomb-Scargle Periodogram")

# add the sig.level horizontal line
g + geom_hline(yintercept=LombScargle$sig.level, linetype="dashed")

Periodogram

对于直方图,它看起来像是基于来自Rand.LombScargle$random.peaks的向量randlsp

rpeaks.df <- data.frame(peaks=Rand.LombScargle$random.peaks)

ggplot(rpeaks.df, aes(peaks)) + 
       geom_histogram(binwidth=1, fill="white", colour="black") + 
       geom_vline(xintercept=Rand.LombScargle$peak, linetype="dashed") +
       xlim(c(0,12)) +  
       labs(title=paste0("P-value: ", Rand.LombScargle$p.value), 
           x="Peak Amplitude", 
           y="Frequency")

Histogram

使用这些图表让他们看起来符合您的口味。