2

我有一个以迭代方式获得的残差矩阵;每列对应于某个单元的残差,每一行对应于过程的步骤。我想制作一个线图,显示每个单元的残差在不同迭代中的轨迹。此外,我想在每个轨迹的末尾添加一个文本标签,显示相应的单元。

我写的代码如下

# extract the residuals as a data frame 
res <- data.frame(Fout$Fresid)

# add a clumn that indicates the Forward step

# obtain the number of observations in the series and the number of Forward 
steps
 n <- ncol(res)
totstep = nrow(res)

# define a variable that indicates the step of the forward search
Fstep = seq(1:totstep)

#scale the residuals
res <- res/sqrt(Fout$Fsigma2[totstep])

res = cbind(Fstep,res)

# assign column names to the data frame
unitlabel = sprintf("r%03d", 1:n)
colnames(res) = c('idxSearch', unitlabel)

# threshold values
q1 = qnorm(0.99)

# reshape data
plot_res <- melt(res, id.var= 'idxSearch', variable.names = 
colnames(res[,-1]))

# plot the data
FresP <- ggplot(data = plot_res, aes(x=idxSearch, y=value, group=variable, 
colour=value)) +
geom_line(size = 1.05) + 
scale_colour_gradient(low = 'purple', high = 'green', breaks = c(-8,0,8)) + 
scale_x_continuous(limits = c(0,totstep)) + 
geom_text(data = plot_res, aes(x= totstep, y=value, 
                            label =variable)) +
geom_hline(yintercept = q1, linetype="dotted", color = "#FF6600", size = 
1.07) + 
geom_hline(yintercept = -q1, linetype="dotted", color = "#FF6600", size = 
1.07)

我获得的情节如下

轨迹图

但是,我希望文本标签靠近线条,而不是像它们一样稀疏。有人可以帮忙吗??

太感谢了

4

0 回答 0