Function 在Rmd中运行timeseries绘图函数,生成杂乱的x轴标签(测试代码中不存在)
我有一个xx.csv timeseries文件夹,我想将其绘制成一个干净的HTML文档。我有一个ggplot代码,它使用单个timeseries.csv生成我想要的绘图。但是,当我尝试将ggplot代码的骨架放入for循环中的函数中,通过该函数运行每个timeseries.csv文件时,我得到了一些格式完全不同的绘图 使用我的测试ggplot代码生成的绘图: 使用函数和for循环生成的绘图: 我试图对丑陋的Rmd图进行更改:Function 在Rmd中运行timeseries绘图函数,生成杂乱的x轴标签(测试代码中不存在),function,for-loop,ggplot2,r-markdown,ggthemes,Function,For Loop,Ggplot2,R Markdown,Ggthemes,我有一个xx.csv timeseries文件夹,我想将其绘制成一个干净的HTML文档。我有一个ggplot代码,它使用单个timeseries.csv生成我想要的绘图。但是,当我尝试将ggplot代码的骨架放入for循环中的函数中,通过该函数运行每个timeseries.csv文件时,我得到了一些格式完全不同的绘图 使用我的测试ggplot代码生成的绘图: 使用函数和for循环生成的绘图: 我试图对丑陋的Rmd图进行更改: 将x轴刻度线精确地间隔为整分钟(即“11:14:00”、“11:
- 将x轴刻度线精确地间隔为整分钟(即“11:14:00”、“11:15:00”)
- 连接数据点(使用子Bing
和geom_线()
解决)geom_路径()
“
中的第3个以禁用它
编辑和更新
- 我得到一个持续错误
geom_path:每个组只包含一个观察值。您需要调整组吗 美学?
- 正如评论者所建议的,我尝试删除plot(),直接使用createChlDiffPlot()并用print()替换plot()。两者都会产生与以前相同的丑陋的绘图
- 将geom_线()替换为geom_路径()。点现在已连接!x轴混乱仍然存在
- 时间变量的读数为
numhms
```
---
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
```
```
---
标题:“Chl过滤”
输出:
flexdashboard::flex_仪表板:
主题:雪人
方向:行
编辑器选项:
块输出类型:控制台
---
```{r设置}
库(flexdashboard)
图书馆(dplyr)
图书馆(GG2)
图书馆(hms)
图书馆(主题)
图书馆(readr)
库(数据表)
####示例数据
我终于明白了
1) 将geom_line()
替换为geom_path()
连接在Rmd中渲染时的数据点
2) df1$Time
被格式化为difftime
对象。当我在全局环境中查看数据帧时,Time:
hmsnum 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轴 为什么使用plot()
打印ggplot
对象?您可以使用print()
或直接调用createChlDiffPlot()
请尝试print()命令。这通常有助于在循环中生成多个绘图。我尝试删除plot()
,直接使用createChlDiffPlot()
并将plot()
替换为print()
。两者都产生了和以前一样丑陋的情节。我有一个之前没有提到的持续性错误(这些尝试仍然会显示):geom_路径:每个组只包含一个观察值。您需要调整团队美学吗?
谢谢您的想法,还有其他可能性吗?@Tung我正在修改其他人编写的脚本。我最终发现我需要用plot()
包装createChlDiffPlot()
函数,因为我正在使用flexdashboard
包生成一个HTML来显示我的所有图形。[