R: Values on Graph Do Not Match Data

Advertisements

I am working with the R programming language.

I have the following data:

var1 = c(5,5,8,13,14,12,11,10,9,9,8,8)
var2 = c(13,13,22,30,35,31,25,24,20,20,20,21)

var1 <- ts(var1, start = 2010, frequency = 12)
var2 <- ts(var2, start = 2010, frequency = 12)

df = cbind(var1,var2)

        var1 var2
Jan 2010    5   13
Feb 2010    5   13
Mar 2010    8   22
Apr 2010   13   30
May 2010   14   35
Jun 2010   12   31
Jul 2010   11   25
Aug 2010   10   24
Sep 2010    9   20
Oct 2010    9   20
Nov 2010    8   20
Dec 2010    8   21

I then made the following graph for this data:

library(dygraphs)

dygraph(df, main = "Stacked Graph") %>%
  dySeries("var1", label = "Var1") %>%
  dySeries("var2", label = "Var2") %>%
  dyOptions(stackedGraph = TRUE) %>%
  dyRangeSelector(height = 20)

My Question: When I look at the graph of this data, I see that the green values almost approach "50" – yet, the largest value is the entire dataset is only 35.

I am not interested in adding the two variables (var1,var2) together – I just want to plot their values using a stacked time series plot.

Can someone please show me how to fix this?

Thanks!

>Solution :

You can either omit the dyOptions() call completely or
set the stackedGraph argument in it to FALSE.

A stacked plot means that the values of the serieses are
added/summed. By turning that feature of you get the plain
series values.

You can set the fillGraph argument in dySeries() to TRUE, to
color the area under the line.

var1 = c(5,5,8,13,14,12,11,10,9,9,8,8)
var2 = c(13,13,22,30,35,31,25,24,20,20,20,21)

var1 <- ts(var1, start = 2010, frequency = 12)
var2 <- ts(var2, start = 2010, frequency = 12)

df = cbind(var1,var2)

library(dygraphs)

dygraph(df, main = "Graph") %>%
  dySeries("var1", label = "Var1", fillGraph = TRUE) %>%
  dySeries("var2", label = "Var2", fillGraph = TRUE) %>%
  dyOptions(stackedGraph = FALSE) %>%
  dyRangeSelector(height = 20)

Leave a ReplyCancel reply