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)

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=-4))
pe

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=-2))
ps

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=-6))
px

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=-6))
pv

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=-6))
pt

multiplot <- plot_grid(pe, ps, px , pv, pt, nrow = 3, ncol = 2, align = "v")
px_pv_plot <- plot_grid(px, pv, nrow=1, ncol=2, align="h")
ntplot <- plot_grid(pt, nrow=1, ncol=1, align="v")
col_plot <- plot_grid(pe, ps, px , pv, pt, nrow = 5, ncol = 1, align = "v")
row_plot <- plot_grid(pe, ps, px , pv, pt, nrow = 1, ncol = 5, align = "h")

save_plot("plots.pdf", multiplot, nrow = 3, ncol = 2, base_aspect_ratio = 1.2)
save_plot("ntplot.pdf", ntplot, nrow=1, ncol=1, base_aspect_ratio = 1)
save_plot("bbplots_vertical.pdf", col_plot, nrow=5, ncol=1, base_aspect_ratio = 1.0)
save_plot("bbplots.pdf", row_plot, nrow=1, ncol=5, base_aspect_ratio = 1.0)
save_plot("bb_vhplots.pdf", row_plot, nrow=1, ncol=2, base_aspect_ratio = 1.0)

