I have the following data table dt.data
:
dt.data <- structure(list(begTime = c("01-JAN-23", "01-JAN-23", "01-JAN-23",
"01-FEB-23", "01-FEB-23", "01-FEB-23", "01-MAR-23", "01-MAR-23",
"01-MAR-23", "01-APR-23", "01-APR-23", "01-APR-23", "01-MAY-23",
"01-MAY-23", "01-MAY-23", "01-JUN-23", "01-JUN-23", "01-JUN-23",
"01-JUL-23", "01-JUL-23", "01-JUL-23", "01-AUG-23", "01-AUG-23",
"01-AUG-23", "01-SEP-23", "01-SEP-23", "01-SEP-23", "01-OCT-23",
"01-OCT-23", "01-OCT-23", "01-NOV-23", "01-NOV-23", "01-NOV-23",
"01-DEC-23", "01-DEC-23", "01-DEC-23"), toBook = c("KG21", "KG64",
"Extern", "KG21", "KG64", "Extern", "KG21", "KG64", "Extern",
"KG21", "KG64", "Extern", "KG21", "KG64", "Extern", "KG21", "KG64",
"Extern", "KG21", "KG64", "Extern", "KG21", "KG64", "Extern",
"KG21", "KG64", "Extern", "KG21", "KG64", "Extern", "KG21", "KG64",
"Extern", "KG21", "KG64", "Extern"), sumQuantity = c(1440, 200.947775009365,
-3978.571120032, 0, -17812.0922910065, 19996.296169952, 0, 12995.1445322506,
-15165.299710032, 2160, -53923.0554299322, 54824.803160056, 504,
-7045.79687321736, 8256.75532999996, 0, -14919.1153921838, 13458.1132399999,
0, -15966.4071659115, 15083.67141, 720, -18714.0358020241, 18129.3816599999,
240, 2905.88494461573, -6727.47018, 0, 39413.3865474728, -36206.19812,
0, -9407.5891668633, 9800.53, 0, -17781.1490225234, 17946.678
), unit = c("MWh", "MWh", "MWh", "MWh", "MWh", "MWh", "MWh",
"MWh", "MWh", "MWh", "MWh", "MWh", "MWh", "MWh", "MWh", "MWh",
"MWh", "MWh", "MWh", "MWh", "MWh", "MWh", "MWh", "MWh", "MWh",
"MWh", "MWh", "MWh", "MWh", "MWh", "MWh", "MWh", "MWh", "MWh",
"MWh", "MWh"), sumValue = c(108891.36, 128053.530976637, -273632.88711977,
0, -1165755.80930466, 1274897.88251747, 0, 512406.636632069,
-617755.657019838, 102372.48, -2495006.82184836, 2522765.0072208,
15463.728, -305363.977539244, 336017.268406819, 0, -487578.86709908,
445164.689526293, 0, -517033.916204391, 475791.067501664, 26482.32,
-640698.403786333, 612188.309980192, 8319.6, 96201.4521993924,
-243811.777077578, 0, 1582012.15513231, -1467874.96096514, 0,
-429850.119024673, 434161.39860219, 0, -719195.470550453, 724300.2
), newBegTime = structure(c(19358, 19358, 19358, 19389, 19389,
19389, 19417, 19417, 19417, 19448, 19448, 19448, 19478, 19478,
19478, 19509, 19509, 19509, 19539, 19539, 19539, 19570, 19570,
19570, 19601, 19601, 19601, 19631, 19631, 19631, 19662, 19662,
19662, 19692, 19692, 19692), class = "Date"), monthYear = c("JAN2023",
"JAN2023", "JAN2023", "FEB2023", "FEB2023", "FEB2023", "MAR2023",
"MAR2023", "MAR2023", "APR2023", "APR2023", "APR2023", "MAY2023",
"MAY2023", "MAY2023", "JUN2023", "JUN2023", "JUN2023", "JUL2023",
"JUL2023", "JUL2023", "AUG2023", "AUG2023", "AUG2023", "SEP2023",
"SEP2023", "SEP2023", "OCT2023", "OCT2023", "OCT2023", "NOV2023",
"NOV2023", "NOV2023", "DEC2023", "DEC2023", "DEC2023")), row.names = c(NA,
-36L), class = c("data.table", "data.frame"))
Now I want to create a grouped bar chart, where my x-axis is the column newBegTime
and my y-axis is the sumQuantity
. The groups should be defined by toBook
.
I have tried the following plotly-coding, but this doesn't give me the result I need:
plotQuantity <- plotly::plot_ly(dt.data, x = ~newBegTime, y = ~sumQuantity, type = 'bar', name = 'Quantity') %>%
add_trace(y = ~toBook, name = 'toBook') %>%
layout(xaxis = list(),
yaxis = list(title = 'MWh'),
barmode = 'group')
You should group_by
your data and give them a color
like this:
library(dplyr)
library(plotly)
dt.data %>%
group_by(toBook) %>%
plot_ly(x = ~newBegTime, y = ~sumQuantity, type = 'bar', color = ~toBook) %>%
layout(xaxis = list(),
yaxis = list(title = 'MWh'),
barmode = 'group')
Created on 2023-12-13 with reprex v2.0.2