Saving a correlation matrix graphic as PDF
Asked Answered
B

3

9

I have an correlation matrix object I created with 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)

I'm trying to save it as a pdf. For some reason I can't figure out how to do that. Any help appreciated. Thank you!

Boice answered 31/5, 2018 at 19:40 Comment(0)
T
16

Start the pdf graphics driver then call your plot.

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()
Tutu answered 3/6, 2018 at 10:16 Comment(0)
E
11

Although it is an old question, I thought of providing an alternate approach using recordPlot(), replayPlot() and 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))
Eaglewood answered 29/7, 2021 at 12:37 Comment(1)
This is not working for me. The resulting PDF is invalid.Creatine
N
3
# 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()
Nee answered 15/7, 2021 at 15:23 Comment(1)
If you are using RMarkdown put the above code in a block and run the entire block instead of line by line. On MacOS use type="quartz" in order to see the labels.Creatine

© 2022 - 2024 — McMap. All rights reserved.