1

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?

1

1 Answer 1

2

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()
Sign up to request clarification or add additional context in comments.

2 Comments

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?
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.