简体   繁体   English

在 Rmd 中运行时间序列绘图函数产生杂乱的 x 轴标签(测试代码中不存在)

[英]Running timeseries graphing function in Rmd producing cluttered x-axis labels (not present in test code)

I have a folder of xx .csv timeseries that I want to graph and knit into a clean HTML document.我有一个 xx .csv 时间序列的文件夹,我想将其绘制并编入一个干净的 HTML 文档中。 I have a ggplot code that produces the plot that I want using a single timeseries.csv.我有一个 ggplot 代码,它使用单个 timeseries.csv 生成我想要的图。 However, when I try to put the bones of that ggplot code in a function inside of a for loop to run each of the timeseries.csv files through the function I get a some plots with pretty different formatting.但是,当我尝试将该 ggplot 代码的骨骼放入 for 循环内的函数中以通过该函数运行每个 timeseries.csv 文件时,我得到了一些格式完全不同的图。

Plot generated with my test ggplot code:使用我的测试 ggplot 代码生成的图: 情节梦想

Plot generated with function and for loop:用函数和 for 循环生成的图: 绘图失败

Changes I'm trying to make to the ugly Rmd plot:我正在尝试对丑陋的 Rmd 情节进行更改:

  • Nicely space the x-axis tick marks to whole mins (ie "11:14:00", "11:15:00")很好地将 x 轴刻度线间隔为整分钟(即“11:14:00”、“11:15:00”)
  • Connect the data points (solved with subbing geom_line() with geom_path() )连接数据点(通过将geom_line()geom_path() geom_line()来解决)

Example Rmd Code Below.下面的示例 Rmd 代码。 Please Note that the graphs produced still have nice formatting, I'm not sure how to reproduce this problem sort of posting a 500 row dataframe.请注意,生成的图形仍然具有很好的格式,我不确定如何重现发布 500 行数据帧的问题。 I also don't know how to post my rmd code without SO using the formatting commands in this post, so I threw in at 3 of " around my header formatting and at the end of the code to disable it.我也不知道如何使用本文中的格式化命令在没有 SO 的情况下发布我的 rmd 代码,所以我在我的标题格式周围添加了 3 个"并在代码末尾禁用它。

Edits and Updates编辑和更新

  • I am getting a persistent error 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? geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic? . .
  • As suggested by the commenters I tried removing plot() and using the the createChlDiffPlot() directly and replacing plot() with print().正如评论者所建议的,我尝试删除 plot() 并直接使用 createChlDiffPlot() 并用 print() 替换 plot()。 Both produce the same ugly plots as before.两者都产生与以前相同的丑陋图。
  • Replaced geom_line() with geom_path().用 geom_path() 替换了 geom_line()。 The points are now connected!点现在已连接! x-axis cluttering is still there. x 轴杂乱仍然存在。
  • Time variable is reading as hms num时间变量读取为hms num

Many thanks for any help on this!非常感谢您对此的任何帮助!

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

I finally figured it out.我终于弄明白了。

1) Replacing geom_line() with geom_path() connected the data points when rendered in Rmd. 1) 在 Rmd 中渲染时,用geom_path() ) 替换geom_line()连接数据点。

2) df1$Time was formatted as a difftime object. 2) df1$Time被格式化为一个difftime对象。 When I looked at the dataframe in the global environment, Time : hms num 11:11:09 ... .当我查看全局环境中的数据num 11:11:09 ...Time : num 11:11:09 ... This made me think my format was ok, but when I ran class(df1$Time) I got [1] "hms" "difftime" .这让我觉得我的格式没问题,但是当我运行class(df1$Time)我得到了[1] "hms" "difftime" With a quick google I found out difftime objects are not quite the same as hms , and my original time was generated by subtracting times.一个快速谷歌,我发现difftime对象不太一样hms ,并通过减去次产生的我原来的时间。 I added a conversion into my mutate function:我在我的 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 I think has some auto-formatting for hms variables, which is why difftime variable was producing ugly crowded x- axes. ggplot 我认为 hms 变量有一些自动格式化,这就是 difftime 变量产生丑陋拥挤的 x 轴的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM