I have a some data that looks like this: https://i.sstatic.net/TjJ9k.jpg
And I am plotting it using:
plot(fifty_twoweekmovavg)
pdf("52_week_moving_average_chartNSW.pdf",onefile=TRUE)
addLegend("topleft",lty = 1,cex=1.2)
dev.off()
How do I plot it so that I only include a few variables? E.g. plot the NSW price and coal price against time, rather than plotting every variable against time?
Thanks
Reproducible example:
NSW1.Price Black.Coal Gas Hydro Liquid.Fuel
2011-01-01 30.89336 32.33668 41.63653 69.82661 108.06855
2011-01-08 30.98103 32.24805 41.33295 69.44308 104.36587
2011-01-15 30.73076 32.11497 40.76273 69.59129 97.30812
2011-01-22 30.76028 30.50381 36.56215 62.50329 61.78828
2011-01-29 29.76733 34.65090 43.94289 93.20954 113.42410
Edit2, How I created data:
mydata=read.csv(file="nem_tech_dataTAS.csv")
library(xts)
library(zoo)
date <- seq(from=as.POSIXct("2010-01-01 00:30", format = "%Y-%m-%d %H:%M"), length.out = nrow(mydata), by = "30 min")
mydata_dated <- xts(mydata, date)
fifty_twoweekmovavg=rollapply(mydata_dated,17520,mean,by = 336,na.pad = FALSE)
Edit3, format of legend:
Plotting xts objects can be done with the plot command if package xts
has been loaded. For more details on how to plot xts objects, use ?plot.xts
.
To select only 2 columns you can use grep
inside the xts object.
library(xts)
plot(fifty_twoweekmovavg[, grep("NSW1|Coal", names(fifty_twoweekmovavg))],
legend(grep("NSW1|Coal", names(fifty_twoweekmovavg))),
main = "52_week_moving_average",
legend.loc = "topleft")
edit: manipulating the legend, this makes it easier, and will result in the same plot, but with lines for the legend instead of squares:
plot(fifty_twoweekmovavg[, grep("NSW1|Coal", names(fifty_twoweekmovavg))],
main = "52_week_moving_average")
# on = 1 is for main plot. lty is for showing a line in the legend.
# see ?addLegend and ?legend
addLegend("topleft", on = 1, lty=1)