I am trying to make a plotly chart for a shiny app. As of now I am doing is hardcoding by adding each trace. But I did realize that the columns in data get updated and the way I am doing that will not plot the data. How can I make sure tht whenever the data is added with a column name which has sale
in it it would get added to the plot. Below are the data and the way I am doing it currently.
d1 <- tibble::tribble(
~Date, ~apple_count, ~apple_sale, ~banana_count, ~banana_sale, ~orange_count, ~orange_sale, ~peaches_count, ~peaches_sale, ~watermelon_count, ~watermelon_sale, ~strawberry_count, ~strawberry_sale,
"8/19/19", 10882.05495, 239575, 0, 0, 0, 0, 0, 0, 9643.600102, 630827, 0, 0,
"8/20/19", 516.29755, 11281, 0, 0, 0, 0, 0, 0, 6041.538067, 510219, 1694.44, 684210,
"8/21/19", 949.4084, 20150, 0, 0, 0, 0, 0, 0, 5371.758106, 565440, 9105.89, 3695182,
"8/22/19", 3950.5318, 88679, 0, 0, 0, 0, 0, 0, 5238.308826, 576678, 6179.47, 2501560,
"8/23/19", 2034.02055, 45672, 0, 0, 0, 0, 0, 0, 4994.43054, 518081, 7366.31, 2984563,
"8/24/19", 1770.50415, 38553, 0, 0, 0, 0, 0, 0, 5001.303585, 551733, 6275.43, 2531400,
)
Below is the code.
output$saleplot <- renderPlotly({
plot_ly(x = ~d1$Date, y = ~d1$apple_sale, type = 'scatter', mode = 'none', name = 'apple', fill = 'tozeroy',fillcolor = 'rgba( 71,238,171, 0.5)') %>%
add_trace(x = ~d1$Date, y = ~d1$banana_sale, name = 'banana',fill = 'tozeroy', fillcolor = 'rgba(255,0,127,0.5)') %>%
add_trace(x = ~d1$Date, y = ~d1$orange_sale, name = 'orange',fill = 'tozeroy', fillcolor = 'rgba(255,255,51, 0.5)') %>%
add_trace(x = ~d1$Date, y = ~d1$peach_sale, name = 'peach',fill = 'tozeroy', fillcolor = 'rgba(127,0,255,0.5)') %>%
add_trace(x = ~d1$Date, y = ~d1$watermelon_sale, name = 'watermelon',fill = 'tozeroy', fillcolor = 'rgba(255,0,0, 0.5)')%>%
add_trace(x = ~d1$Date, y = ~d1$strawberry_sale, name = 'strawberry',fill = 'tozeroy', fillcolor = 'rgba(0,102,204, 0.5)') %>%
layout(title = 'sale By Day',
# xaxis = list(title = 'Date',rangeslider = list(type = "date")),
xaxis = list(title = 'Date'),
yaxis = list(title = 'sale'))
})