Home > Back-end >  Obtaining geom_line values at mid-month for unequally spread geom_points
Obtaining geom_line values at mid-month for unequally spread geom_points

Time:03-15

I need to obtain value at 15th of every month from the geom line created using the reproducible example below. The original geom points are at varied monthly dates depending on data collection days. Appreciate any help possibleenter image description here

Value <- c(19.14104, 11.72115, 9.66083, 10.99109, 13.65047, 10.10627, 13.53027, 18.25272, 25.57741)
Dates <- c("07/11/19", "28/11/19", "16/12/19", "10/01/20", "21/01/20", "03/02/20", "04/03/20", "19/03/20", "20/05/20")

df <- data.frame(Value, Dates)
View(df)

df$Dates <- lubridate::dmy(df$Dates)


gpp_plot <- ggplot()   
  geom_point(data = df, aes(x=Dates, y=Value), alpha=0.5)  
  geom_line(data = df, aes(x=Dates, y=Value), alpha=0.5)  
  theme(axis.text.y=element_text(size=10),
        axis.title=element_text(size=10, , face = "bold"),
        axis.text.x = element_text(size=10, face = "bold", angle = 45, vjust = 0.5)) 
  xlab('')  
  ylab('LI-7810_FCH4_DRY_[nmol 1m-2s-1]')  
  scale_x_date(date_labels = "%b %y",breaks = "1 months")  
  theme(legend.position="none")
gpp_plot

CodePudding user response:

As suggested in comments, you could approx the Value at new dates:

library(ggplot2)
library(lubridate)

Dates <- dmy(Dates)

NewDates <- seq(floor_date(min(Dates),'month'),floor_date(max(Dates),'month'),by='month') days(14)

#[1] "2019-11-15" "2019-12-15" "2020-01-15" "2020-02-15" "2020-03-15" "2020-04-15" "2020-05-15"

NewValue <- approx(Dates,Value,NewDates)$y

df <- data.frame(Value=c(Value,NewValue), Dates=c(Dates,NewDates))


gpp_plot <- ggplot()   
  geom_point(data = df, aes(x=Dates, y=Value), alpha=0.5)  
  geom_line(data = df, aes(x=Dates, y=Value), alpha=0.5)  
  theme(axis.text.y=element_text(size=10),
        axis.title=element_text(size=10, , face = "bold"),
        axis.text.x = element_text(size=10, face = "bold", angle = 45, vjust = 0.5)) 
  scale_x_date(date_labels = "%b %y",breaks = "1 months")  
  ylab('LI-7810_FCH4_DRY_[nmol 1m-2s-1]')  

  theme(legend.position="none")
gpp_plot

  • Related