创建的函数图形输出未正确编织,但在控制台 ggplot2 中正确 时间序列 RMarkdown 不知道为什么

created function graphical output does not knit correctly but correct in console ggplot2 Timeseries RMarkdown Dont know why

提问人:Max Miller 提问时间:10/10/2023 更新时间:10/10/2023 访问量:18

问:

我制作了一个函数来分析一段时间序列数据。它返回 2 个 ggplot,这些 ggplot 被馈送到熔化的数据帧。时间序列具有小数秒。在 rstudio 中运行代码时,输出是正确的,但是当编织到报告的文件时,小数秒似乎在每秒 .00 实例处聚集。

Rstudio 输出(预期报告输出) 在此处输入图像描述 编织到任何类型的文档时输出 在此处输入图像描述

法典


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))
}
r 日期 ggplot2 时间序列 熔化

评论

0赞 Max Miller 10/10/2023
对不起,代码中的斜杠。不知道为什么会在那里。

答: 暂无答案