【问题标题】:Calculating FWHM from spectra in R从 R 中的光谱计算 FWHM
【发布时间】:2023-02-15 19:17:45
【问题描述】:

我正在尝试找到一种使用 R 编程计算光谱峰 FWHM 的方法。

以下数据集用于获取其中一个峰值:

theta <- c(32.1, 32.2, 32.3,32.4,32.5, 32.6, 32.7, 32.8, 32.9, 33.0, 33.1)
intensity <- c(0, 0, 18, 138, 405, 449, 187, 29, 2, 0, 0)

Geom_line 是我选择的绘制方式。使用的代码:

pacman::p_load(pacman, ggplot2, dplyr, tidyverse, svglite)
DataOne <- data.frame(thetaOne, intensityOne)
view (DataOne)
PlotOne <- ggplot(data = DataOne) +
  geom_line(aes(x = thetaOne, y = intensityOne))
PlotOne

我的愿望是在绘制光谱后,我可以计算每个光谱中峰的半峰全宽(峰高一半处的宽度)。 我使用的数据集比上面提供的数据集大得多。如果可能的话,我想要一个允许我选择峰值开始和结束的 x 轴区域的函数。

我发现了这个:Determining FWHM from a distribution in R。 但是,它似乎已经过时了,已经有将近 5 年的历史了。另外,我不明白他们提出的解决方案是什么。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    给定峰值曲线的输入 x、y 值,这个小函数将返回 FWHM 的 x、y 坐标的数据框:

    fwhm <- function(x, y) {
      peak <- which.max(y)
      pre  <- seq(peak)
      post <- peak:length(x)
      half <- y[peak]/2
      xvals <- suppressWarnings(c(approx(y[pre], x[pre], xout = half)$y, 
                                  approx(y[post], x[post], xout = half)$y))
      
      ss <- which(x > min(xvals) & x < max(xvals))
      data.frame(x = c(min(xvals), x[ss], max(xvals)),
                 y = c(half, y[ss], half))
    }
    

    要在您的情况下使用它,我们可以这样做:

    half_width <- fwhm(DataOne$thetaOne, DataOne$intensityOne)
    

    我们可以通过以下方式获得半最大值宽度的 x 值:

    range(half_width$x)
    #> [1] 32.43240 32.68569
    

    并绘制它可以像这样完成:

    ggplot(data = DataOne, aes(x = thetaOne, y = intensityOne)) +
      geom_line() +
      geom_area(data = half_width, aes(x, y), fill = "red", alpha = 0.3) +
      theme_minimal(base_size = 16)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-05
      • 2018-08-12
      • 1970-01-01
      • 1970-01-01
      • 2019-05-18
      • 1970-01-01
      • 2018-10-03
      • 2013-04-30
      相关资源
      最近更新 更多