Home > Net >  How to generate multible plots from one dataframe in a row in r?
How to generate multible plots from one dataframe in a row in r?

Time:02-02

I am quite new to R and am trying to simplify my codes as I often create different plots from one dataframe. I have experimented with loops but have not achieved anything yet. Librarys needed:

library(dplyr)
library(ggplot2)
library(lubridate)

I have a data set with different measurements over a period of time in several batches similar to this one:

x<-rbind(as.data.frame(cbind(batch = "A", datetime = format(seq(as.POSIXct("2023-01-01 06:00:00", tz="UTC"), by="min", length.out=50), "%Y/%m/%d %H:%M:%OS"),
                            rbind(
                                  as.data.frame(cbind(measurement = "ref_Flow",values = rnorm(50, mean = 1.6, sd = 0.005e 01))),
                                  as.data.frame(cbind( measurement = "Flow" ,values = rnorm(50, mean = 1.6, sd = 0.008))),
                                  as.data.frame(cbind(measurement = "ref_Temp",values = rnorm(50, mean = 23, sd = 0.5e 01))),
                                  as.data.frame(cbind(measurement = "Temp",values = rnorm(50, mean = 23, sd = 1))),
                                  as.data.frame(cbind(measurement = "ref_Hum",values = rnorm(50, mean = 50, sd = 1e 01))),
                                  as.data.frame(cbind(measurement = "Hum", values = rnorm(50, mean = 50, sd = 5)))
                                   ))),
         as.data.frame(cbind(batch = "B", datetime = format(seq(as.POSIXct("2023-01-05 08:43:00", tz="UTC"), by="min", length.out=50), "%Y/%m/%d %H:%M:%OS"),
                             rbind(
                               as.data.frame(cbind(measurement = "ref_Flow",values = rnorm(50, mean = 0.16, sd = 0.005e 01))),
                               as.data.frame(cbind( measurement = "Flow" ,values = rnorm(50, mean = 0.16, sd = 0.01))),
                               as.data.frame(cbind(measurement = "ref_Temp",values = rnorm(50, mean = 23, sd = 0.5e 01))),
                               as.data.frame(cbind(measurement = "Temp",values = rnorm(50, mean = 23, sd = 0.8))),
                               as.data.frame(cbind(measurement = "ref_Hum",values = rnorm(50, mean = 50, sd = 1e 01))),
                               as.data.frame(cbind(measurement = "Hum", values = rnorm(50, mean = 50, sd = 2)))
                             ))),
         as.data.frame(cbind(batch = "C", datetime = format(seq(as.POSIXct("2023-01-20 07:33:00", tz="UTC"), by="min", length.out=50), "%Y/%m/%d %H:%M:%OS"),
                             rbind(
                               as.data.frame(cbind(measurement = "ref_Flow",values = rnorm(50, mean = 0.16, sd = 0.005e 01))),
                               as.data.frame(cbind( measurement = "Flow" ,values = rnorm(50, mean = 0.16, sd = 0.002))),
                               as.data.frame(cbind(measurement = "ref_Temp",values = rnorm(50, mean = 23, sd = 0.5e 01))),
                               as.data.frame(cbind(measurement = "Temp",values = rnorm(50, mean = 23, sd = 1.3))),
                               as.data.frame(cbind(measurement = "ref_Hum",values = rnorm(50, mean = 50, sd = 1e 01))),
                               as.data.frame(cbind(measurement = "Hum", values = rnorm(50, mean = 50, sd = 10)))
                             ))))
x$values<-as.numeric(x$values)
x$datetime<-as.POSIXct(x$datetime)

I would like to plot measurements and ref_measurements per batch. I would do this for each batch and measurement:

ggplot(subset(x, batch == "A") %>% 
         filter(measurement %in% c("Flow", "ref_Flow"))) 
  aes(x = datetime, y = values, color = measurement) 
  geom_line() 
  labs(title = "Batch: A",
       subtitle = "Measurement: Flow")

How can I integrate a loop (or similar) to not repeat the code x times?

CodePudding user response:

Using a for loop, storing the plots in a list

pl <- list()
for(i in unique(x$batch)){
pl[[i]] <- ggplot(subset(x, batch == i) %>% 
               filter(measurement %in% c("Flow", "ref_Flow")))  
             aes(x = datetime, y = values, color = measurement)  
             geom_line()  
             labs(title = paste("Batch:", i), subtitle = "Measurement: Flow")}

Plot or save later by batch

pl$A

<<plot>>
pdf("file.pdf")

pl$A

dev.off()
  • Related