我有一个以迭代方式获得的残差矩阵;每列对应于某个单元的残差,每一行与该程序的步骤相对应。 我想生成一个线图,显示不同迭代中每个单元残差的轨迹。 此外,我想在显示相应单位的每个轨迹的末尾添加一个文本标签。
我写的代码如下
# 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)
我获得的情节如下:
但是我希望文本标签靠近线条而不是稀疏。 任何人都可以帮忙??
非常感谢