ggplot一个带有矩阵变量的 Dataframe ?

kqlmhetl  于 2022-12-06  发布在  其他
关注(0)|答案(3)|浏览(272)

我正在尝试使用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数据集是一个棘手的数据集:两个变量中有一个是我从未遇到过的矩阵。2我该如何清理这个数据集以使其整洁?3我尝试了以下方法:

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)
njthzxwz

njthzxwz1#

@user7264给出的答案是正确的,只是在添加新变量 wavelength 时有一点错误,它应该是一个数值变量,而不是一个字符。因此,考虑到这个响应和颜色问题,我建议的答案如下:

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()

这就产生了这个图。

我希望这是有用的!😃

kt06eoxx

kt06eoxx2#

这是非常接近的。如果你关心特定的颜色,你可以使用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()

iyfamqjs

iyfamqjs3#

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))

相关问题