使用 R 在一个图中绘制光谱数据
Plotting spectral data in one plot using R
我有多个数据框,其中第一列(最后用 NA 填充)是波数,其他列是我的多个观察的特定波数的变量。
是否有可能以我的第一列保存 x 轴变量的方式绘制列,而另一列绘制成一个大图及其各自的 y 值?
我已经尝试过“matplot”(结果是“数字”而不是点),
matplot(df[,1],df[,3:5],xlab = "Wavelength [nm]", ylab = "Absorbance")
不同组的“xyplot”(不可能给出超过一个 y 值),但 none 似乎有效(以我对 R 的知识水平)。
最终结果应该是这样的:
感谢您的帮助!
您总是可以创建自己的函数来执行此操作;当没有什么真正适合我的需要时,我会定期创建此类函数。
我很快就把它放在一起了,但你可以根据自己的需要进行调整。
# generate data
set.seed(6)
n <- 50
dat <- data.frame(x1=seq(1,100, length.out = n),
x2=seq(1,20, length.out = n)+rnorm(n),
x3=seq(1,20, length.out = n)+rnorm(n, mean = 3),
x4=seq(1,20, length.out = n)+rnorm(n, mean = 5))
# make some NAs at the end
dat[45:n,2] <- NA
dat[30:n,3] <- NA
plot_multi <- function(df, x=1, y=2, cols=y,
xlim=range(df[,x], na.rm = T),
ylim=range(df[,y], na.rm = T),
main="", xlab="", ylab="", ...){
# setup plot frame
plot(NULL,
xlim=xlim,
ylim=ylim,
main=main, xlab=xlab, ylab=ylab)
# plot all your y's against your x
pb <- sapply(seq_along(y), function(i){
points(df[,c(x, y[i])], col=cols[i], ...)
})
}
plot_multi(dat, y=2:4, type='l', lwd=3, main = ":)",
xlab = "Wavelength", ylab = "Absorbance")
结果:
编辑
我实际上是在网上偶然发现了你的数据集,所以我将包括如何使用我上面的代码绘制它。
file <- 'http://openmv.net/file/tablet-spectra.csv'
spectra <- read.csv(file, header = FALSE)
# remove box label
spectra <- spectra[,-1]
# add the 'wavelength' and rotate the df
# (i didn't find the actual wavelength values, but hey).
spectra <- cbind(1:ncol(spectra), t(spectra))
plot_multi(spectra, y=2:ncol(spectra), cols = rainbow(ncol(spectra)),
type='l', main=":))", ylab="Absorbance", xlab = "'Wavelength'")
您可以使用 pavo R 包,它是用来处理光谱数据的(完全公开,我是维护者之一):
library(pavo)
df <- t(read.csv("http://openmv.net/file/tablet-spectra.csv", header = FALSE))
df <- df[-1, ]
df <- apply(df, 2, as.numeric)
df <- cbind(wl = seq_len(nrow(df)),
df)
df <- as.rspec(df)
#> wavelengths found in column 1
plot(df, ylab = "Absorbance", col = rainbow(3))
由 reprex package (v0.3.0)
于 2019-07-26 创建
我有多个数据框,其中第一列(最后用 NA 填充)是波数,其他列是我的多个观察的特定波数的变量。
是否有可能以我的第一列保存 x 轴变量的方式绘制列,而另一列绘制成一个大图及其各自的 y 值?
我已经尝试过“matplot”(结果是“数字”而不是点),
matplot(df[,1],df[,3:5],xlab = "Wavelength [nm]", ylab = "Absorbance")
不同组的“xyplot”(不可能给出超过一个 y 值),但 none 似乎有效(以我对 R 的知识水平)。
最终结果应该是这样的:
感谢您的帮助!
您总是可以创建自己的函数来执行此操作;当没有什么真正适合我的需要时,我会定期创建此类函数。 我很快就把它放在一起了,但你可以根据自己的需要进行调整。
# generate data
set.seed(6)
n <- 50
dat <- data.frame(x1=seq(1,100, length.out = n),
x2=seq(1,20, length.out = n)+rnorm(n),
x3=seq(1,20, length.out = n)+rnorm(n, mean = 3),
x4=seq(1,20, length.out = n)+rnorm(n, mean = 5))
# make some NAs at the end
dat[45:n,2] <- NA
dat[30:n,3] <- NA
plot_multi <- function(df, x=1, y=2, cols=y,
xlim=range(df[,x], na.rm = T),
ylim=range(df[,y], na.rm = T),
main="", xlab="", ylab="", ...){
# setup plot frame
plot(NULL,
xlim=xlim,
ylim=ylim,
main=main, xlab=xlab, ylab=ylab)
# plot all your y's against your x
pb <- sapply(seq_along(y), function(i){
points(df[,c(x, y[i])], col=cols[i], ...)
})
}
plot_multi(dat, y=2:4, type='l', lwd=3, main = ":)",
xlab = "Wavelength", ylab = "Absorbance")
结果:
编辑
我实际上是在网上偶然发现了你的数据集,所以我将包括如何使用我上面的代码绘制它。
file <- 'http://openmv.net/file/tablet-spectra.csv'
spectra <- read.csv(file, header = FALSE)
# remove box label
spectra <- spectra[,-1]
# add the 'wavelength' and rotate the df
# (i didn't find the actual wavelength values, but hey).
spectra <- cbind(1:ncol(spectra), t(spectra))
plot_multi(spectra, y=2:ncol(spectra), cols = rainbow(ncol(spectra)),
type='l', main=":))", ylab="Absorbance", xlab = "'Wavelength'")
您可以使用 pavo R 包,它是用来处理光谱数据的(完全公开,我是维护者之一):
library(pavo)
df <- t(read.csv("http://openmv.net/file/tablet-spectra.csv", header = FALSE))
df <- df[-1, ]
df <- apply(df, 2, as.numeric)
df <- cbind(wl = seq_len(nrow(df)),
df)
df <- as.rspec(df)
#> wavelengths found in column 1
plot(df, ylab = "Absorbance", col = rainbow(3))
由 reprex package (v0.3.0)
于 2019-07-26 创建