2

I have a matrix of residuals obtained in an iterative way; each column corresponds to the residuals of a certain unit and each row correspdons to the step of the procedure. I would like to produce a line plot that shows the trajectories of the residuals of each unit over the different iterations. Moreover, I would like to add a text label at the end of each trajectory that shows the corresponding unit.

The code that I wrote is the following

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

And the plot that i obtain is the following one

Trajectories plot

However I would like to have the text labels close to the lines and not sparse as they are. Anyone can help??

Thank you so much

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58

0 Answers0