Welcome to R and the RStudio Community! We're glad you're here. You're in luck! The solution you've proposed would be a very reasonable (yet complex) solution in a spreadsheet environment like Excel, but it is trivially easy to do in R, using packages like
A couple notes:
- I really appreciate your clear problem statement, and the fact that you've put some thought into solving this before posting.
- For future reference, it is really helpful if you can post code that generates your sample data. I did it for you in this case because it was pretty straightforward, but you'll have the best chance to get help with your problem if you're not asking people to re-create your problem themselves first. See here for some tips on how to do this.
- Finally, your solution!
# Simulate data
dates <- seq.Date(as.Date("2016/1/1"), as.Date("2019/6/1"), by = "month")
df_data <- data.frame(
date_gathered = dates,
what_shipped = sample(
c("apples", "oranges", "grapes"),
replace = TRUE
# Enter your desired category here
type <- "apples"
# Mutate the data to get a year column
df_year <- df_data %>%
mutate(yr = year(date_gathered)) %>%
filter(what_shipped == type)
# Generate the plot
gg_fruit <- ggplot(df_year) +
geom_bar(aes(yr), stat = "count")
One last note! The solution to a question like this, along with loads of other useful stuff, can be found in hadley's fantastic book, R for Data Science.