I'm trying to plot thwo data series and a common date (POSIXct
) on a chart for a personal project that I'm developing. I've firstly used plot()
to generate the chart and the main data line, applying axis.date()
to visualize the dates on the chart. Then, I've used lines()
to plot the other data series but, as a result, a very strange behaviour happens: all the series on the chart appear to be 'compressed' and no date is shown:
This is how the chart should look like whitout dates:
This is the code I've written to generate the first chart:
plot(x=df$Data[start:end],
y=df$Losses[start:end],
xaxt="n",
type='l',
ylim=c(-0.5,0.5),
xlab='Dates',
ylab='Losses/VaR')
axis.POSIXct(1,at=seq(as.Date(df$Data[start]),as.Date(df$Data[end]),by="1 day"),format="%m/%Y")
lines(df$VaRevt[start:end], col='red', x=df$Data[start:end])
I really can't figure out what's wrong...
Do you have any suggestion?
Thanks!
I post here a sample of my data (start=1, end=201):
file
I've found that I've stored df$Data
as POSIXct
without specifying the hours, so it leads to multiple data values for each date.
Solved by converting POSIXct
to POSIXlt
and adding the hour to the POSIXlt
object. However another issue arises: the figure plots a 'jump' for saturdays and sundays (which are not present in the dataset).
How can I tell plot
or axis.POSIXct
not to create the lines which connect fridays values to mondays values?