Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving a correlation matrix graphic as PDF

Tags:

plot

r

r-corrplot

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!

like image 814
bigjdawg43 Avatar asked Jan 29 '23 00:01

bigjdawg43


2 Answers

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()
like image 95
Joe Avatar answered Feb 05 '23 09:02

Joe


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))
like image 35
Rahi Avatar answered Feb 05 '23 08:02

Rahi