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:

Clustered Bar Plot in R

  • 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

Similar Reads

Clustered Bar Plot

...

Simple Clustered Bar Plot

A clustered bar chart is a horizontal chart, which could present multiple bars in the form of a cluster. The horizontal bars basically group together, as they are under the same, y values. We have various options to format clustered bar charts, we can change the value of the x-axis, y-axis, its title, etc. In this article, we will learn how to format a clustered bar chart in Power BI and explore its various options....

Grouped Stacked Bar Plot

R # Load the ggplot2 package library(ggplot2)   # Sample data data <- data.frame(   Category = c("A", "B", "C", "D"),   Group1 = c(10, 15, 7, 12),   Group2 = c(8, 11, 9, 14) )   # Create a simple clustered bar plot ggplot(data, aes(x = Category)) +   geom_bar(aes(y = Group1, fill = "Group 1"), stat = "identity", position = "dodge") +   geom_bar(aes(y = Group2, fill = "Group 2"), stat = "identity", position = "dodge") +   labs(title = "Simple Clustered Bar Plot", y = "Value") +   scale_fill_manual(values = c("Group 1" = "blue", "Group 2" = "red")) +   theme_minimal()...

Clustered Bar Plot with Multiple Groups

...

Clustered Bar Plot in R using Plotly

R # Load the ggplot2 package library(ggplot2)   # Sample data sales_data <- data.frame(   Category = c("Electronics", "Clothing", "Books", "Furniture"),   North = c(250, 180, 90, 450),   South = c(220, 150, 140, 310),   West = c(280, 100, 100, 380) )   # Create a visually appealing grouped stacked bar plot ggplot(sales_data, aes(x = Category)) +   geom_bar(aes(y = North, fill = "North"), stat = "identity") +   geom_bar(aes(y = South, fill = "South"), stat = "identity") +   geom_bar(aes(y = West, fill = "West"), stat = "identity") +   labs(     title = "Sales by Product Category and Region",     y = "Total Sales",     fill = "Region"   ) +   scale_fill_manual(     values = c("North" = "#1f77b4", "South" = "#ff7f0e", "West" = "#2ca02c"),     labels = c("North Region", "South Region", "West Region")   ) +   theme_minimal() +   theme(legend.position = "top")...

Conclusion

...