0

so for example i got this dataframe

Brand owner Period
Adidas andy May 2018
Nike diana June 2019
Adidas rose August 2019
Nike sara July 2020
Puma laura March 2020
Joma harry April 2018
Adidas jon May 2018
Diadora keith June 2021

how do i count the number of occurences of the brand and use it as a y axis, period for x axis (yearmon num), and group by owner to make a chart line?

  • Welcome to SO. It is helpful if you can make your question reproducible. See https://stackoverflow.com/help/minimal-reproducible-example and https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Dylan_Gomes Jun 30 '21 at 21:34

1 Answers1

1

Unfortunately, the number of obs is too little to run it properly. But transform Period to a date format for proper display (maybe use lubridate or zoo as package). Then run the following:

library(ggplot2)

df %>%
  group_by(Brand, owner, Period) %>%
  add_count() %>%
  ungroup() %>%
  ggplot(aes(Period, n, group=owner, col=owner) +
  geom_line()
ToWii
  • 590
  • 5
  • 8
  • what if i want to group it by brand and use the number of occurrence as the y axis, and the period( yearmon) as the x axis? – Hash Slasher Jul 01 '21 at 15:45
  • Try `df %>% group_by(Brand, Period) %>% add_count() %>% ungroup() %>% ggplot(aes(Period, n)) + geom_line()`. Then, of course, no grouping by `owner` obtains. Just one remark: Be so kind to accept my post as answer to your question, if it answers your questions. Thanks! – ToWii Jul 02 '21 at 16:53