1

I have a some data that looks like this: https://imgur.com/a/UK64GCp

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:

Current legend:legend1

Desired legend: legend2

user8261831
  • 392
  • 3
  • 16

3 Answers3

1

If you want to draw two lines that show the Gas and Hydro values over time, you first need to create a time series. Create a column that gets the date and turns it into Date format using as.Date. In your example above, you would write:

fifty_twoweekmovavg$date=as.Date(rownames(fifty_twoweekmovavg))

That gets you your x axis values.

Now in order to get both your Gas and Hydro values you have to make sure that the y axis fits both of them as the values for Gas and Hydro do not intersect.

One way of doing this is by:

extents=range(c(fifty_twoweekmovavg$Gas,fifty_twoweekmovavg$Hydro))

Once you have your date and extents set, you may finally proceed to plot your lines:

plot(fifty_twoweekmovavg$date,fifty_twoweekmovavg$Gas,type='l',ylim=extents)
lines(fifty_twoweekmovavg$date,fifty_twoweekmovavg$Hydro,col='red')
thequerist
  • 1,624
  • 3
  • 16
  • 26
  • thanks but when I do your first line I get this: `> fifty_twoweekmovavg$date=as.Date(rownames(fifty_twoweekmovavg)) Error in as.Date.default(x, ...) : do not know how to convert 'x' to class “Date”` – user8261831 Jan 09 '19 at 04:15
  • Can you try `str(fifty_twoweekmovavg)` and copy and paste what you get. – thequerist Jan 09 '19 at 04:28
  • `An ‘xts’ object on 2011-01-01/2018-12-29 containing: Data: num [1:418, 1:6] 30.9 30.9 30.7 30.6 30.6 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:6] "TAS1.Price" "Gas" "Hydro" "Liquid.Fuel" ... Indexed by objects of class: [POSIXct,POSIXt] TZ: xts Attributes: NULL` – user8261831 Jan 09 '19 at 04:30
  • Ah, can you show the code you used to create the XTS object? [This question](https://stackoverflow.com/questions/11429889/converting-an-xts-object-to-a-data-frame) may be useful to you. – thequerist Jan 09 '19 at 04:36
  • yep its in the original question now – user8261831 Jan 09 '19 at 04:44
  • Try this: `fifty_twoweekmovavg_xts=fifty_twoweekmovavg` and then `fifty_twoweekmovavg=data.frame(date=index(fifty_twoweekmovavg),fifty_twoweekmovavg,row.names=NULL)`. You will still have your old XTS object but this will allow you to plot in base using a data frame. If you really want to plot the XTS object look into plot.zoo. Here is [an example](https://stackoverflow.com/questions/34319504/r-how-can-i-change-date-format-when-i-plot-an-xts-zoo-object). – thequerist Jan 09 '19 at 04:53
1

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)

enter image description here

phiver
  • 19,366
  • 14
  • 36
  • 42
  • Oh great, I’ll try this out – user8261831 Jan 09 '19 at 21:01
  • Works perfect thank you, although I don't completely understand how "grep" works – user8261831 Jan 09 '19 at 21:51
  • One minor thing but I'd like to fix it if I can is the format of the legend. Using your code my legend has a box indicating each colour, wheres originally I had a line indicating each colour. I have edited my question above to illustrate this. How do I format the legend with the line format rather than your box format? Thanks! – user8261831 Jan 09 '19 at 22:10
0

The code below can render a plot with multiple lines with the graph legend.

df2 = data.frame(matrix(data=c(
30.89336,   32.33668, 41.63653,  69.82661,   108.06855,
30.98103,   32.24805, 41.33295,  69.44308,   104.36587,
30.73076,   32.11497, 40.76273,  69.59129,    97.30812,
30.76028,   30.50381, 36.56215,  62.50329,    61.78828,
29.76733,   34.65090, 43.94289,  93.20954,   113.42410
), ncol = 5, byrow = TRUE ))
colnames(df2) = c("NSW1.Price", "Black.Coal", "Gas", "Hydro", "Liquid.Fuel")
df1 = data.frame("time" = as.Date(
      c("2011-01-01",   
        "2011-01-08",   
        "2011-01-15",   
        "2011-01-22",   
        "2011-01-29"),"%Y-%m-%d"))
df = cbind(df1, df2)
plot(0, cex=0, xlim=range(df$time), 
     ylim=c(0,max(c(df$NSW1.Price, df$Black.Coal))))
lines(df$time, df$NSW1.Price, col="cyan", lty = 1)
lines(df$time, df$Black.Coal, col="black", lty=2)
legend("bottomleft", legend = c("NSW1.Price", "Black.Coal"), 
       col = c("cyan", "black"), lty = c(1,2))

enter image description here

stok
  • 340
  • 4
  • 8