0

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:
enter image description here

This is how the chart should look like whitout dates:

enter image description here

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).
enter image description here
How can I tell plot or axis.POSIXct not to create the lines which connect fridays values to mondays values?

  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Maybe the problem is that you are only using `as.Date()` on the axis call and not the plot class itself. Hard to say without seeing the data. – MrFlick Oct 13 '20 at 17:40
  • sample posted.. however, it seems I've found the error: I've defined the dates as `POSIXct` without specifying the hours, which are stored in a separate column... this lead to an error, because I have 24 posixct dates which are considered as the same date (no hour is provided). I've tried to convert to `POSIXlt` using `as.POSIXlt` and adding hours with `$hour` but I'm not able to re-store the posixlt dates in the dataframe... –  Oct 13 '20 at 18:16

0 Answers0