I have data that looks like this:
Date Title Units
1 2023-11-18 L 2
2 2023-11-19 L 1
3 2023-11-23 L 1
4 2023-11-27 L 2
5 2023-11-28 L 1
6 2023-12-07 L 1
7 2024-01-24 A 1
8 2024-01-25 A 1
9 2024-01-27 A 1
10 2024-01-28 A 3
... etc.
I want to create a stacked barplot using base R that looks like this (not the colors etc., just the principle):
I tried:
> barplot(Units ~ Title, obp)
Error in barplot.formula(Units ~ Title, obp) :
duplicated categorical values - try another formula or subset
I don’t understand how to do this! (No ggplot2!)
Sample data:
obp <- structure(list(Date = structure(c(19679, 19680, 19684, 19688,
19689, 19698, 19746, 19747, 19749, 19750, 19752, 19753, 19753,
19756, 19764, 19767, 19770, 19778, 19782, 19782, 19790, 19793,
19795, 19800, 19802, 19803, 19803, 19803, 19804, 19805, 19805,
19805, 19807, 19809, 19809, 19809, 19810, 19812, 19815, 19816,
19821, 19828, 19829, 19829, 19831, 19831, 19835, 19836, 19837,
19838, 19839, 19840, 19842, 19848, 19849, 19850, 19851, 19854,
19855, 19856, 19858, 19858, 19859, 19860, 19862, 19865, 19868,
19870, 19871, 19871, 19872, 19877, 19878, 19878, 19883, 19884,
19884, 19886, 19891, 19891), class = "Date"), Title = structure(c(3L,
3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 1L,
2L, 3L, 2L, 2L, 1L, 1L, 1L, 4L, 1L, 4L, 2L, 4L, 1L, 4L, 2L, 4L,
1L, 4L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 4L, 4L, 4L,
4L, 4L, 4L, 1L, 4L, 4L, 4L, 2L, 4L, 2L, 4L, 1L, 4L, 1L, 4L, 2L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 4L, 4L, 1L, 4L), levels = c("A",
"D", "L", "C"), class = "factor"), Units = c(2L, 1L, 1L, 2L,
1L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L)), class = "data.frame", row.names = c(NA,
-80L))
New contributor
Ben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.