0

I have this dataset:

    structure(list(Agency.Station.ID = c("MI270N003.6D", "MI270N004.7D", 
"MI270N005.7D", "MI270N007.3D", "MI270N008.5D", "MI270N003.6D", 
"MI270N004.7D", "MI270N005.7D", "MI270N007.3D", "MI270N008.5D"
), date_time = structure(c(1427846400, 1427846400, 1427846400, 
1427846400, 1427846400, 1427846700, 1427846700, 1427846700, 1427846700, 
1427846700), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    Date = structure(c(16526, 16526, 16526, 16526, 16526, 16526, 
    16526, 16526, 16526, 16526), class = "Date"), Time = c("00:00:00", 
    "00:00:00", "00:00:00", "00:00:00", "00:00:00", "00:05:00", 
    "00:05:00", "00:05:00", "00:05:00", "00:05:00"), Speed = c(59, 
    34, 46, 61, 46, 58, 39, 51, 36, 52), Total.Volume = c(7.5, 
    6, 6, 11.6, 7.2, 10, 8, 8, 7.2, 7.2), Ocupancy = c(0.8, 0.4, 
    0.6, 1.1, 1, 0.8, 0.7, 1.1, 0.8, 0.8), S.D = c(28.9, 17.8, 
    19.9, 24.4, 27.8, 23.6, 15.7, 15.3, 13.5, 26.6), Precipitation = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, 10L), class = "data.frame")

I want to have a pdf for each Agency.Station.ID. in each pdf there is a page for the date and in each page, I want to have a plot that has Time on x-axis and (Speed and Precipitation) on y axis.

mustafa
  • 203
  • 1
  • 8

1 Answers1

2

Here a quick and dirty solution:

library(tidyverse)
# first you've to work with your data, choosing the helpful columns
dat1 <- dat[,c(1,3,5,9)]

# then put them in long format
dat1 <- dat1 %>% gather(variable, value,-c('Date','Agency.Station.ID'))

# now the loop that creates the plots.pdf. Remember to add your path
for (i in unique(dat1$Agency.Station.ID)){
                                          dat1 <- dat1[dat1$Agency.Station.ID==i,]
                                          ggplot(dat1,
                                                 aes(x = Date,
                                                     y = value,
                                                     group = variable,
                                                     color = variable)) + 
                                          geom_line() + geom_point()
                                          ggsave(paste0('C:\\addyourpath\\',i,'.pdf'))
                                         }
s__
  • 9,270
  • 3
  • 27
  • 45
  • It did not work. I want the plot to have Time on X-axis and both speed and precipitation on the Y-axis. the gather code makes the data looks messy. – mustafa May 11 '20 at 14:50
  • I have two loops, first I will loop through each Agency.Station.ID to create a pdf. then I will loop through each date to make new page inside each pdf file. Each page has a plot of (Time VS Speed and Precipitation). – mustafa May 11 '20 at 15:00
  • @mustafa generally ggplot works well with data in the long format, so that's why the gather function. The result you need is given by my code, that creates a pdf for each Agency.Station.ID, and "put a plot" in it. Try it putting your path, to see if it works. – s__ May 11 '20 at 15:19
  • I reposted the question because I think I did not make myself clear here, please see my other post. one page in a pdf does not work since I need a page for each data. – mustafa May 11 '20 at 15:22