Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in R Programming by (50.2k points)

I am having 2 columns in my dataframe that I am trying to use ggplot to the graph. On the x-axis, I want the date which has a daily frequency. On the y-axis, I want the number of unique names that show up on that given day.

The variables look something like this in the dataframe.

     Date           Name

1   2016-03-01      Joe

2   2016-03-01      Joe

3   2016-03-01      Joe 

4   2016-03-01      Mark

5   2016-03-01      Sue

6   2016-03-02      Mark    

7   2016-03-02      Joe

8   2016-03-03      Joe

9   2016-03-03      Joe

10  2016-03-03      Bill

So the frequency on the y-axis on the first day would show 3, 2 on the second, and 2 on the third.

My main question is how do I produce that graph.

1 Answer

0 votes
by (108k points)

The count number of unique Names for each Date and then plot with geom_bar/geom_col.

library(dplyr)

library(ggplot2)

df %>%

  group_by(Date) %>%

  summarise(n = n_distinct(Name)) %>%

  ggplot()  + geom_col(aes(Date, n))

  #ggplot() + geom_bar(aes(Date, n), stat = "identity")

If you are interested in R certification, then do refer to the following R programming certification.

Browse Categories

...