Making a line graph using ggplot2 using dates

Viewed 43

So I have a list of dates and corresponding data as follows:

DATE        Event
7/14/2021   Start
7/14/2021   Start
7/14/2021   Start
7/14/2021   Start
7/13/2021   Start
7/12/2021   Start
7/12/2021   Start
7/12/2021   Start
7/12/2021   Start
7/12/2021   Start
7/12/2021   Start
7/11/2021   Start
7/11/2021   Start
7/6/2021    Submit
6/30/2021   Submit
6/25/2021   Submit
6/24/2021   Submit
6/23/2021   Submit
6/22/2021   Submit
6/22/2021   Submit

All I want to do is have 1 plot with two lines:

  • x-axis: DATE
  • y-axis: count of records
  • Line 1: Starts
  • Line 2: Submits

I'm trying to do this with ggplot and the tidyverse, any help is welcome!!

1 Answers

** saw your comment, if you want to have separate lines, based on the Event field, use the argument group in the function ggplot(). You can add color by Event, as well. I've added it to the code.

This should work for you. Look for the inline comment that starts with "# this is the part you're interested in."

# original data
df = data.frame(DATE........Event = c("7/14/2021   Start",
                                      "7/14/2021   Start","7/14/2021   Start",
                                      "7/14/2021   Start","7/13/2021   Start",
                                      "7/12/2021   Start","7/12/2021   Start","7/12/2021   Start",
                                      "7/12/2021   Start","7/12/2021   Start",
                                      "7/12/2021   Start","7/11/2021   Start","7/11/2021   Start",
                                      "7/6/2021   Submit","6/30/2021   Submit",
                                      "6/25/2021   Submit","6/24/2021   Submit",
                                      "6/23/2021   Submit","6/22/2021   Submit","6/22/2021   Submit")
)

# had to fix the data, because of the way you posted it
names(df)[1] <- "Date"
df <- separate(df, Date, into = c("Date", "Event"), sep = "   ")

# this is the part you're interested in
# first you need the date to be a date object
# the library lubridate is the best option for simplicity
df <- df %>% mutate(Date = lubridate::mdy(Date))

# then you can graph the count
#               and group by event**
ggplot(df, aes(x = Date, 
               group = Event,
               color = Event)) + 
  geom_line(stat = "count")

This is what it looks like: enter image description here

Related