ggplot2保存TIFF格式绘图失败问题求助
解决ggplot2绘图保存TIFF文件失败的问题
问题描述
用ggplot2绘制完成g3后,RStudio能正常预览,但调用tiff()函数保存TIFF文件时提示无法打开文件;改用ggsave()函数也出现错误。
报错详情
使用tiff()函数的报错
执行代码:
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()
控制台输出:
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'
使用ggsave()函数的报错
执行代码:
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.
完整绘图代码
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()
解决方案
问题根源
- 文件名含非法字符:文件名中的
**是系统禁止的特殊字符,导致tiff()无法创建文件; - ggsave缺少扩展名:
ggsave需要明确的.tiff扩展名来识别输出设备,无扩展名时会报错。
修复步骤
方法1:修正tiff()函数的调用
移除文件名中的特殊字符,添加.tiff扩展名,同时ggplot对象只需调用print()即可:
tiff("CREATINE_fit_dot_plot_Day_2_and_4.tiff", width=6, height=6, units='in', res=1080, compression = 'none') print(g3) dev.off()
方法2:正确使用ggsave()
添加.tiff扩展名,可指定device = tiff确保格式正确:
ggsave("CREATINE_fit_dot_plot_Day_2_and_4.tiff", plot = g3, width=6, height=6, units='in', res=1080, compression = 'none', device = tiff)
额外注意事项
- 文件名避免使用
*、/、:等系统保留字符; - 确认当前工作目录有写入权限,可通过
getwd()查看目录,setwd()切换目录; - ggplot对象无需调用
plot(),仅用print()输出到绘图设备即可,重复调用会导致重复绘图。
内容的提问来源于stack exchange,提问作者misia
相关产品推荐
相关产品推荐

