ggplot2 won't plot

I've been running an R script for the last three months to graph the historical prices of whisky in my database. It takes the whisky name, auction date and price, plots the dots and a loess curve. The data is contained in chatGPT(The file not the programme) which I update monthly. This month I added new data into chatGPT, imported it into RStudio and ran the script. The script showed no errors in the consol but did not create the updated graphs.
I didn't amend the script, only the data frame. Can anyone point me to where the problem is?
Here is the script copied from the consol.

  • Load the required libraries

  • library(ggplot2)
  • library(tidyverse)
  • library(grDevices)
  • Set the output directory for the graphs

  • output_dir <- "C:\Users\alana\OneDrive\Documents\R\Rgraphs\loess\"
  • Split the data into a list of data frames, one for each whisky

  • whiskies <- split(chatGPT, chatGPT$name)
  • Iterate over the list of data frames

  • for (whisky in whiskies) {
  • Extract the name of the current whisky

  • name <- whisky$name[1]
  • Create a valid file name from the whisky name

  • file_name <- make.names(name, unique = TRUE)
  • Create a line of best fit using the lm function

  • fit <- lm(price ~ date, data = whisky)
  • Create a ggplot object with the data and the line of best fit

  • plot <- ggplot(whisky, aes(x = date, y = price)) +
  • geom_point() +
    
  • geom_smooth(se = FALSE) +
    
  • labs(title = name)
    
  • Save the plot to a PDF file

  • pdf(file = paste0(output_dir, file_name, ".pdf"))
  • print(plot)
  • dev.off()
  • }

Does this work for you. It should store the PDF in your working directory.

library(ggplot2)
plot <- ggplot(mtcars, aes(cyl, mpg)) + geom_point()
pdf(file = "ggplotTest.pdf")
print(plot)
dev.off()

Apologies, there was nothing wrong with the script. I had forgotten to change some column names before importing the excel file into R. Thanks FJCC.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.