I have some data that looks like this
# Generate example data
exampleData <- data.frame(Month = sample(1:5, 500, replace = T),
Product = sample(LETTERS[1:10], 500, replace = T),
Site = sample(letters[1:5], 500, replace = T),
Used = sample(1:100, 500, replace = T))
exampleData <- aggregate(. ~ Month + Product + Site, data = exampleData, sum) # Consolidating any duplicates
exampleData <- exampleData[order(exampleData$Month, exampleData$Product, exampleData$Site, exampleData$Used),]
I wanted to see trends in different products at different sites, so created this function
# Funciton to retrieve info about a product and site
productSiteInfo <- function(p, s) {
return(exampleData[intersect(which(exampleData$Product == p), which(exampleData$Site == s)),])
}
To make my comparisons easier, I want to make a grid of line plots, where the grid consits of plots of a specific product at all the sites. So I tried this code
# Plotting the data
prods <- unique(exampleData$Product) # All products
prod <- sample(prods,1) # Select a product of interest
sites <- unique(exampleData$Site) # All sites
par(mfrow=c(3,2)) # Create grid
lapply(head(sites), function(site) { # Plot trend of prod at all sites
aDF <- productSiteInfo(prod, site)
ggplot() +
geom_line(data = aDF, aes(x = Month, y = Used), color = "black") +
xlab("Month") +
ylab("Units") +
ggtitle(paste("Consumption of", prod, "at", site))
})
But it's not working as expected. I'm not getting a grid of plots, but just individual plots. I was wondering why that was, and what I can do to get that grid. My actual data has ~10 products and ~160 sites, so it's gonna be much larger than this example.
Thanks for the help!