0

我有一个 xx .csv 时间序列的文件夹,我想将其绘制成一个干净的 HTML 文档。我有一个 ggplot 代码,它使用单个 timeseries.csv 生成我想要的图。但是,当我尝试将该 ggplot 代码的骨架放在 for 循环内的函数中以通过该函数运行每个 timeseries.csv 文件时,我得到了一些格式完全不同的图。

使用我的测试 ggplot 代码生成的绘图: 情节梦想

使用函数和 for 循环生成的绘图: 情节失败

我正在尝试对丑陋的 Rmd 图进行更改:

  • 将 x 轴刻度线很好地间隔为整分钟(即“11:14:00”、“11:15:00”)
  • 连接数据点(通过 subbing geom_line()with解决geom_path()

下面的示例 Rmd 代码。请注意,生成的图表仍然有很好的格式,我不确定如何重现这个问题,比如发布一个 500 行数据框。我也不知道如何在没有 SO 的情况下使用本文中的格式化命令发布我的 rmd 代码,所以我在"我的标题格式和代码末尾添加了 3 个以禁用它。

编辑和更新

  • 我收到一个持续性错误geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?
  • 正如评论者所建议的那样,我尝试删除 plot() 并直接使用 createChlDiffPlot() 并将 plot() 替换为 print()。两者都产生与以前相同的丑陋情节。
  • 将 geom_line() 替换为 geom_path()。点现在已连接!x 轴杂乱仍然存在。
  • 时间变量读取为hmsnum

非常感谢您对此的任何帮助!

```
---
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
```
4

1 回答 1

0

我终于弄明白了。

1)在 Rmd 中渲染时替换geom_line()为连接的数据点。geom_path()

2)df1$Time被格式化为一个difftime对象。当我查看全局环境中的数据框时,Time :hms 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 轴。

于 2020-02-10T22:47:22.577 回答