【发布时间】:2015-09-17 23:37:10
【问题描述】:
我需要使用 R 中的 ggplot2(或 lattice)包绘制一个光谱数据矩阵,其中行由 2 个因子变量分组,因为它具有分面功能。
考虑使用来自pls 包的DS$NIR 光谱数据(矩阵)数据框DS:
library(pls)
data(gasoline)
DS <-gasoline
让我们添加一些分组变量:
set.seed(0)
DS$Type <- as.factor(sample(c("Training set","Validation set","Others"),
nrow(DS),
replace = TRUE))
DS$Group <- cut(DS$octane,
breaks = c(80,86,88,90),
labels = c("Low","Medium","High"))
并查看数据:
str(DS)
'data.frame': 60 obs. of 4 variables:
$ octane: num 85.3 85.2 88.5 83.4 87.9 ...
$ NIR : AsIs [1:60, 1:401] -0.050193 -0.044227 -0.046867 -0.046705 -0.050859 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr "1" "2" "3" "4" ...
.. ..$ : chr "900 nm" "902 nm" "904 nm" "906 nm" ...
$ Type : Factor w/ 3 levels "Others","Training set",..: 1 2 3 3 1 2 1 1 3 3 ...
$ Group : Factor w/ 3 levels "Low","Medium",..: 1 1 3 1 2 1 3 3 3 3 ...
我需要将DS$NIR 的每一行绘制为单独的一行。 X 轴值可以通过以下方式提取:
x <- as.numeric(gsub(" nm", "", dimnames(DS$NIR)[[2]]))
- 线条颜色应取决于因子
Group的水平。 - 线条应该是半透明的。
- 每个颜色组(即因子
Group的每个级别)都应该有一条不透明的实线,表示该组的平均值(或中位数)。 - 因子
Type的每个级别都应绘制在一个单独的方面。
我找到了example,它是如何绘制光谱数据的,但我很难理解并根据我的情况调整代码。
【问题讨论】:
-
看看 hyperSpec 包,它在小插图中有很多详细的示例