Home > Mobile >  ggplot new scale_x function failing to work correctly
ggplot new scale_x function failing to work correctly

Time:07-26

In order to get rid of non-trading days of stocks (aka weekends) in ggplot instead of the date, I use the number of rows in the data and then add breaks and labels. The code below "works" does the trick and plots the data like chartSeries from the quantmod package. ggplot adds information that is not there or shows gaps depending on what kind of chart you are making. For dealing with stock prices this is not handy. Hence the works section.

But since this is just a labeling issue, an axis transformer function would be more logical and easier to use. I tried creating a scale_x_finance function (see does not work section), but I must be interpreting the inverse function incorrectly as I only get back a plot of 1 date and not the whole timeseries.

I read several SO questions like enter image description here

To demonstrate that gaps in the data simply remove space in the x axis (which seems to be the ultimate goal here), we can remove a week's worth of data and see that the dates on either side of the missing dates just move closer together:

test_data <- test_data[-(25:31),]

ggplot(test_data, aes(x = date, y = close))   
  geom_line(aes(y = close))  
  scale_x_finance(dates = test_data$date)

enter image description here

  • Related