在 R 中使用 ggplot2 将点添加到折线图

Add Dot to Line Chart using ggplot2 in R

我有数据,我将其命名为“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)

这里是图表:

这是我的其他数据,我将其命名为“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)

问题是,如何使用 ggplot2 将 mytransaction.dot 中的交易数据添加到 mytransaction.plot 中的图形中,但具有点外观。结果将是:

非常感谢!!!

图书馆

library(tidyverse)
library(lubridate)

数据

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

点数

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

情节

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

输出