ggplot()不会将文件保存到文件夹

时间:2018-10-05 20:33:41

标签: r ggplot2

这是一个非常简单的问题:

对多个文件(在文件夹中)进行分析之后,ggplot不会将每个图形保存到我指定的文件夹中。但是,plot()函数可以。

png(paste0(myFiles[j], ".png"), units ="in", width=7, height=5, res=300)

df1 <- data.frame(MO, -log(GC1), -log(GC2))
colnames(df1) <- c("MO", "IPs -> DLPFc", "DLPFc -> IPs")

data1 <- melt(df1, id.vars = "MO", measure.vars = c("IPs -> DLPFc", "DLPFc -> IPs"))

ggplot(data1, aes(x = MO, y = value)) +
  geom_line(aes(colour=variable, group=variable), size=2) + 
  labs(x = "Model Length (ms)", y = "-Log(P-Value)") + 
  theme(axis.title = element_text(size = 20)) + 
  geom_hline(yintercept = 3, col='green', size  = 1.5) + 
  theme(legend.position="none") + 
  theme(axis.text.x = element_text(face="bold", color="black", size=15, angle=0), 
        axis.text.y = element_text(face="bold", color="black", size=15, angle=0)) +
  scale_y_continuous(limits = c(0,30), breaks = c(0, 3.0))

dev.off()

1 个答案:

答案 0 :(得分:4)

将您的ggplot调用的输出存储到变量,然后将其传递到ggsave

gg <- ggplot(...)
ggsave( paste0(myFiles[j], ".png"), gg, width=7, height=5 )