3

How can I remove the date range in the top right corner of an xts plot? For example, in the top right corner of the xts plot below, I would like to remove the text "2007-01-02 / 2007-06-30".

library(xts)
data(sample_matrix)
sample.xts <- as.xts(sample_matrix)
plot(sample.xts[,"Close"])

Sample xts plot from joshuaulrich.github.io/xts

The code and plot is taken from Joshua Ulrich's xts website.

Samuel
  • 2,895
  • 4
  • 30
  • 45
  • 1
    You can use the solution in [this answer](https://stackoverflow.com/a/50051183/271616) until there's a patch to make it optional. – Joshua Ulrich May 15 '18 at 22:21

1 Answers1

1

I don't think it's straightforward to customise the output of plot.xts but perhaps somebody else will correct me.

I would use ggplot to plot the data, which gives you all the options you could wish for to customise labels, ticks, grid lines, titles, annotations etc.

Here is an example how to recreate above plot:

library(scales);
library(tidyverse);
sample.xts %>%
    as.data.frame() %>%
    rownames_to_column("Date") %>%
    mutate(Date = as.Date(Date, format = "%Y-%m-%d")) %>%
    ggplot(aes(Date, Close)) +
    geom_line() +
    scale_x_date(
        date_breaks = "1 month",
        labels = date_format("%b\n%Y")) +
    theme_minimal()

enter image description here

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68