我试图使用hyperSpec
软件包绘制多面板平均反射光谱及其标准偏差。但是我没有得到预期的结果。我有10个群集/组,但绘图仅给了我4个群集。另外,各组的顺序也不正确。我正在使用以下代码
library(hyperSpec)
data_1 <- read.csv("Multipanel_plotting.csv")
data <- data[-1]
wl <- seq (350, 2500,1)
cluster <- as.factor(data_1$clusters)
spectra <- new ("hyperSpec", spc = data, data = data.frame(cluster),
labels = list (.wavelength = "Wavelength (nm)",
spc = "Reflactance"), wavelength = wl)
#Multipanel plotting
qplotspc(aggregate(spectra, spectra$cluster, mean_pm_sd),
mapping = aes(x = .wavelength,
y = spc,
colour = cluster)) +
facet_grid(cluster ~.) + ggtitle("Spectra")
但是我想要这样的情节
我正在提供数据集的Google驱动器链接 https://drive.google.com/file/d/1I_VlNbFTwi10fn3yNE4HknhpjreZKotV/view?usp=sharing
在这方面的任何帮助都将受到高度赞赏。
答案 0 :(得分:0)
我可以使用以下代码自行解决
library(varhandle)
library(dplyr)
library(tidyr)
library(stringr)
library(Rmisc)
library(ggplot2)
library(forcats)
data_1 <- read.csv("Multipanel_Spectra_plotting.csv")
df1=gather(data_1,"Wavelengths","Values",-clusters)
head(df1)
df2=cbind(df1,str_split_fixed(df1$Wavelengths,"_",2))
head(df2)
colnames(df2)[4:5]=c("WL","nm")
head(df2)
str(df1)
df4=summarySE(df2, measurevar="Values", groupvars=c("Wavelengths","clusters", "nm"))
head(df4)
#For arranging the wavelengths
df4 <- df4 %>%
arrange(unfactor(df4$nm)) %>% # rearrange the df in the order we want (1,2,3,4,5)
mutate(Wavelengths = factor(Wavelengths)) # this line reorders the factor in the same order
head(df4)
ggplot(df4, aes(sort(unfactor(nm)), Values, group = clusters, color = clusters)) + geom_line() +
facet_grid(clusters~ .) + scale_x_continuous(breaks=seq(350,2500,430),labels=c("350","780","1210","1640","2070","2500")) +
labs(title = "Spectra", x = "Wavelength (nm)", y = "Reflactance")+
geom_ribbon(aes(ymax = Values + sd, ymin = Values - sd),
alpha = 0.5,
fill = "black"
) + theme_bw()
但是现在唯一的问题是情节的顺序,情节的顺序应该是1、2、3,...,10。