ggplot geom_bar(position="dodge") not working

15 views Asked by At
library(ggplot2)

# Sample data
toy <- data.frame(Category = c("A", "B"), Value1 = c(10, 15), Value2 = c(8, 12))

# Create a bar chart with dodged bars and adjusted widths
ggplot(toy, aes(x = Category)) +
  geom_bar(aes(y = Value1), stat = "identity", position = "dodge", fill = "blue", width = 0.4) +
  geom_bar(aes(y = Value2), stat = "identity", position = "dodge", fill = "red", width = 0.4)

Running code, I see the for each category, the red and the blue bars overlap despite position="dodge". How do I fix this and have red and blue bars side by side for each category?

width adjusting didn't help. position=position_dodge() was unproductive.

0

There are 0 answers