Wanted to see if anyone could help me out with an error I'm getting when trying to save a TIFF of a plot I'm creating. The plot preview shows up in my Rstudio window but without the tiff function running the formatting is off...
The error occurs when I run the following three lines:
tiff("CREATINE** fit dot plot Day 2 and 4", width=6, height=6, units='in', res=1080, compression = 'none')
plot(g3)
print(g3)
The errors in my console read:
tiff("CREATINE** fit dot plot Day 2 and 4", width=6, height=6, units='in', res=1080, compression = 'none')
plot(g3)
# `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
print(g3)
# TIFFOpen: CREATINE** fit dot plot Day 2 and 4: Cannot open.
# `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
# Warning message:
# In grid.newpage() :
# unable to open TIFF file 'CREATINE** fit dot plot Day 2 and 4'
dev.off()
# TIFFOpen: CREATINE** fit dot plot Day 2 and 4: Cannot open.
# RStudioGD
# 2
# Warning message:
# In dev.off() :
# unable to open TIFF file 'CREATINE** fit dot plot Day 2 and 4'
My full code is below:
Sys.time()
[1] "2024-02-14 13:37:07 MST"
day_2<-subset(Merged.Master.File_5.24.22_circ, Study.Day==2)
day_4<-subset(Merged.Master.File_5.24.22_circ, Study.Day==4)
library(ggplot2); library(lme4)
theme_new <- theme_set(theme_classic())
theme_new <- theme_update(text = element_text(colour="black", size = 15, family="sans"))
theme_new <- theme_update(legend.position = "top", legend.text = element_text(colour="black", size = 14), legend.title=element_blank(), plot.margin=unit(c(0,0,0,0),"mm"))
theme_set(theme_new)
day2_DFM <- lmer(CREATINE~cos((2*pi*Time)/24) + sin((2*pi*Time)/24) + (1|Subject), data=day_2, REML=F, na.action=na.omit)
day4_DFM <- lmer(CREATINE~cos((2*pi*Time)/24) + sin((2*pi*Time)/24) + (1|Subject), data=day_4, REML=F, na.action=na.omit)
###not sure how important these 2 lines are###
FittedFE.D2 <- function(day2_DFM) model.matrix(day2_DFM) %*% fixef(day2_DFM)
head(FittedFE.D2(day2_DFM))
plotdata.D2 <- data.frame(day2_DFM@frame, fitted = FittedFE.D2(day2_DFM))
plotdata.D2$Time <- day_2$Time
FittedFE.D4 <- function(day4_DFM) model.matrix(day4_DFM) %*% fixef(day4_DFM)
plotdata.D4 <- data.frame(day4_DFM@frame, fitted = FittedFE.D4(day4_DFM))
plotdata.D4$Time <- day_4$Time
myY <- scale_y_continuous(name = "Creatine (DFM)")
myX <- scale_x_continuous(breaks=c(1,5,9,13,17,21), name = "Relative Clock Hour")
plotdata <- rbind(plotdata.D2, plotdata.D4)
plotdata$day<-Merged.Master.File_5.24.22_circ$Study.Day
g1 <- ggplot(data=plotdata,aes(y= CREATINE, x=Time)) + stat_summary(aes(y=CREATINE, group=day, colour=factor(day)), fun=mean, geom="point", size=2, shape=16) + myX + myY + scale_color_manual(values=c("black", "red"), labels=c("Baseline", "Circadian Misalignment")) + theme(legend.title = element_blank()) + theme(legend.position = "top")
print(g1)
plotdata$SEM.hi <- Merged.Master.File_5.24.22_circ$SEM.hi
plotdata$SEM.lo <- Merged.Master.File_5.24.22_circ$SEM.lo
g2 <- g1 + geom_errorbar(data=plotdata, aes(ymin=SEM.lo, ymax=SEM.hi, group=day, colour=factor(day)), width=0.3)
print(g2)
g3 <- g2 + stat_smooth(aes(y=fitted, group=plotdata$day, colour=factor(plotdata$day)), span=0.9, geom="line", linetype=1, lwd=1, se=F) + coord_cartesian(ylim = c(-0.5, 0.5))
print(g3)
tiff("CREATINE** fit dot plot Day 2 and 4", width=6, height=6, units='in', res=1080, compression = 'none')
plot(g3)
print(g3)
dev.off()
Thank you so much for any feedback!
I tried running the ggsave function instead of tiff and get the following error:
ggsave("CREATINE** fit dot plot Day 2 and 4", width=6, height=6, units='in', res=1080, compression = 'none')
# Error in `ggsave()`:
# ! `filename` has no file extension and `device` is "NULL".
# Run `rlang::last_trace()` to see where the error occurred.
rlang::last_trace()
# <error/rlang_error>
# Error in `ggsave()`:
# ! `filename` has no file extension and `device` is "NULL".
# ---
# Backtrace:
# ▆
# 1. └─ggplot2::ggsave(...)
# Run rlang::last_trace(drop = FALSE) to see 3 hidden frames.