Clustered Bar Plot in R using Plotly
R
# Load the plotly package library (plotly) # Sample data data <- data.frame ( Month = c ( "January" , "February" , "March" , "April" ), ProductA = c (100, 120, 90, 110), ProductB = c (80, 110, 85, 100) ) # Create a clustered bar plot with plotly plot_ly (data, x = ~Month) %>% add_bars (y = ~ProductA, name = "Product A" , marker = list (color = "pink" )) %>% add_bars (y = ~ProductB, name = "Product B" , marker = list (color = "yellow" )) %>% layout ( title = "Sales Comparison of Product A and Product B" , xaxis = list (title = "Month" ), yaxis = list (title = "Sales" ) ) |
Output:
- The plot_ly function initializes a plotly plot, specifying the data frame (data) and mapping the x-axis to the Month variable.
- The add_bars function is used twice to add bars for Product A and Product B, with different colors.
- The layout function sets the title, x-axis label, and y-axis label for the plot.
- This code will generate an interactive clustered bar plot comparing the sales of Product A and Product B across different months, with the specified title and axis labels. we can hover over the bars to see the exact sales values for each product in each month, making it an interactive and informative visualization.
Clustered Bar Plot in R
One of the most popular packages for data visualisation is ggplot2, which can be used to create a clustered bar plot in R.
Table of Content
- Clustered Bar Plot
- Simple Clustered Bar Plot
- Grouped Stacked Bar Plot
- Clustered Bar Plot with Multiple Groups
- Clustered Bar Plot in R using Plotly
- Conclusion