I have a problem concerning the function ggsave() and I would be really grateful for any help and or suggestions/solutions. I am creating four plots and put them all in one big plot, and since I want to loop the whole function using all columns in my dataframe I want to save the created plots in a specified folder (preferably with an identifying name).
plotting_fun3 <- function(Q){
plot1 <- plot_likert(
t(Q),
title = "Total population",
legend.labels = c("strongly disagree","disagree", "neither nor", "agree", "strongly agree"),
grid.range = c(1.6, 1.1),
expand.grid = FALSE,
axis.labels = c(" "),
values = "sum.outside",
show.prc.sign = TRUE,
catcount = 4,
cat.neutral = 3,
)
plot2 <- plot_likert(
t(Q[survey$animal=="Dogs"]),
title = "Female",
legend.labels = c("strongly disagree","disagree", "neither nor", "agree", "strongly agree"),
grid.range = c(1.6, 1.1),
expand.grid = FALSE,
axis.labels = c(" "),
values = "sum.outside",
show.prc.sign = TRUE,
catcount = 4,
cat.neutral = 3,
)
plot3 <- plot_likert(
t(Q[survey$animal=="Cats"]),
title = "Male",
legend.labels = c("strongly disagree","disagree", "neither nor", "agree", "strongly agree"),
grid.range = c(1.6, 1.1),
expand.grid = FALSE,
axis.labels = c(" "),
values = "sum.outside",
show.prc.sign = TRUE,
catcount = 4,
cat.neutral = 3,
)
plot4 <- plot_likert(
t(Q[survey$animal=="Turtle"]),
title = "Others",
legend.labels = c("strongly disagree","disagree", "neither nor", "agree", "strongly agree"),
grid.range = c(1.6, 1.1),
expand.grid = FALSE,
axis.labels = c(" "),
values = "sum.outside",
show.prc.sign = TRUE,
catcount = 4,
cat.neutral = 3,
)
theplot <- ggarrange(plot1, plot2, plot3, plot4,
labels = NULL,
common.legend = TRUE,
legend = "bottom",
ncol = 1, nrow = 4)
#ggsave(filename=paste(Q,".png",sep=""), plot=theplot, device = "png")
#ggsave(filename=paste("animal_plot", ID, ".jpeg"), plot=plots[[x]])
#ggsave(path = "/myDirectory",
# device = "png", filename = "animal_plot", plot = theplot)
#save_plot(filename = "hello", plot = theplot,
# "/myDirectory",
# device = "png")
#ggsave(sprintf("%s.pdf", Q), device = "pdf")
return(theplot)
}
The commented lines show all kinds of ways I have tried to save the plot in my directory. I encounter 2 different problems:
Either: Most of the ggsave suggestions I found on stack overflow. Several of them did not include the line device = "png"
. If I leave out this line of code I always get something like this:
Fehler: `device` must be NULL, a string or a function.
Run `rlang::last_error()` to see where the error occurred.
If I follow that command I get:
<error/rlang_error>
`device` must be NULL, a string or a function.
Backtrace:
1. global::plotting_fun3(survey[, 9])
2. ggplot2::ggsave(sprintf("%s.pdf", Q))
3. ggplot2:::plot_dev(device, filename, dpi = dpi)
Run `rlang::last_trace()` to see the full context.
> rlang::last_trace()
<error/rlang_error>
`device` must be NULL, a string or a function.
Backtrace:
█
1. └─global::plotting_fun3(survey[, 9])
2. └─ggplot2::ggsave(sprintf("%s.pdf", Q))
3. └─ggplot2:::plot_dev(device, filename, dpi = dpi)
So online I found people with the same or similar problem and the suggestion has always been to use device = "png"
or similar.
Now if I do this I encounter a different problem: The plots are saved in the right directory but the name is wrong. Usually the name is "3.png" or "3.pdf" or depending on what I create. If "3.png" already exists it gives the file another number.
I had this problem in an older project three months ago and couldn't solve it and now I have it again.
For what it's worth, I use macOS Mojave 10.14.6, my R version is Version 1.3.1093
Thank you in advance for any thoughts, suggestions or other comments.
[EDIT]
Here is some sample data:
> str(myDF[,c(2,9:10)])
data.frame': 123 obs. of 3 variables:
$ animal: chr "Cats" "Cats" "Turtles" "Cats" ...
$ q8 : int 3 5 5 3 4 4 2 5 3 5 ...
$ q9.1 : int 4 5 5 4 3 4 2 4 2 4 ...
The values stay between 1 and 5 for all observations. They actually represent answers such as "strongly agree", "agree", "neither agree nor disagree"...etc.
Alternatively, if you prefer this to the other one:
> myDF[,c(2,9:10)]
animal q8 q9.1
1 Cats 3 4
2 Cats 5 5
3 Turtles 5 5
4 Cats 3 4
5 Turtles 4 3
6 Turtles 4 4
7 Turtles 2 2
8 Cats 5 4
9 Cats 3 2
10 Turtles 5 4
11 Turtles 4 3
12 Turtles 3 3
13 Dogs 3 3
14 Cats 3 3
15 Dogs 1 1
16 Dogs 1 3
plot_likert
orggarrange
? Does each of your attempts give the exact same error message? – Disjunctive