3

I had produced the following chart, which is created using an xts object.

enter image description here

The code I used was simply

   plot(graphTS1$CCLL, type = "l", las = 2, ylab = "(c)\nCC for Investors", xlab = "", main = "Clustering Coefficient at the Investor Level")

I updated my R Studio Package and R version and then by running the same code I got the following chart.

enter image description here

Obviously the two are not the same. Can people help me remove the second Y axis - I do not need that, and remove the automatic heading of 2007-03-01 / 2010-12-01. Numerous attempts of this have failed. I did use zoo.plot but the gridlines and ability and the quarterly marks were removed.

My R version is 3.4.0 (2017-04-21) with the following platform "x86_64-apple-darwin15.6.0". Thanks in advance.

Matthew Oldham
  • 187
  • 1
  • 13

2 Answers2

5

You want to remove the right hand axis. There is an argument in plot.xts(..., yaxis.right = TRUE, ...). So

library('xts')
#> Loading required package: zoo
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric

data(sample_matrix)
sample.xts <- as.xts(sample_matrix, descr='my new xts object')

plot(sample.xts, yaxis.right = FALSE)

does what you want.

I took a stab at solving the second question, removing the label at the top right hand side. Examining the source code for plot.xts() reveals that label is hardcoded into the main title. Even setting main = '' isn't going to remove it. You can work around it by editing plot.xts() and copying it to a new function.

plotxts <- fix("plot.xts")
# In the editor that opens, replace the lines below:
###    text.exp <- c(expression(text(xlim[1], 0.5, main, font = 2, 
###        col = theme$labels, offset = 0, cex = 1.1, pos = 4)), 
###        expression(text(xlim[2], 0.5, paste(start(xdata[xsubset]), 
###            end(xdata[xsubset]), sep = " / "), col = theme$labels, 
###            adj = c(0, 0), pos = 2)))
###    cs$add(text.exp, env = cs$Env, expr = TRUE)
# with these lines:
###    text.exp <- expression(text(xlim[1], 0.5, main, font = 2,
###        col = theme$labels, offset = 0, cex = 1.1, pos = 4))

# Finally, you need to ensure your copy's environment is the xts namespace:
environment(plotxts) <- asNamespace("xts")

plotxts(sample.xts, yaxis.right = FALSE, main = "Main Title")

The second, and perhaps simpler option is to use a different plot function and modify it to produce the gridlines etc that you want. I will start with plot.zoo() because it is already handling time series nicely.

zoo::plot.zoo(sample.xts, screens = 1, xlab="", las=2, main="Main Title")
grid() # add the grid

That at least gets the grid on there. I can't test if it will handle the x axis labels the same way without data at the right frequency.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
atiretoo
  • 1,812
  • 19
  • 33
1

You can now (finally) omit the date range from the plot in xts 0.12.2 on CRAN. Set main.timespan = FALSE.

plot(sample.xts[,1], yaxis.right = FALSE, main = "Main Title", main.timespan = FALSE)

enter image description here

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418