I have made a function to analyze a segment of time series data. It returns 2 ggplots that are fed a melted data frame. The time series has fractional seconds. When the code is ran in rstudio the output is correct but when knit to a file for a report the fractional seconds appear to clumped at each second .00 instance.
Rstudio output (expected report output) enter image description here Output when knitted to any type of document enter image description here
Code
library(dplyr)
library(lubridate)
library(magrittr)
library(reshape2)
library(ggplot2)
library(grid)
library(GGally)
library(gridExtra)
library(ggplotify)
library(plotly)
library(car)
library(cowplot)
SegmentAnalysis \<- function(df, start, end, primary_marker = NA, secondary_marker = NA){
e1 \<- read.csv(df, header = T)
marker \<- data.frame(timestrip = primary_marker, variable = "marker", value = 0)
e1$timestrip \<- as.numeric(format(as.POSIXct(e1$datetime,
format = '%Y-%m-%dT%H:%M:%OS'),
format = '%H%M%OS'))
e1$timestrip \<- strptime(e1$timestrip, format = '%H%M%OS')
start \<- strptime(start, format = "%H%M%OS")
end \<- strptime(end, format = "%H%M%OS")
marker$timestrip \<- strptime(marker$timestrip, format = "%H%M%OS")
marker$timestrip \<- as.POSIXct(marker$timestrip)
st_out \<- start - 40
ed_out \<- end + 40
e2 \<- dplyr::filter(e1, timestrip \>= st_out & timestrip \<= ed_out)
e2GaugeReading \<- (e2\[,c(3,5:6, 8,10, 12)\])
e2GaugeReading$timestrip \<- as.POSIXct(e2GaugeReading$timestrip)
for( i in 2:ncol(e2GaugeReading)-1){
if((max(e2GaugeReading\[,i\]) - min(e2GaugeReading\[,i\])) != 0 )
{
e2GaugeReading\[,i\] = e2GaugeReading\[,i\]/max(abs(e2GaugeReading\[,i\]))
}
}
e1GaugeReading \<- dplyr::filter(e2GaugeReading, timestrip \>= start & timestrip \<= end)
\#e1GaugeReading$timestrip \<- as.POSIXct(e1GaugeReading$timestrip)
e1GR_Long \<- melt(e1GaugeReading, id.vars = "timestrip")
SignalPlotSmall \<- ggplot(e1GR_Long, aes(x = timestrip, y = value, col = variable)) + geom_line(linewidth = 1)+
ggtitle("Segment Chassis Signal Plot Small") + labs(x = "Time in Seconds") +
geom_point(data = marker, size = 8, colour = "red", shape = 18)
e2GR_Long \<- melt(e2GaugeReading, id.vars = "timestrip")
SignalPlotLarge \<- ggplot(e2GR_Long, aes(x = timestrip, y = value, col = variable)) + geom_line(linewidth = 1) +
ggtitle("Segment Chassis Signal Plot Large") + labs(x = "Time in Seconds") +
geom_point(data = marker, size = 8, colour = "red", shape = 18)
\#e1GR_heatmap \<- (e1GaugeReading\[,c(-6)\])
\#scatterplot_matrix \<- ggpairs(e1GR_heatmap)
\#ggmatrix_gtable(scatterplot_matrix)
return(plot_grid(SignalPlotSmall, SignalPlotLarge, nrow = 2))
\#return(ggplotly(SignalPlotSmall))
}