Last active
September 7, 2020 14:10
-
-
Save addiversitas/76310152b492dd3431b1277d6d1745ac to your computer and use it in GitHub Desktop.
basic plotly chart in a shiny app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library("shiny") | |
| library("plotly") | |
| library("data.table") | |
| dates <- seq(as.Date("2019-02-01"),length=12,by="months") - 1 | |
| books <- c(17,3,3,18,20,19,15,12,17,4,18,7) | |
| laptops <- c(13,5,8,12,27,32,31,9,22,11,12,30) | |
| tvs <- c(30,41,22,11,4,33,16,17,8,49,14,50) | |
| data <- data.frame(dates = dates, | |
| books = books, | |
| laptops = laptops, | |
| tvs = tvs) | |
| data <- melt(data, id.vars = "dates") | |
| server <- function(input, output) { | |
| output$sales = renderPlotly({ | |
| fig <- plot_ly(data, x = ~dates, y = ~value, type = 'bar', name = ~variable, color = ~variable) | |
| fig <- fig %>% layout(xaxis = list(tickvals = dates, tickformat = "%b %Y", tickfont = list(size = 12)), barmode = 'stack') | |
| fig | |
| }) | |
| } | |
| ui <- fluidPage( | |
| plotlyOutput("sales") | |
| ) | |
| shinyApp(ui = ui, server = server) | |
| shinyApp(ui = ui, server = server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment