Data visualization is key when presenting our analysis and results, for that reason today we will see the plotly library.
This library allows us to create interactive charts very useful for the end user.
To perform this exercise we need the following libraries: plotly and crosstalk.
First of all we load our dataset to be able to play with the data. In this case, it is the number of sales per store in a country.
Example <- read.csv("Example.csv") head(Example)
## X Shop Sales ## 1 1 Shop00002 374 ## 2 2 Shop00005 84 ## 3 3 Shop00007 142 ## 4 4 Shop00008 0 ## 5 5 Shop00009 67
And here the code with a brief explanation:
# Here we specify what the variable will be to select Selectize <- SharedData$new(Example, ~Shop, "Select a Shop") #In this place we created the graph p <- plot_ly(Selectize, color = ~Shop) %>% add_trace(x = ~Shop, y = ~Sales, type = 'bar', text = ~Sales, textposition = 'auto', marker = list(color = 'Shop', line = list(color = 'rgb(8,48,107)', width = 1.5))) %>% layout(title = paste0("Sales by Shop"), barmode = 'group', xaxis = list(title = "Shop"), yaxis = list(title = "Sales")) #Here we create the relationship between the selector and the graph select <- highlight( ggplotly(p, tooltip = "Shop"), selectize = TRUE, persistent = TRUE ) bscols(select)
And finally we obtain a selectable graph that allows us to analyze the results in a visual way. yo can see the result here!