Option for several files creation

I'd like to save my three 3 plots in *png files, but if I try:

#Packages
library(lme4)
library(ggplot2)
library(ggeffects)

#Open my dataset
myds<-read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/my_glmm_dataset.csv")
myds <- myds[,-c(3)] # remove bad character variable

# Negative binomial GLMM
m.laden.1  <- glmer.nb(ladenant ~ Bioma +  poly(temp,2) + scale(UR) + (1 | formigueiro), data = myds)

# Visualize
ggpredict(m.laden.1) %>% plot(add.data = TRUE)

# I have 3 plots for:
# $Bioma

# $temp

# $UR

# Save the plot in a png file
png(filename="example.png", res = 300, width = 15, height = 15, units = "cm") 
ggpredict(m.laden.1) %>% plot(add.data = TRUE)
dev.off()

Doesn't work and I have in the file example.png, just the image of the last plot. I'd like the three files, each file with one plot. Please any help with it?

This code returns a list, you can iterate over the list to save individual files, take a look at this example:

library(lme4)
library(ggplot2)
library(ggeffects)
library(magrittr)

#Open my dataset
myds<-read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/my_glmm_dataset.csv",
               fileEncoding = 'latin1')
myds <- myds[,-c(3)] # remove bad character variable

# Negative binomial GLMM
m.laden.1  <- glmer.nb(ladenant ~ Bioma +  poly(temp,2) + scale(UR) + (1 | formigueiro), data = myds)

# Visualize
plot <- ggpredict(m.laden.1) %>%
    plot(add.data = TRUE)

purrr::walk2(plot, names(plot),
             .f= ~ ggsave(paste0(.y, ".png"),
                          plot = .x,
                          dpi = 300,
                          width = 15,
                          height = 15,
                          units = "cm"))
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.