1

For each element of my list of data (data_l) I got a plot with this simple function :

data_l <- split(data,list(data$Sample.type,data$Locality), drop=TRUE)
lapply(names(data_l), function(i){
  ggplot(data_l[[i]], aes(Date, Concentration, group = Chemicals, col = as.factor(Chemicals))) +
    geom_line() +
    geom_point() +
    facet_grid(cols = vars(Chemicals), rows = vars(Locality), scales = "fixed") +
    ylab(paste("Concentrations in", data_l[[ i ]]$Measuring.Unit[ 1 ])) +
    ggsave(paste(i, ".png", sep = ""),  dpi = 400, width = 30, height = 20, units = "cm")
})
dev.off()

I fixed in ggsave the height at 20 cm. But, depending on the number of facets, the plot size has to be adapted for each element of the list.

Is it possible to define a function for "height" in ggsave that would take into account the number of rows in facet_grid ? (10 cm if 1 row in facet_grid, 20 cm if 2 rows,...)

Sylvain
  • 133
  • 1
  • 10

0 Answers0