0

I am trying to generate a pdf for each station using ggplot. The code is running with no errors, though, the pdf is empty at my directory.

`structure(list(Agency.Station.ID = structure(c(41L, 31L, 30L, 
42L, 1L, 32L, 32L, 28L, 3L, 42L, 34L, 42L, 30L, 36L, 30L), .Label = c("MI064E009.1D", 
"MI064E009.8D", "MI064E010-0D", "MI064E011.2D", "MI064E012.8D", 
"MI064E013.6D", "MI064E015.2D", "MI064E016.3D", "MI064E017.0D", 
"MI064E018.1D", "MI064E018.9D", "MI064E020.9D", "MI064E021.9D", 
"MI064E023.2D", "MI064E024.2D", "MI064W009.2D", "MI064W010.0D", 
"MI064W011.1D", "MI064W012.8D", "MI064W013.6D", "MI064W015.1D", 
"MI064W015.9D", "MI064W017.0D", "MI064W017.9D", "MI064W018.4D", 
"MI064W018.9D", "MI064W020.2D", "MI064W021.9D", "MI064W023.2D", 
"MI064W024.2D", "MI070E206.0D", "MI070E208.5D", "MI070E209.6D", 
"MI070E210.0D", "MI070E211.0D", "MI070E230.7D", "MI070E232.7D", 
"MI070E235.0D", "MI070W208.2D", "MI070W209.5D", "MI070W210.0D", 
"MI070W232.7D"), class = "factor"), date1 = structure(c(16753, 
16436, 16591, 16638, 16742, 16748, 16593, 16769, 16575, 16737, 
16540, 16521, 16447, 16723, 16749), class = "Date"), DT = structure(c(1447499400, 
1420138500, 1433557500, 1437618900, 1446587100, 1447108500, 1433691900, 
1448924700, 1432161900, 1446111600, 1429095000, 1427514900, 1421096400, 
1444911600, 1447177200), tzone = "", class = c("POSIXct", "POSIXt"
)), variables = c("Speed", "Total.Volume", "Total.Volume", "Total.Volume", 
"Total.Volume", "Total.Volume", "Occupancy", "Speed", "Speed", 
"Occupancy", "Total.Volume", "Speed", "Total.Volume", "Total.Volume", 
"Occupancy"), values = c(58, 57, 76.7, 40.7, NA, NA, 7.3, 14, 
62, 1.1, 49.5, 44, 153.3, 112.8, NA)), row.names = c(NA, -15L
), class = c("tbl_df", "tbl", "data.frame"))`

The code I am using is

for(i in levels(df$Agency.Station.ID)) {
  pdf(paste0(i,'.pdf'), width = 20)
  
  for(j in levels(df$date1)) {
    subset.data <- df[which(df$Agency.Station.ID==i & df$date1==j),]
    if (nrow(subset.data)!=0) {
      p <- ggplot(data=subset.data, aes(DT, values, group = variables))+
        geom_line(aes(color=variables)) +
        
        labs(title=paste('Station:',i, " Date:",j)) +
        theme_bw()
      invisible(print(p))
    }
  }
  dev.off()
}
  • Your `levels(df$date1) = NULL`. Nothing happened inside the inner loop – Tung Mar 22 '22 at 20:49
  • @Tung Thanks. How would I fix that? – Mustafa Kamal Mar 22 '22 at 20:52
  • I assume you want to plot a timeseries for each station then why do you need the inner loop? – Tung Mar 22 '22 at 20:55
  • @Tung, Yes! I want to have a pdf for each station. each page will contain a specific date. On each date (page), X = DT, Y = Values, group = Variables – Mustafa Kamal Mar 22 '22 at 20:57
  • Sidenote: `ggplot` has a convenient function `ggsave` to save plot output. The file format is inferred from the filename. Example: `ggsave(plot.pdf, ...)` will save the *current* ggplot output as "plot.pdf" without the hazzle of explicitly opening graphic devices. –  Mar 22 '22 at 21:43
  • See also this https://stackoverflow.com/a/52436617/786542 – Tung Mar 22 '22 at 23:28
  • @Tung I checked the link you sent and used it. Though, I am getting the same empty pdfs – Mustafa Kamal Mar 23 '22 at 16:55
  • I found that I needed to change the structure of the variable "date1" to character because it was a Date. Thank you everyone for the help – Mustafa Kamal Mar 27 '22 at 16:49

0 Answers0