I'm trying to plot multiple series on a dygraph line chart. The df is of this structure:
structure(list(Date = structure(c(4216, 4216, 4217, 4217, 4218,
4218, 4219, 4219, 4220, 4220, 4221, 4221, 4222, 4222, 4223, 4223,
4224, 4224, 4225, 4225, 4226, 4226, 4227, 4227, 4228, 4228, 4229,
4229, 4230, 4230, 4231, 4231, 4232, 4232, 4233, 4233, 4234, 4234,
4235, 4235, 4236, 4236, 4237, 4237, 4238, 4238, 4239, 4239, 4240,
4240, 4241, 4241, 4242, 4242, 4243, 4243, 4244, 4244, 4245, 4245,
4246, 4246, 4247, 4247, 4248, 4248, 4249, 4249, 4250, 4250, 4251,
4251, 4252, 4252, 4253, 4253, 4254, 4254, 4255, 4255, 4256, 4256,
4257, 4257, 4258, 4258, 4259, 4259, 4260, 4260, 4261, 4261, 4262,
4262, 4263, 4263, 4264, 4264, 4265, 4265, 4266, 4266, 4267, 4267,
4268, 4268, 4269, 4269, 4270, 4270, 4271, 4271, 4272, 4272, 4273,
4273, 4274, 4274, 4275, 4275, 4276, 4276, 4277, 4277, 4278, 4278,
4279, 4279, 4280, 4280, 4281, 4281, 4282, 4282, 4283, 4283, 4284,
4284, 4285, 4285, 4286, 4286, 4287, 4287, 4288, 4288, 4289, 4289,
4290, 4290, 4291, 4291, 4292, 4292, 4293, 4293, 4294, 4294, 4295,
4295, 4296, 4296, 4297, 4297, 4298, 4298, 4299, 4299, 4300, 4300,
4301, 4301, 4302, 4302, 4303, 4303, 4304, 4304, 4305, 4305, 4306,
4306, 4307, 4307, 4308, 4308, 4309, 4309, 4310, 4310, 4311, 4311,
4312, 4312, 4313, 4313, 4314, 4314, 4315, 4315, 4316, 4316, 4317,
4317, 4318, 4318, 4319, 4319, 4320, 4320, 4321, 4321, 4322, 4322,
4323, 4323, 4324, 4324, 4325, 4325, 4326, 4326, 4327, 4327, 4328,
4328, 4329, 4329, 4330, 4330, 4331, 4331, 4332, 4332, 4333, 4333,
4334, 4334, 4335, 4335, 4336, 4336, 4337, 4337, 4338, 4338, 4339,
4339, 4340, 4340, 4341, 4341, 4342, 4342, 4343, 4343, 4344, 4344,
4345, 4345, 4346, 4346, 4347, 4347, 4348, 4348, 4349, 4349, 4350,
4350, 4351, 4351, 4352, 4352, 4353, 4353, 4354, 4354, 4355, 4355,
4356, 4356, 4357, 4357, 4358, 4358, 4359, 4359, 4360, 4360, 4361,
4361, 4362, 4362, 4363, 4363, 4364, 4364, 4365, 4365, 4366, 4366,
4367, 4367, 4368, 4368, 4369, 4369, 4370, 4370, 4371, 4371, 4372,
4372, 4373, 4373, 4374, 4374, 4375, 4375, 4376, 4376, 4377, 4377,
4378, 4378, 4379, 4379, 4380, 4380, 4381, 4381, 4382, 4382, 4383,
4383, 4384, 4384, 4385, 4385, 4386, 4386, 4387, 4387, 4388, 4388,
4389, 4389, 4390, 4390, 4391, 4391, 4392, 4392, 4393, 4393, 4394,
4394, 4395, 4395, 4396, 4396, 4397, 4397, 4398, 4398), class = "Date"),
Em_balde = c("Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short", "Medium", "Short", "Medium", "Short", "Medium",
"Short"), Valor = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000,
0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0,
1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000,
0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0,
1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000,
0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0,
1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000,
0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0,
1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000,
0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0,
1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000,
0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0,
1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000,
0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0,
1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000,
0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0,
1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000,
0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0, 1000, 0,
1000, 0, 1000, 0)), class = c("grouped_df", "tbl_df", "tbl",
"data.frame"), row.names = c(NA, -366L), groups = structure(list(
Date = structure(c(4216, 4217, 4218, 4219, 4220, 4221, 4222,
4223, 4224, 4225, 4226, 4227, 4228, 4229, 4230, 4231, 4232,
4233, 4234, 4235, 4236, 4237, 4238, 4239, 4240, 4241, 4242,
4243, 4244, 4245, 4246, 4247, 4248, 4249, 4250, 4251, 4252,
4253, 4254, 4255, 4256, 4257, 4258, 4259, 4260, 4261, 4262,
4263, 4264, 4265, 4266, 4267, 4268, 4269, 4270, 4271, 4272,
4273, 4274, 4275, 4276, 4277, 4278, 4279, 4280, 4281, 4282,
4283, 4284, 4285, 4286, 4287, 4288, 4289, 4290, 4291, 4292,
4293, 4294, 4295, 4296, 4297, 4298, 4299, 4300, 4301, 4302,
4303, 4304, 4305, 4306, 4307, 4308, 4309, 4310, 4311, 4312,
4313, 4314, 4315, 4316, 4317, 4318, 4319, 4320, 4321, 4322,
4323, 4324, 4325, 4326, 4327, 4328, 4329, 4330, 4331, 4332,
4333, 4334, 4335, 4336, 4337, 4338, 4339, 4340, 4341, 4342,
4343, 4344, 4345, 4346, 4347, 4348, 4349, 4350, 4351, 4352,
4353, 4354, 4355, 4356, 4357, 4358, 4359, 4360, 4361, 4362,
4363, 4364, 4365, 4366, 4367, 4368, 4369, 4370, 4371, 4372,
4373, 4374, 4375, 4376, 4377, 4378, 4379, 4380, 4381, 4382,
4383, 4384, 4385, 4386, 4387, 4388, 4389, 4390, 4391, 4392,
4393, 4394, 4395, 4396, 4397, 4398), class = "Date"), .rows = structure(list(
1:2, 3:4, 5:6, 7:8, 9:10, 11:12, 13:14, 15:16, 17:18,
19:20, 21:22, 23:24, 25:26, 27:28, 29:30, 31:32, 33:34,
35:36, 37:38, 39:40, 41:42, 43:44, 45:46, 47:48, 49:50,
51:52, 53:54, 55:56, 57:58, 59:60, 61:62, 63:64, 65:66,
67:68, 69:70, 71:72, 73:74, 75:76, 77:78, 79:80, 81:82,
83:84, 85:86, 87:88, 89:90, 91:92, 93:94, 95:96, 97:98,
99:100, 101:102, 103:104, 105:106, 107:108, 109:110,
111:112, 113:114, 115:116, 117:118, 119:120, 121:122,
123:124, 125:126, 127:128, 129:130, 131:132, 133:134,
135:136, 137:138, 139:140, 141:142, 143:144, 145:146,
147:148, 149:150, 151:152, 153:154, 155:156, 157:158,
159:160, 161:162, 163:164, 165:166, 167:168, 169:170,
171:172, 173:174, 175:176, 177:178, 179:180, 181:182,
183:184, 185:186, 187:188, 189:190, 191:192, 193:194,
195:196, 197:198, 199:200, 201:202, 203:204, 205:206,
207:208, 209:210, 211:212, 213:214, 215:216, 217:218,
219:220, 221:222, 223:224, 225:226, 227:228, 229:230,
231:232, 233:234, 235:236, 237:238, 239:240, 241:242,
243:244, 245:246, 247:248, 249:250, 251:252, 253:254,
255:256, 257:258, 259:260, 261:262, 263:264, 265:266,
267:268, 269:270, 271:272, 273:274, 275:276, 277:278,
279:280, 281:282, 283:284, 285:286, 287:288, 289:290,
291:292, 293:294, 295:296, 297:298, 299:300, 301:302,
303:304, 305:306, 307:308, 309:310, 311:312, 313:314,
315:316, 317:318, 319:320, 321:322, 323:324, 325:326,
327:328, 329:330, 331:332, 333:334, 335:336, 337:338,
339:340, 341:342, 343:344, 345:346, 347:348, 349:350,
351:352, 353:354, 355:356, 357:358, 359:360, 361:362,
363:364, 365:366), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -183L), .drop = TRUE))
I've usually been able to convert it to a usable format for dygraphs by doing xts(x = df$valor, order.by = df$Date).
However because there are multiple "Em_balde" a problem is caused. What I'd like to do is have a separate line for each series contained within "Em_balde".
For single data series I've been using:
dygraph(df, main = "Em Balde - Médio") %>%
dySeries("V1", label = "Valor") %>%
dyOptions(labelsUTC = TRUE, fillGraph=TRUE, fillAlpha=0.1, drawGrid = FALSE, stepPlot = TRUE) %>%
dyHighlight(highlightCircleSize = 5, highlightSeriesBackgroundAlpha = 0.2, hideOnMouseOut = FALSE)
But as said above that doesn't work for my use case.
To plot multiple series, each line needs to sit within its own column. Therefore I had to use pivot_wider to amend the original dataframe, followed by a normal xts conversion to produce the chart