How can I create a faceted ggplot histogram with multiple grouping variables in R?
I have a dataset with columns: product, category, sales. I want to create a histogram of sales faceted by both product and category.
Here’s a sample of my data:
library(dplyr)
library(ggplot2)
data <- data.frame(
product = rep(c("A", "B", "C"), each = 10),
category = rep(c("X", "Y"), 15),
sales = sample(100:200, 30, replace = TRUE)
)
Tried: facet_grid(product ~ category)
.
Expected: Separate histograms for each product-category combo.
Result: Incorrect facetting, histograms overlapped.
New contributor
wainaina_felix is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.