library(plotly) time = c('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec') p1 = c(20, 35, 30, 35, 27, 43, 24, 34, 14, 27, 22, 50) p2 = c(25, 32, 34, 20, 25, 34, 54, 23, 43, 33, 27, 29) data <- data.frame(time,p1,p2 ) data$time <- factor(data$time, levels = data$time) fig <- plot_ly(data, x = ~time, y = ~p1, type = 'bar', name = 'Product A') fig <- fig %>% add_trace(y = ~p2, name = 'Product B') fig <- fig %>% layout(yaxis = list(title = 'Sale of Products'), barmode = 'stack') fig