1

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 Answer 1

1

** 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

Sign up to request clarification or add additional context in comments.

4 Comments

This isn't quite it, I need a line for Starts and a line for Submits, two lines one plot.
This looks more right, can u update the plot?
@John Thomas I updated the graph after I had initially updated the code. That was before your comment, though. The graph is aligned with the code. Maybe you were looking at it while I was changing it?
FYI @Kat, you can read in the data more easily using read.table("clipboard", header = TRUE) on windows (and linux, I think) and read.table(pipe("pbpaste"), header=TRUE) on macos (both after highlighting the data text in the question and copying). It'll read this in as a two-column frame without the need for separate(.) (though it still needs mdy or as.Date).

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.