The `dygraphs::dyGroups()` and `dygraphs::dySeries()` Functions in R Don't Plot Point Shapes Correctly

53 Views Asked by At

I'm having some trouble using the dygraphs package to plot my time-series data. I'm trying to plot 6 response variable columns on the same graph, and I'm trying to split these 6 columns into 2 groups of 3 ('Response_A' and 'Response_B'). Here's a reproducible example.

library (dygraphs)
library (xts)
Example_Data <- structure(list(Time = structure(c(1550257200, 1550258100, 1550259000, 1550259900, 1550260800, 1550261700, 1550262600, 1550263500, 1550264400, 1550265300), class = c("POSIXct", "POSIXt"), tzone = ""), Response_A_1 = c(4.6029401969782, 4.68724036103945, 4.44070599703368, 4.87931323308186, 4.58395337921826, 4.13254678999122, 4.55633027113849, 3.99809955607536, 3.90407856741876, 3.87141569254634), Response_A_2 = c(1.33207856386441, 1.1263604015175, 1.7530348544256, 1.58212211917903, 1.56368927111121, 1.56649283706014, 1.57641840574136, 1.65725516435232, 1.37405727186196, 1.01379705906292), Response_A_3 = c(-0.712946895581712, -0.495506883781476, -0.497509372257565, -0.43605285269654, -0.279764351247258, -0.672498347906582, -1.08628471762834, -0.862017399897083, -0.480049255018176, -0.921377850163446), Response_B_1 = c(8.05911306790343, 4.76155066942116, 5.2337055392762, 6.27565177418466, 6.31406658270596, 7.2186998945874, 6.41200941352901, 5.26234494984677, 4.81567175649172, 5.384386885322), Response_B_2 = c(2.21574040628837, 1.96466019993492, 1.61687963435508, 2.46021347009434, 1.40671770235007, 2.37497954950564, 1.56812703957206, 2.32421882336103, 1.06530960894756, 1.7425644022731), Response_B_3 = c(-0.924676222692984, -1.02580583818082, -0.218012582155277, -0.88964419330529, 0, -1.41994013756489, -1.9063835260558, -0.733969120574939, -0.549471450802579, -0.903473880496443)), row.names = c(NA, 10L), class = "data.frame")
head(Example_Data)
# > head(Example_Data)
#                  Time Response_A_1 Response_A_2 Response_A_3 Response_B_1 Response_B_2 Response_B_3
# 1 2019-02-15 14:00:00     4.602940     1.332079   -0.7129469     8.059113     2.215740   -0.9246762
# 2 2019-02-15 14:15:00     4.687240     1.126360   -0.4955069     4.761551     1.964660   -1.0258058
# 3 2019-02-15 14:30:00     4.440706     1.753035   -0.4975094     5.233706     1.616880   -0.2180126
# 4 2019-02-15 14:45:00     4.879313     1.582122   -0.4360529     6.275652     2.460213   -0.8896442
# 5 2019-02-15 15:00:00     4.583953     1.563689   -0.2797644     6.314067     1.406718    0.0000000
# 6 2019-02-15 15:15:00     4.132547     1.566493   -0.6724983     7.218700     2.374980   -1.4199401
Example_Data_for_Plotting <- xts::xts(x = Example_Data[, grep('Response', colnames(Example_Data))], order.by = Example_Data$Time)

I've tried plotting these data in the following way.

dygraphs::dygraph(Example_Data_for_Plotting) %>%
  dygraphs::dyGroup(colnames(Example_Data_for_Plotting)[grep('Response_A', colnames(Example_Data_for_Plotting))], drawPoints = T, strokeWidth = 0, pointSize = 2.5, pointShape = 'triangle', color = c('blue', 'brown', 'black')) %>%
  dygraphs::dyGroup(colnames(Example_Data_for_Plotting)[grep('Response_B', colnames(Example_Data_for_Plotting))], drawPoints = T, strokeWidth = 0, pointSize = 2.5, pointShape = 'square', color = c('blue', 'brown', 'black'))

In this case, I get the following error message.

Error in dygraphs::dyGroup(., colnames(Example_Data_for_Plotting)[grep("Response_A",  : 
  Invalid value for pointShape parameter. Should be one of the following: 'dot', 'triangle', 'square', 'diamond', 'pentagon', 'hexagon', 'circle', 'star', 'plus' or 'ex'

I've also tried using 6 separate dygraphs::dySeries() functions as a workaround in the following manner.

dygraphs::dygraph(Example_Data_for_Plotting) %>%
  dygraphs::dySeries(colnames(Example_Data_for_Plotting)[grep('Response', colnames(Example_Data_for_Plotting))][1], drawPoints = T, strokeWidth = 0, pointSize = 2.5, pointShape = 'triangle', color = 'blue') %>%
  dygraphs::dySeries(colnames(Example_Data_for_Plotting)[grep('Response', colnames(Example_Data_for_Plotting))][2], drawPoints = T, strokeWidth = 0, pointSize = 2.5, pointShape = 'triangle', color = 'brown') %>%
  dygraphs::dySeries(colnames(Example_Data_for_Plotting)[grep('Response', colnames(Example_Data_for_Plotting))][3], drawPoints = T, strokeWidth = 0, pointSize = 2.5, pointShape = 'triangle', color = 'black') %>%
  dygraphs::dySeries(colnames(Example_Data_for_Plotting)[grep('Response', colnames(Example_Data_for_Plotting))][4], drawPoints = T, strokeWidth = 0, pointSize = 2.5, pointShape = 'square', color = 'blue') %>%
  dygraphs::dySeries(colnames(Example_Data_for_Plotting)[grep('Response', colnames(Example_Data_for_Plotting))][5], drawPoints = T, strokeWidth = 0, pointSize = 2.5, pointShape = 'square', color = 'brown') %>%
  dygraphs::dySeries(colnames(Example_Data_for_Plotting)[grep('Response', colnames(Example_Data_for_Plotting))][6], drawPoints = T, strokeWidth = 0, pointSize = 2.5, pointShape = 'square', color = 'black')

In this case, there's no error message, but I don't get the point shapes I'm hoping for. The colors are correct but the only shape that matches what I coded for is the one for the last series. The rest are the default (dots).

Is there a bug? I know I'm entering the arguments to these functions correctly.

1

There are 1 best solutions below

1
On

Workaround using dySeries():

dygraph(xts::xts(x = Example_Data[, grep('Response', colnames(Example_Data))], order.by = Example_Data$Time)) %>%
  dySeries(
    "Response_A_1",
    strokePattern = "dotted",
    color = "blue"
  ) %>%
  dySeries(
    "Response_A_2",
    strokePattern = "dotted",
    color ='brown'
  ) %>%
  dySeries(
    "Response_A_3",
    strokePattern = "dotted",
    color = 'black'
  ) %>%
  dyGroup(
    colnames(Example_Data[grep('Response_B', colnames(Example_Data))]),
    drawPoints = TRUE,
    color = c('blue', 'brown', 'black')
  ) %>%
  dyRangeSelector()