1

R Studio - Version 2023.03.0+386 (2023.03.0+386)

I am pretty new to programming. I am trying to create a for loop that will create 10 plots using ggplot. Each plot will be from a selection from a different column in a dataset.

I am having an issue with the date format. The message 'Error: Invalid input: date_trans works with objects of class Date only' keeps coming up.

This is the code I'm running. The date format in the .csv is 'YYYY-MM-DD'.

in addition to the code below, I have also entered data$date_collected <- as.Date(data$date_collected, format("%Y,%m,%d")) and received the same error message.

library(ggplot2)
library(dplyr)

# Read the data from CSV file
data <- read.csv("synthetic_data.csv")

# Convert "date_collected" column to proper date format
data$date_collected <- as.Date(data$date_collected)

# Specify the number of graphs to generate
num_graphs <- 10

# Loop through each graph
for (i in 1:num_graphs) {
  # Select the column name and subtitle for the current graph
  column_name <- paste0("gmax_", i)
  subtitle <- paste0("Location ", i)
  
  # Filter data for the specific site (GeorgetownHS) and select the 5 most recent entries
  filtered_data <- data %>%
    filter(Site == "GeorgetownHS") %>%
    arrange(desc(date_collected)) %>%
    head(5)
  
  # Create the plot
  ggplot(filtered_data, aes(x = date_collected, y = !!sym(column_name))) +
    geom_line() +
    geom_point() +
    labs(
      x = "Previous 5 Data Collection Dates",
      y = "GMax Value",
      title = "GMax Values",
      subtitle = subtitle
    ) +
    scale_x_date(date_labels = "%Y-%m-%d", date_breaks = "1 day") +
    scale_y_continuous(limits = c(50, 120)) +
    geom_hline(yintercept = 100, linetype = "dashed", color = "red") +
    annotate("text", x = Inf, y = 100, hjust = 1, label = "Recommended Maximum GMax Rating")
  
  # Save the graph as a file (optional)
  ggsave(filename = paste0("graph_", i, ".png"), width = 6, height = 4, dpi = 300)
}
stefan
  • 90,330
  • 6
  • 25
  • 51
  • 2
    Welcome to SO! It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data so that we can run your code and figure out what's the issue. The best way to share your data would be via `dput()`, e.g. run `dput(data[1:10, ])` or the first ten rows of data and copy the output to your post. – stefan May 24 '23 at 18:41
  • 1
    I suspect you can reduce this question from "many plots" to just "one plot"; whether it's all in your list that fail or just one of them, find one that fails and reduce the question to just that one plot please. That is, find one that breaks, then [edit] your question and post the output from `dput(head(x))` into a [code block]. It's a good question otherwise with code and context, the only thing we need is sample data. Thanks! – r2evans May 24 '23 at 18:58
  • 1
    On a sidenote: one of ggplot's strengths is *facetting*: instead of creating otherwise identical plots by looping through the values of columns A = c(a1, a2, a3, ...), B = c(b1, b2, b3, ...), C = c(c1, c2, c3, ...) you `pivot_longer` columns A:C into a value column = c(a1, a2, a3, ..., b1, ... , c1, ...) along with a name column = c(A, A, A, ..., B, ..., C, ...) and have ggplot create the subplots with `facet_grid` or `facet_wrap`. A real time saver: http://www.cookbook-r.com/Graphs/Facets_(ggplot2)/ – I_O May 24 '23 at 19:48
  • 1
    I was able to run line by line through this finally. I tried doing it before but must have been doing something wrong. The issue was something in the last line of the ggplot code. The "annotate("text"...)" was the culprit. I'm not sure what it was, but I took it out and replaced it with a geom_label and everything works great now. – fgsconsulting May 24 '23 at 21:15

0 Answers0