将相关矩阵图形保存为PDF

时间:2018-05-31 19:40:21

标签: r plot r-corrplot

我有一个用corrplot

创建的相关矩阵对象
p1 <- corrplot(correlations_history, method = "number", type = "lower", title = "Regional Factor Correlation Matrix over history", mar = c(0,0,1,0), number.cex = 0.5, number.digits = 2)

我试图将其保存为pdf。出于某种原因,我无法弄清楚如何做到这一点。任何帮助赞赏。谢谢!

3 个答案:

答案 0 :(得分:2)

启动pdf图形驱动程序,然后调用你的情节。

pdf(file = "yourfilename.pdf")

corrplot(correlations_history, method = "number", type = "lower", 
title = "Regional Factor Correlation Matrix over history", 
mar = c(0,0,1,0), number.cex = 0.5, number.digits = 2)

dev.off()

答案 1 :(得分:0)

# Initialize file path
file_path= "Correlation matrix.png"
png(height=1800, width=1800, file=file_path, type = "cairo")

# Your function to plot image goes here
corrplot(
         correlations_history, 
         method = "number", 
         type = "lower", 
         title = "Regional Factor Correlation Matrix over history", 
         mar = c(0,0,1,0), 
         number.cex = 0.5, 
         number.digits = 2
       )

# Then
dev.off()

答案 2 :(得分:0)

虽然这是一个老问题,但我想提供一种使用 recordPlot()replayPlot()ggsave() 的替代方法。

p1 <- { # Prepare the Corrplot 
       corrplot(correlations_history, 
                method = "number", 
                type = "lower", 
                title = "Regional Factor Correlation Matrix over history", 
                mar = c(0,0,1,0), 
                number.cex = 0.5, 
                number.digits = 2);
        # Call the recordPlot() function to record the plot
        recordPlot()
       }

# In case if you want to save the image using ggsave
# replayPlot basically prints the plot.
library(ggplot2)
ggsave(filename = "p1.pdf", plot = replayPlot(p1))