library(ggplot2)
library(cowplot)

mydata_signals = read.csv("./signals.csv")
mydata_events = read.csv("./events.csv")

total <- merge(mydata_signals,mydata_events,by="fsatime", all=TRUE)

vertical_adjust <- 1
label_size <- 20

pe <- ggplot(data=total , aes(x=fsatime, y=Event, group=1)) +
  geom_point(na.rm = FALSE) +
  ylab("Event") + 
  xlab("Time (s)") + 
  scale_y_discrete("Event", labels = waiver(), limits=c("when(x +-)", "kick")) +
  theme(axis.title.y=element_text(vjust=vertical_adjust), axis.title=element_text(size=label_size,face="bold"))

ps <- ggplot(data=total , aes(x=fsatime, y=fsastate, group=1)) +
  geom_line() +
  ylab("State") + 
  xlab("Time (s)") + 
  scale_y_discrete("State", labels = waiver(), limits=c("Initial", "FreeFall", "Collision", "Kicked")) +
  theme(axis.title.y=element_text(vjust=vertical_adjust), axis.title=element_text(size=label_size,face="bold"))

px <- ggplot(data=total , aes(x=fsatime, y=x, group=1)) +
        geom_line() +
        ylab("Height (m)") + 
        xlab("Time (s)") +
        theme(axis.title.y=element_text(vjust=vertical_adjust), axis.title=element_text(size=label_size,face="bold"))

pv <- ggplot(data=total , aes(x=fsatime, y=v, group=1)) +
        geom_line() +
        ylab("Velocity (m/s)") + 
        xlab("Time (s)") +
        theme(axis.title.y=element_text(vjust=vertical_adjust), axis.title=element_text(size=label_size,face="bold"))

pt <- ggplot(data=total , aes(x=fsatime, y=current_cbdtime, group=1)) +
        geom_point() +
        ylab("CBD Time (s)") +
        xlab("FSA Time (s)") +
        scale_y_continuous(limits=c(0, 10)) +
        scale_x_continuous(limits=c(0, 10)) +
        theme(axis.title.y=element_text(vjust=vertical_adjust), axis.title=element_text(size=label_size,face="bold"))

row_plot <- plot_grid(pe, ps, px , pv, pt, nrow = 1, ncol = 5, align = "h")
save_plot("bbplots.pdf", row_plot, nrow=1, ncol=5, base_aspect_ratio = 1.0)

