Reputation: 51
I have a question regarding bar plots in R. I have a dataset with 4 variables (Month, Year, Location and Number of Births) with 12 different months, 3 different years and 4 different locations.
Month Birth Year Location
Jan 3 2017 A
Feb 6 2017 A
Mar 8 2017 A
Apr 9 2017 A
Is it possible to create a bar plot with 4 graphs (each one for one year) and within them x-axis=Month, y-axis=Births, per location. Grouped or stack bar plot is the same for me.
Thanks a lot for your help in advance!
Upvotes: 0
Views: 78
Reputation: 1700
You can do this with ggplot2
and then use facet_grid
or facet_wrap
.
library(tidyverse)
data <- tibble(
Month = rep(c("Jan", "Feb", "Mar"), 4),
Birth = c(1:12),
Year = c(rep("2017", 3), rep("2018", 3),rep("2017", 3), rep("2018", 3)),
Location = c(rep("A", 6), rep("B", 6))
)
ggplot(data, aes(x = Month, y=Birth)) +
geom_bar(stat='identity', position = 'stack', aes(fill = Location)) +
facet_grid(~Year)
Created on 2021-04-28 by the reprex package (v0.3.0)
Upvotes: 1