2

I have a dataset of cumulative returns (called Merged_Returns_Set), which looks like this:

   Time                USD_THB_Close USD_CNH_Close JP225_USD_Close USD_MXN_Close GBP_USD_Close IN50_USD_Close

   <dttm>                      <dbl>         <dbl>           <dbl>         <dbl>         <dbl>          <dbl>
 1 2017-12-13 20:00:00         1.00          1.00            1.00          1.00           1.00           1.00
 2 2017-12-13 20:15:00         1.000         1.000           0.999         0.998          1.00           1.00
 3 2017-12-13 20:30:00         1.00          0.999           1.00          0.999          1.00           1.00
 4 2017-12-13 20:45:00         1.000         1.000           1.00          1.000          1.00           1.00

I wanted to plot all cumulative returns plot on one ggplot, so after browsing stackoverflow, I came up with the following solution:

  df.melted <- reshape::melt(data.frame(Returns_Data_Set), id = "Time")
  ggplot(data = df.melted, aes(x = Time, y = value, color = variable)) +
  geom_point() + theme(legend.position="none") 

The pertinent part of the dataset df.melted, which is used for the ggplot, looks like this (the same for all symbols):

191   2017-12-15 22:45:00    USD_THB_Close 0.9996249
192   2017-12-15 23:00:00    USD_THB_Close 0.9995326
193   2017-12-17 23:15:00    USD_THB_Close 0.9999015
194   2017-12-18 00:00:00    USD_THB_Close 0.9997478
195   2017-12-18 00:15:00    USD_THB_Close 0.9997785

Looking at the plot below, how can I tell R to skip the dates on the x-axis, for which there is no data?

enter image description here

user3612816
  • 325
  • 2
  • 11
  • It sounds like you're trying to insert breaks in an axis. This is something [ggplot2 generally cannot do](https://stackoverflow.com/questions/7194688/using-ggplot2-can-i-insert-a-break-in-the-axis). – bouncyball Mar 21 '18 at 13:28
  • Are there any decent alternatives for producing such plots? – user3612816 Mar 21 '18 at 13:30
  • In that link I posted, it looks like the `plotrix` library could be helpful. I have never used that library – bouncyball Mar 21 '18 at 13:36

1 Answers1

0

As @bouncyball notes in comments you can't (as far as I know). You can hack around it a bit in ggplot however by faceting to your two periods of observable data. This has the advantage of making clear to the reader that there is a gap in the data. I'll generate some data to illustrate:

library(ggplot2)
df <- data.frame(date = seq(from = as.Date("2000-01-01"), to = as.Date("2004-12-04"), by = "day"))
df$comp <- rep(1:6, nrow(df)/6)
df$price <- rnorm(nrow(df), df$comp, 1)

# now make some missing bits
df$price[df$date < "2003-11-01" & df$date > "2001-06-01"] <- NA

# we see a gap
ggplot(na.omit(df), aes(date, price, group=comp, color=as.factor(comp))) + 
  geom_point() + theme_classic()

# make an indicator for the two periods
df$period <- ifelse(df$date < "2003-11-01", "< June 2001", "> Nov. 2003")
ggplot(na.omit(df), aes(date, price, group=comp, color=as.factor(comp))) + geom_point() +
  facet_wrap(~period, nrow = 1, scales = "free_x") +
  theme_classic()

Created on 2018-03-21 by the reprex package (v0.2.0).

gfgm
  • 3,627
  • 14
  • 34