我试着用ggplot复制这个地块
pacman::p_load(tidyverse, pls, remotes)
install_github("rwehrens/ChemometricsWithR")
data(gasoline)
wavelengths <- seq(900,1700, 2)
matplot(wavelengths, t(gasoline$NIR), type = "l", lty = 1, xlab = "Wavelength (nm)", ylab = "1/R")

但似乎无法让它发挥作用。gasoline数据集是一个棘手的数据集:这两个变量中的一个是我以前从未遇到过的矩阵。如何清理此数据集以使其整洁?我尝试了以下几点:
gasoline2 <- as.data.frame(as.matrix(gasoline)) %>%
pivot_longer(cols = -c(octane),
names_to = "wavelength",
values_to = "1/R") 但在这个代码看来是不可能的:
ggplot(gasoline, mapping = aes(x = wavelengths, y = t(gasoline$NIR)))+
geom_line(mapping = aes(color = octane))返回此错误:
Error in `geom_line()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 1st layer.
Caused by error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (60)
✖ Fix the following mappings: `x` and `y`
Backtrace:
1. base (local) `<fn>`(x)
2. ggplot2:::print.ggplot(x)
4. ggplot2:::ggplot_build.ggplot(x)
5. ggplot2:::by_layer(...)
12. ggplot2 (local) f(l = layers[[i]], d = data[[i]])
13. l$compute_aesthetics(d, plot)
14. ggplot2 (local) compute_aesthetics(..., self = self)
15. ggplot2:::check_aesthetics(evaled, n)发布于 2022-11-30 20:11:43
这是相当接近。如果您关心特定的颜色,您可以使用scale_color_manual()或其他一些scale_color_*()函数。
#give every octane a unique ID for grouping later on
gasoline <-
gasoline |>
mutate(group = 1:n())
#colbind matrix-column as a dataframe
gasoline2 <-
bind_cols(
gasoline |> select(octane, group),
gasoline |> pull(NIR)
) |>
# convert colnames to numeric wavelengths
pivot_longer(
cols = c(-octane, -group),
names_to = "wavelength",
values_to = "1/R",
names_pattern = "(\\d+)",
names_transform = as.numeric
) |>
# octane as factor for line colors
mutate(octane = as.factor(octane))
ggplot(gasoline2,
#group aesthetic to plot separate lines for repeat values of octane
aes(x = wavelength, y = `1/R`, color = octane, group = group)) +
geom_line(size = .7) +
scale_color_discrete(guide = "none") +
theme_classic()

发布于 2022-11-30 19:55:30
@user7264提供的答案是正确的,尽管在添加新的可变波长时有一个小错误。它应该是一个数值变量,而不是一个字符。因此,考虑到这一反应和颜色问题,我建议的答案如下:
pacman::p_load(tidyverse, pls, remotes)
install_github("rwehrens/ChemometricsWithR")
gasoline2 <- as.data.frame(as.matrix(gasoline)) %>%
pivot_longer(cols = -c(octane),
names_to = "wavelength",
values_to = "1/R") %>%
# From the above code of @user7264, add as.numeric()
mutate(wavelength = as.numeric(str_remove_all(wavelength, "[^[:digit:]]")))
ggplot(gasoline2, mapping = aes(x = wavelength, y = `1/R`)) +
geom_line(mapping = aes(color = octane)) +
scale_x_continuous(breaks = seq(1000, 2000, 200)) +
scale_colour_continuous(type = "viridis") +
theme_bw()这导致了这场阴谋。

我希望这是有用的!
发布于 2022-11-30 18:47:35
pacman::p_load(tidyverse, pls, remotes)
install_github("rwehrens/ChemometricsWithR")
gasoline2 <- as.data.frame(as.matrix(gasoline)) %>%
pivot_longer(cols = -c(octane),
names_to = "wavelength",
values_to = "1/R") %>%
mutate(wavelength = str_remove_all(wavelength, "[^[:digit:]]"))
ggplot(gasoline2, mapping = aes(x = wavelength, y = `1/R`))+
geom_line(mapping = aes(color = octane))

**但是,我无法找到与颜色匹配的scale_color_gradient语法
编辑:感谢前面的两张海报,这是我最后的复制品!
gasoline2 <- as.data.frame(as.matrix(gasoline)) %>%
mutate(group = row_number()) %>%
relocate(group, .after = octane) %>%
pivot_longer(cols = -c(octane, group),
names_to = "wavelength",
values_to = "spectra") %>%
mutate(wavelength = as.numeric(str_remove_all(wavelength, "[^[:digit:]]"))) %>%
mutate(octane = as.factor(octane))
ggplot(gasoline2, mapping = aes(x = wavelength, y = `spectra`, color = octane, group = group))+
geom_line(linewidth = 0.5)+
scale_color_discrete(guide = "none")+
xlab(label = "\nWavelength (nm)")+
ylab(label = "1/R\n")+
theme_classic()+
scale_y_continuous(n.breaks = 7)+
scale_x_continuous(breaks = seq(1000,1600,200))

https://stackoverflow.com/questions/74632789
复制相似问题