cex.main is ignored when plotting [xts]

Viewed 670

I have some xts data, I want to reduce the size of the "main" text but leave the rest of the text its original size. So I tried using cex.main but this does not work. The only function that works is cex but this changes the size of all of my text, not just the main text.

This is my current code and data:

pdf("52_week_ratio_of_technology_to_dispatch_weighted_moving_average_SA.pdf",onefile=TRUE)
plot(ratio_data_dated[, grep("Gas|Coal", names(ratio_data_dated))],
     legend(grep("Gas|Coal", names(ratio_data_dated))),
     cex=0.45,
     col = c("red", "blue"),
     main = "SA: 52 week moving average of ratio of technology to time weighted price",
     legend.loc = "topleft")
dev.off()

> head(ratio_data_dated)
           Battery Brown.Coal      Gas Liquid.Fuel Rooftop.PV Solar      Wind
2011-01-01       0  1.0301403 1.380166    9.023921          0     0 0.9038254
2011-01-08       0  0.9390685 1.214767    6.662844          0     0 0.7722963
2011-01-15       0  1.0262270 1.288217    6.116886          0     0 0.7821223
2011-01-22       0  1.0299489 1.292552    6.116968          0     0 0.7843045
2011-01-29       0  1.0752547 1.531712    7.580647          0     0 0.8201304
2011-02-05       0  0.8692285 1.286059    6.160726          0     0 0.6858254

> str(ratio_data_dated)
An ‘xts’ object on 2011-01-01/2018-12-29 containing:
  Data: num [1:418, 1:7] 0 0 0 0 0 0 0 0 0 0 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:7] "Battery" "Brown.Coal" "Gas" "Liquid.Fuel" ...
  Indexed by objects of class: [POSIXct,POSIXt] TZ: 
  xts Attributes:  
 NULL

Any help would be appreciated, thanks.

1 Answers

I always revert to using base R plot options. Gives me what I need. What you can do in this case is something like below. I force the main title to be empty when creating the plot and then use title to set all the correct options. plot.xts uses it's own plotting device instead of using base R plotting options. I tend to overrule these to give me more control.

plot(ratio_data_dated[, grep("Gas|Coal", names(ratio_data_dated))],
     main = NULL)

addLegend("topleft", 
          on = 1,
          lty = 1
          )
title(main = "SA: 52 week moving average of ratio of technology to time weighted price", cex.main = 0.45)

You might want to look into a small package called rtsplot. This all base R functionality for xts objects, but it has a small learning curve in plotting exactly what you want in data like you have.

Related