[英]Running timeseries graphing function in Rmd producing cluttered x-axis labels (not present in test code)
我有一个 xx .csv 时间序列的文件夹,我想将其绘制并编入一个干净的 HTML 文档中。 我有一个 ggplot 代码,它使用单个 timeseries.csv 生成我想要的图。 但是,当我尝试将该 ggplot 代码的骨骼放入 for 循环内的函数中以通过该函数运行每个 timeseries.csv 文件时,我得到了一些格式完全不同的图。
我正在尝试对丑陋的 Rmd 情节进行更改:
geom_line()
与geom_path()
geom_line()
来解决) 下面的示例 Rmd 代码。 请注意,生成的图形仍然具有很好的格式,我不确定如何重现发布 500 行数据帧的问题。 我也不知道如何使用本文中的格式化命令在没有 SO 的情况下发布我的 rmd 代码,所以我在我的标题格式周围添加了 3 个"
并在代码末尾禁用它。
编辑和更新
geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?
geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?
.hms
num非常感谢您对此的任何帮助!
```
---
title: "Chl Filtration"
output:
flexdashboard::flex_dashboard:
theme: yeti
orientation: rows
editor_options:
chunk_output_type: console
---
```{r setup}
library(flexdashboard)
library(dplyr)
library(ggplot2)
library(hms)
library(ggthemes)
library(readr)
library(data.table)
#### Example Data
df1 <- data.frame(Time = as_hms(c("11:22:33","11:22:34","11:22:35","11:22:38","11:23:00","11:23:01","11:23:02")),
Chl_ug_L_Up = c(0.2,0.1,0.25,-0.2,-0.3,-0.15,0.1),
Chl_ug_L_Down = c(0.5,0.4,0.3,0.2,0.1,0,-0.1))
df2 <- data.frame(Time = as_hms(c("08:02:33","08:02:34","08:02:35","08:02:40","08:02:42","08:02:43","08:02:49")),
Chl_ug_L_Up = c(-0.2,-0.1,-0.25,0.2,0.3,0.15,-0.1),
Chl_ug_L_Down = c(-0.1,0,0.1,0.2,0.3,0.4,0.1))
data_directory = "./" # data folder in R project folder in the real deal
output_directory = "./" # output graph directory in R project folder
write_csv(df1, file.path(data_directory, "SO_example_df1.csv"))
write_csv(df2, file.path(data_directory, "SO_example_df2.csv"))
#### Function to create graphs
createChlDiffPlot = function(aTimeSeriesFile, aFileName, aGraphOutputDirectory, aType)
{
aFile_Mod = aTimeSeriesFile %<>%
select(Time, Chl_ug_L_Up, Chl_ug_L_Down) %>%
mutate(Chl_diff = Chl_ug_L_Up - Chl_ug_L_Down)
one_plot = ggplot(data = aFile_Mod, aes(x = Time, y = Chl_diff)) + # tried adding 'group = 1' in aes to connect points
geom_path(size = 1, color = "green") +
geom_point(color = "green") +
theme_gdocs() +
theme(axis.text.x = element_text(angle = 45, hjust = 1),
legend.title = element_blank()) +
labs(x = "", y = "Chl Difference", title = paste0(aFileName, " - ", "Filtration"))
one_graph_name = paste0(gsub(".csv", "", aFileName), "_", aType, ".pdf")
ggsave(one_graph_name, one_plot, dpi = 600, width = 7, height = 5, units = "in", device = "pdf", aGraphOutputDirectory)
return(one_plot)
}
"``` ### remove the quotes when running example
Plots - After Velocity Adjustment
=====================================" ### remove quotes when running example
```{r, fig.width=13.5, fig.height=5}
all_files_Filtration = list.files(data_directory, pattern = ".csv")
# Loop to plot function
for(file in 1 : length(all_files_Filtration))
{
file_name = all_files_Filtration[file]
one_file = fread(file.path(data_directory, file_name))
# plot the time series agains
plot(createChlDiffPlot(one_file, file_name, output_directory, "Velocity_Paired"))
}
"``` #remove quotes when running example
```
我终于弄明白了。
1) 在 Rmd 中渲染时,用geom_path()
) 替换geom_line()
连接数据点。
2) df1$Time
被格式化为一个difftime
对象。 当我查看全局环境中的数据num 11:11:09 ...
, Time :
num 11:11:09 ...
。 这让我觉得我的格式没问题,但是当我运行class(df1$Time)
我得到了[1] "hms" "difftime"
。 一个快速谷歌,我发现difftime
对象不太一样hms
,并通过减去次产生的我原来的时间。 我在我的 mutate 函数中添加了一个转换:
select(Time, Chl_ug_L_Up, Chl_ug_L_Down) %>%
mutate(Chl_diff = Chl_ug_L_Up - Chl_ug_L_Down,
Time = as_hms(Time)) # convert difftime objecct to hms
ggplot 我认为 hms 变量有一些自动格式化,这就是 difftime 变量产生丑陋拥挤的 x 轴的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.