Hello, I have created an R code which in a folder puts the graphs of the linear regression on me, but I would like to show on these graphs the r-squared and the p-value of each lm(with NAs) . How to do this?
library(ggplot2)
library(ggpmisc)
setwd(work)
files <- list.files(path = "data", pattern = (".csv$"))
for (k in 1:length(files)) {
fname <- files[k]
cat(paste0("Now analyse data/", fname, "...\n"))
data <- read.csv2(paste0("data/", fname), header = T, stringsAsFactors = F, dec = ",")
setwd(graphe) #faire 1dossier par fichier
newdir <- paste0(fname)
dir.create(newdir)
for(i in 2:ncol(data)){
cwd <- getwd()
setwd(newdir)
jpeg(paste(i, "jpeg", sep = "."), width = 15, height =12, units="cm", quality=75, res=300)
p <- ggplot(data) +
geom_point(aes_string(y = "score", x = colnames(data)[i]))+
geom_smooth(aes_string(y = "score", x = colnames(data)[i]),method=lm, colour="red", fill="red", alpha=0.25)+
theme_classic ()
print(p)
dev.off()
setwd(cwd)
}
setwd(work)
}
Thank you in advance
pattern=(".csv"$)
?newdir <- paste0(fname)
makes no sense and is not required.newdir <- fname
should suffice.jpeg()
anddev.off()
instead of the more elegantggsave()
?ggplot()
object top
only toprint()
it? Why not just callggplot()
and be done with it?Thank you for your answer (My data has NAs)
As I want to form the plots (among other things) in specific places, I thought that the best way was to always change the directory.
the brackets were advised by my supervisor. As I'm a beginner in R, I followed everything he told me.
Okay, thanks, I didn't know.
As explained above, I regularly change directories to save the plots, etc. in a specific place.
I found the jpeg and dev of technique on the internet because I didn't understand well how to record with ggsave.
by using ggsave, I think we can remove the print step
Everyone starts out with bad code. If not for that, we could never start learning. You're already doing things right, you only need to build consistency. For example, see how the
read.csv2
usespaste0()
to read a file within a sub-folder? The same thing can be done with everything else.thank you for your proposal, but R does not recognize the different folders and files.I will take into account your comments and try to improve my code, thank you
Sorry, what? That does not make sense. If there is a particular error message, we can figure out what's going on.
It was actually a mistake on my part the folders create well but you still have to put the ggplot in a variable and then print it because otherwise no graph can be saved. Thank you, I will be able to make my script clearer. Thank you for your time.
edit : and I don't want to create a folder with the name of csv but with the name of the file without the extension
Sorry, I'd forgotten a
dev.off()
line (which can be avoided ifggsave()
is used instead ofjpeg()
..dev.off()
. I don't think you'd need to save the plot to a variable.ggplot()
draws the plot to the active device automatically.There should be some function to get the file's basename without extension from the full filename. See the
tools
package.