Hash Slasher
Hash Slasher

Reputation: 11

How to count Number Of Occurences and Use it for Line Chart in R

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?

Upvotes: 1

Views: 1033

Answers (1)

ToWii
ToWii

Reputation: 660

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()

Upvotes: 2

Related Questions