Search code examples
rplotline

Plot points of one table onto the line graph of another table


I plotted both a line graph, and a scatter-like plot of my data. The line graph portrays the continuous measurements I took over a few months. The scatted-like plot shows the measurements I took by hand, to check the reliability of the continuous data.

Therefore, I would like to plot the points from the hand measurements onto the line graph, to see whether they overlap. Is it possible to combine these plots into one?

Thank you for your time.

Hereby my code thus far...

> dput(head(MW3, 10))
structure(list(MonitoringPoint = c("Pb 3-1", "Pb 3-1", "Pb 3-1", 
"Pb 3-1", "Pb 3-1", "Pb 3-1", "Pb 3-1", "Pb 3-1", "Pb 3-1", "Pb 3-1"
), SerialNumber = c("R1453", "R1453", "R1453", "R1453", "R1453", 
"R1453", "R1453", "R1453", "R1453", "R1453"), Date_Time = structure(c(1680804000, 
1680807600, 1680811200, 1680814800, 1680818400, 1680822000, 1680825600, 
1680829200, 1680832800, 1680836400), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), Date = structure(c(1680739200, 1680739200, 1680739200, 
1680739200, 1680739200, 1680739200, 1680825600, 1680825600, 1680825600, 
1680825600), tzone = "UTC", class = c("POSIXct", "POSIXt")), 
    Time = structure(c(-2209010400, -2209006800, -2209003200, 
    -2208999600, -2208996000, -2208992400, -2209075200, -2209071600, 
    -2209068000, -2209064400), tzone = "UTC", class = c("POSIXct", 
    "POSIXt")), Pressure = c(1089.85, 1090.025, 1090.375, 1090.725, 
    1091.075, 1091.775, 1092.35833333333, 1093.23333333333, 1093.75833333333, 
    1094.45833333333), Temperature = c(8.29, 8.28666666666667, 
    8.28333333333334, 8.28333333333334, 8.28, 8.28, 8.28, 8.28, 
    8.27666666666667, 8.27333333333333), Conductivity = c(1209, 
    1209, 1209, 1208, 1209, 1210, 1215, 1217, 1220, 1222), TOC_MV_cm = c(4, 
    4, 4, 4, 4, 4, 4, 4, 4, 4), WaterLevel_TOC_cm = c(64.0600000000002, 
    63.7829999999999, 63.433, 63.0830000000001, 62.7329999999999, 
    61.9309999999998, 61.6536666666666, 60.9826666666665, 60.3556666666668, 
    59.7576666666664), WaterLevel_MV_cm = c(60.0600000000002, 
    59.7829999999999, 59.433, 59.0830000000001, 58.7329999999999, 
    57.9309999999998, 57.6536666666666, 56.9826666666665, 56.3556666666668, 
    55.7576666666664), WaterLevel_MV_m = c(0.600600000000002, 
    0.597829999999999, 0.59433, 0.590830000000001, 0.587329999999999, 
    0.579309999999998, 0.576536666666666, 0.569826666666665, 
    0.563556666666668, 0.557576666666664), PB_NAP_MV_m = c(77.016, 
    77.016, 77.016, 77.016, 77.016, 77.016, 77.016, 77.016, 77.016, 
    77.016), WaterLevel_NAP_m = c(76.4154, 76.41817, 76.42167, 
    76.42517, 76.42867, 76.43669, 76.4394633333333, 76.4461733333333, 
    76.4524433333333, 76.4584233333333), WaterLevelReference = c("WATER HEAD (TOC-CL)", 
    "WATER HEAD (TOC-CL)", "WATER HEAD (TOC-CL)", "WATER HEAD (TOC-CL)", 
    "WATER HEAD (TOC-CL)", "WATER HEAD (TOC-CL)", "WATER HEAD (TOC-CL)", 
    "WATER HEAD (TOC-CL)", "WATER HEAD (TOC-CL)", "WATER HEAD (TOC-CL)"
    ), Salinity = c(0.601516, 0.601516, 0.601516, 0.600992, 0.601516, 
    0.60204, 0.60466, 0.605708, 0.60728, 0.608328)), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))



> dput(head(Manual_waterlevel_3, 10))
structure(list(Locatie = c("3_1", "3_1", "3_1", "3_1", "3_1", 
"3_1"), Datum = structure(c(1681257600, 1682380800, 1683849600, 
1684800000, 1686873600, 1696896000), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), `Pb t.o.v. mv (cm)` = c(4, 4, 4, 4, 4, 4), `H2O t.o.v. Pb (of dergelijke) (cm)` = c(50, 
56, 47, 66, 107, 61), `H2O t.o.v. Pb (of dergelijke) (m)` = c(0.5, 
0.56, 0.47, 0.66, 1.07, 0.61), `H2O t.o.v. mv (cm)` = c(46, 52, 
43, 62, 103, NA), `H2O t.o.v. mv (m)` = c(0.46, 0.52, 0.43, 0.62, 
1.03, NA), `NAP mv (m)` = c(77.016, 77.016, 77.016, 77.016, 77.016, 
77.016), H20_NAP = c(76.556, 76.496, 76.586, 76.396, 75.986, 
76.446)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", 
"data.frame"))

# Import data set of the diver measurements
library(readxl)
MW3 <- read_excel("C:/Users/923721/OneDrive - Royal HaskoningDHV/Internship documents/Data_Weustenrade/Waterstanden/MW3.xlsx")

# Obtain the average water level per day (removing the time component)
Water_level_perday <- MW3 %>%
  mutate(date = floor_date(Date)) %>%
  group_by(Date) %>%
  summarize(mean_waterlevel = mean(WaterLevel_NAP_m))

# Plot diver measurement data
Diver <- ggplot(Water_level_perday, aes(x = Date, y = mean_waterlevel)) + geom_line()  + labs(x = "Time", y = "Water level_NAP (m)") + theme_classic() 

# Load data set of the manual measurements
library(readxl)
Manual_waterlevel_3 <- read_excel("C:/Users/923721/OneDrive - Royal HaskoningDHV/Internship documents/Data_Weustenrade/Waterstanden/Manual_waterlevel_3.xlsx")

# Plot manual measurement data
Manual <- ggplot(Manual_waterlevel_3, aes(x = Datum, y = H20_NAP)) + geom_point()  + labs(x = "Time", y = "Water level_NAP (m)") + theme_classic() 

Solution

  • As I wrote in my comment, to combine plots based on two different data frames, you simply need to provide the appropriate value for the data parameter in each geom_xxxx call. Normally, the value data comes from the start of the pipe or the call to ggplot(). Similarly, you can move the various aes() around as well.

    Based on your dput data and summarising MW3 as you do,

    Water_level_perday %>% 
      ggplot(aes(x = Date, y = mean_waterlevel)) + 
      geom_line()  + 
      geom_point(data = Manual_waterlevel_3, aes(x = Datum, y = H20_NAP))  + 
      labs(x = "Time", y = "Water level_NAP (m)") + 
      theme_classic() 
    

    gives

    enter image description here