Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 206278

Graphing function inside 'for loop' in Rmd rendering very different plots then test ggplot code

$
0
0

I have a folder of xx .csv timeseries that I want to graph and knit into a clean HTML document. I have a ggplot code that produces the plot that I want using a single 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.

Plot generated with my test ggplot code: Plot dreams

Plot generated with function and for loop: Plot Fail

Changes I'm trying to make to the ugly Rmd plot:

  • Nicely space the x-axis tick marks to whole mins (i.e. "11:14:00", "11:15:00")
  • Connect the data points

I've tried adding group = 1 to the aes() (I lost the SO post) to connect geom_point() by geom_line(). This also triggers some errors and the For Loop stops before its finished running through the entire list of files. Also tried reversing the order of geom_point() and geom_line() layers with no results.

Example Rmd Code Below. 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. 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.

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(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_line(size = 1, color = "green") + # tried switching order of geom_line() and geom_point()
    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
```

Viewing all articles
Browse latest Browse all 206278

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>