rggplot2plotggfortify

Add Dot to Line Chart using ggplot2 in R


I have data, i name it "mytransaction.plot"

datex <- c(rep("2021-07-23", 19), rep("2021-07-24", 21), rep("2021-07-25", 17),
           rep("2021-07-26", 19), rep("2021-07-27", 16), rep("2021-07-28", 17),
           rep("2021-07-29", 14), rep("2021-07-30", 17), rep("2021-07-31", 17))
hourx <- c(5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,0,1,4,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,2,4,5,7,8,9,10,11,12,13,14,16,17,18,20,21,22,0,1,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,0,8,10,11,12,13,14,15,16,17,18,19,20,21,22,23,0,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,0,1,10,11,12,13,14,15,16,18,19,21,22,23,0,1,2,9,10,11,12,13,14,15,16,17,18,19,20,21,22,2,6,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22)
transaction <- c(3,9,7,18,5,3,6,9,7,9,4,5,9,14,3,5,1,1,2,2,2,2,2,1,1,1,1,2,6,4,8,6,3,4,2,2,3,3,2,1,1,2,1,1,4,1,2,4,5,1,4,2,7,4,1,1,2,1,1,3,3,3,8,9,23,87,150,171,120,52,4,5,2,5,6,4,2,1,4,4,9,5,7,5,7,6,8,5,9,4,6,6,3,2,4,5,10,6,7,5,9,5,7,5,9,11,8,2,1,2,1,2,8,16,12,15,8,1,2,3,1,4,11,16,13,4,2,4,12,9,7,11,16,15,15,14,8,7,5,1,1,1,3,2,3,2,18,16,11,9,1,12,15,16,6,4,2)
mytransaction.plot <- data.frame(datex, hourx, transaction)

here the graph: enter image description here

And this is my other data, i name it "mytransaction.dot".

datex <- rep("2021-07-26", 6)
hourx <- 12:17
transaction <- c(23,87,150,171,120,52)
mytransaction.dot <- data.frame(datex, hourx, transaction)

The question is, how do i add transaction data from mytransaction.dot into graph from mytransaction.plot but with Dot Appearance using ggplot2. The Result will be:

enter image description here

Many Thank You!!!


Solution

  • Libraries

    library(tidyverse)
    library(lubridate)
    

    Data

    Line

    datex <- c(rep("2021-07-23", 19), rep("2021-07-24", 21), rep("2021-07-25", 17),
               rep("2021-07-26", 19), rep("2021-07-27", 16), rep("2021-07-28", 17),
               rep("2021-07-29", 14), rep("2021-07-30", 17), rep("2021-07-31", 17))
    hourx <- c(5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,0,1,4,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,2,4,5,7,8,9,10,11,12,13,14,16,17,18,20,21,22,0,1,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,0,8,10,11,12,13,14,15,16,17,18,19,20,21,22,23,0,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,0,1,10,11,12,13,14,15,16,18,19,21,22,23,0,1,2,9,10,11,12,13,14,15,16,17,18,19,20,21,22,2,6,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22)
    transaction <- c(3,9,7,18,5,3,6,9,7,9,4,5,9,14,3,5,1,1,2,2,2,2,2,1,1,1,1,2,6,4,8,6,3,4,2,2,3,3,2,1,1,2,1,1,4,1,2,4,5,1,4,2,7,4,1,1,2,1,1,3,3,3,8,9,23,87,150,171,120,52,4,5,2,5,6,4,2,1,4,4,9,5,7,5,7,6,8,5,9,4,6,6,3,2,4,5,10,6,7,5,9,5,7,5,9,11,8,2,1,2,1,2,8,16,12,15,8,1,2,3,1,4,11,16,13,4,2,4,12,9,7,11,16,15,15,14,8,7,5,1,1,1,3,2,3,2,18,16,11,9,1,12,15,16,6,4,2)
    mytransaction.plot <-
       data.frame(datex, hourx, transaction) %>% 
       mutate(datex = ymd(datex),
              #Create datetime object with datex + hourx
              datetime = ISOdatetime(
                 year = year(datex),
                 month = month(datex),
                 day = day(datex),
                 hour = hourx,
                 min = 0,
                 sec = 0))
    

    Point

    datex <- rep("2021-07-26", 6)
    hourx <- 12:17
    transaction <- c(23,87,150,171,120,52)
    mytransaction.dot <-
       data.frame(datex, hourx, transaction) %>% 
       #Transform datex to date
       mutate(datex = ymd(datex),
              #Create datetime object with datex + hourx
              datetime = ISOdatetime(
                 year = year(datex),
                 month = month(datex),
                 day = day(datex),
                 hour = hourx,
                 min = 0,
                 sec = 0))
    

    Plot

    mytransaction.plot %>%
       ggplot(aes(datetime,transaction))+
       # Add line
       geom_line()+
       # Add point, with data.frame mytransaction.dot
       geom_point(data = mytransaction.dot)
    

    Output

    enter image description here