0

I want to save multiple plots to a pdf file. So I tried to use a list to save the results of ggplot. But the problem is the first place of the list is always wrong with that error:

Error: Aesthetics must be either length 1 or the same as the data (651): x, y, colour.

And the rest of the list doesn't have this problem. The code is here:

raw_data <- read.csv("hourly_42401_2018.csv")
raw_data <- separate(data=raw_data,col=Date.Local,into=c("year","month","day"),sep="-")
data <- raw_data[raw_data$State.Name=="Alabama"&raw_data$County.Name=="Jefferson"&raw_data$Site.Num=="23",c("Time.Local","Sample.Measurement","month","day")]
data$Time.Local <- as.numeric(data$Time.Local)-1

m <- c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
num_m <- unique(data$month)
filename=paste("D:/test/temp1","/","SO2",".pdf",sep="")

plot_list <- list()
for(i in 1:length(num_m))
{
  data_m <- data[data$month==num_m[i],]
  if(nrow(data_m)==0)
    next

  title <- paste("SO2",",",m[as.numeric(num_m[i])],",",2018,",","Alabama",",","Jefferson",",","23",sep="")

  plot_list[[i]] <- ggplot(data_m,aes(x=data_m$Time.Local,y=data_m$Sample.Measurement,colour=data_m$day))+
      geom_point()+geom_line()+xlab("Time")+ylab("parts per million")+labs(fill="Day")+ggtitle(title)+
      theme(plot.title = element_text(hjust = 0.5))+scale_x_continuous(breaks=seq(0,24,6))
  #ggsave(filename,width=50,height=20,units="cm",dpi=300)
}
pdf(filename)
for(i in 1:length(num_m))
{
  p <- plot_list[[i]]
  print(p)
}
dev.off()

And the list description is here: enter image description here Thank very much! It's my first time to ask for help on this website. I am sorry I forgot the sample input and result I expected.The raw data is here:https://drive.google.com/file/d/1Ny3i_W0H9-w0WjXTPZtdQ6QyKXzdv9Va/view?usp=sharing. The data frame data_m is the input data:

 > head(data_m)
    Time.Local Sample.Measurement month day
652          0                0.4    02  01
653          1                0.3    02  01
654          2                0.2    02  01
655          3                0.2    02  01
656          4                0.1    02  01
657          5                0.2    02  01

And the result I am looking for is a pdf file with plots on each page.One of the plots is like that: enter image description here

  • 1
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jul 30 '18 at 18:42
  • I cannot reproduce the error with the sample data you provided. If we cannot reproduce the problem, it will be very difficult to attempt to help you. – MrFlick Jul 30 '18 at 19:51
  • I am sorry for my mistake. I have already upload the raw data. Thanks for all you guys' help! – Jason Yang Jul 30 '18 at 20:30

2 Answers2

0

As mentioned you should include someway to recreate your data

Anyway this should work

    library(tidyverse)

m <- c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
raw_data <- read_csv("hourly_42401_2018.csv")

aw_data <- separate(data=raw_data,col="Date Local",into=c("year","month","day"),sep="-")
data <- aw_data %>% 
  filter(`State Name`=="Alabama"&&
         `County Name`=="Jefferson"&&
         `Site Num`=="0023")


num_m <- unique(data$month)

plot_list <- list()
for(i in 1:length(num_m)){
  data_m <- data[data$month==num_m[1],]

  if(nrow(data_m)!=0){
    title <- paste(
      "SO2",
      m[as.numeric(num_m[i])],
      2018,
      "Alabama",
      "Jefferson",
      "23",sep=",")

    plot_list[[i]] <- 
      ggplot(
        data_m,
        aes(
          x = `Time Local`,
          y = `Sample Measurement`,
          colour = day
          )
        )+
      geom_point()+
      geom_line()+
      xlab("Time")+
      ylab("parts per million")+
      ggtitle(title)+
      theme(plot.title = element_text(hjust = 0.5))+
      scale_x_continuous(breaks=seq(0,24,6))    
  }

}
pdf("filename.pdf")
for(i in 1:length(num_m)){
  p <- plot_list[[i]]
  print(p)
}
dev.off()

I'm not sure what I change, I retyped everything. Make your to restart your R session, especially with pdf and dev.off

  • Yep but its better to type dput(data) in the console et copy paste the output so one can recreated your data. Like mine for data gives structure(list(Time.local = c(0, 1, 2, 3, 4, 5), Sample.Measurement = c(0.1, + 0.2, 0.3, 0.4, 0.5, 0), month = c(1, 2, 3, 4, 1, 2), day = c(1, + 5, 3, 6, 5, 8)), class = "data.frame", row.names = c(NA, -6L)) – Jean-Rémi Telmon Jul 30 '18 at 21:32
  • Right. I tried your code on my local computer, it worked. But I think the problem is the size of the elements in each position of the list. So I upload all the data. This problem is so weird that when I use your test data, everything is OK. But when I turn to my raw data, the error appears again. – Jason Yang Jul 30 '18 at 21:46
  • @JasonYang I downloaded your data and tried. No problem. I edited my code because I used read_csv and it produces tibbles that accept space in cols name. PDF Outputed is 3Mo (and slowdown my computer a lot, but ok) – Jean-Rémi Telmon Jul 30 '18 at 22:46
0

Your problem is that

ggplot(data_m,aes(x=data_m$Time.Local,y=data_m$Sample.Measurement,colour=data_m$day))+

should not include data_m$ inaes(). Your code will not use the provided data for plotting.

TC Zhang
  • 2,757
  • 1
  • 13
  • 19