I created a loop that creates a PDF per car brand while each PDF has about 21 plots inside and it is 1 plot per PDF page and i would like to turn it into a 2 plots per page. I found some examples but they didn't help
here is the data:
env1 <- structure(list(Inspection_year = structure(c(1L, 1L, 1L, 1L,1L, 1L), .Label = c("2017", "2018", "2019", "2020"), class = "factor"),Brand = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Alfa Romeo - Models in total","Audi - Models in total", "BMW - Models in total", "Chevrolet - Models in total","Chrysler - Models in total", "Citroen - Models in total","Dacia - Models in total", "Dodge - Models in total", "Fiat - Models in total","Ford - Models in total", "Honda - Models in total", "Hyundai - Models in total","Jaguar - Models in total", "Jeep - Models in total", "Kia - Models in total","Lada-Vaz - Models in total", "Lada - Models in total", "Land Rover - Models in total","Lexus - Models in total", "Mazda - Models in total", "Mercedes-Benz - Models in total","Mini - Models in total", "Mitsubishi - Models in total","Nissan - Models in total", "Opel - Models in total", "Peugeot - Models in total","Porsche - Models in total", "Renault - Models in total","Saab - Models in total", "Seat - Models in total", "Skoda - Models in total","Smart - Models in total", "Ssangyong - Models in total","Subaru - Models in total", "Suzuki - Models in total", "Tesla Motors - Models in total","Toyota - Models in total", "Volkswagen - Models in total","Volvo - Models in total"), class = "factor"), Registration_year = structure(c(1L,1L, 1L, 1L, 1L, 1L), .Label = c("2002", "2003", "2004", "2005","2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013","2014", "2015", "2016", "2017"), class = "factor"), object_of_inspection = structure(c(7L,6L, 5L, 2L, 3L, 4L), .Label = c("Brake systems - Dynamometer test of parking brake","Brake systems - Dynamometer test of service brake", "Brake systems - Other brake and decelerator","Brake systems - Parking brake", "Brake systems - Service brake","Brake systems - Stability control system", "Brake systems (all objects)","Lighting devices and electrical equipment - Brake light"), class = "factor"), Number of inspections = c(250, 250,250, 250, 250, 250), Average mileage = c(196000, 196000,196000, 196000, 196000, 196000), Median mileage = c(193000,193000, 193000, 193000, 193000, 193000), Demand for repairs (number of faults) = c(11,0, 9, 0, 0, 2), Rejections (number of faults) = c(55, 1,21, 3, 0, 1), Driving bans (number of faults) = c(0, 0,0, 0, 0, 0)), row.names = 3:8, class = "data.frame")
and here is the code
for (brand in levels(env1$Brand)){
POUTSA_DATA <- list()
pdf(paste0("C:\\Users\\sky4v\\se R\\Operation uranus\\axles per brand\\axles",brand,"plots.pdf"))
for (category in colnames(env1[,8:10])){
for (object in levels(env1$object_of_inspection)){
skatodata <- subset(env1, Brand == brand & object_of_inspection == object)
POUTSA_DATA[[object]] <- skatodata
if (sum(skatodata[,category])>0){
diagramma<- (ggplot(skatodata, aes(fill=Inspection_year, y=skatodata[,category], x=Registration_year)) +
geom_bar(position="dodge", stat="identity") +
ggtitle( paste0(brand,"\n",category, "\nby registration year per inspection year \n(",object,")"))+
xlab("Registration year") +
ylab(colnames(skatodata[category]))+
theme(axis.text.x = element_text(angle = 0, size = 10, hjust = 1 ,vjust= 0.5 )))
}
else {print("gamisou")
}
}
}
for (i in 1:length(names(POUTSA_DATA))){
if (nchar(names(POUTSA_DATA)[i])>=32){
names(POUTSA_DATA)[i]<- substring(names(POUTSA_DATA)[i],25,nchar(names(POUTSA_DATA)[i]))
}
if ( nchar(names(POUTSA_DATA)[i])>=32){
names(POUTSA_DATA)[i]<- substring(names(POUTSA_DATA)[i],1,31)
}
}
write_xlsx(POUTSA_DATA,paste0("C:\\Users\\sky4v\\se R\\Operation uranus\\axles per brand\\axles data ",brand, ".xlsx"))
dev.off()
}