10

I have a pretty dumb question to ask everyone.

I am using ggpairs under GGally to create a correlation matrix, and somehow I found that GGally did not provide a saving function as ggplot2 did. The function ggsave did not work for a non-ggplot2 object. I tried to use pdf or png, but they did not work. I am wondering if there's an easy to save this picture to a local file? Thank you for your kind help.

Xinting WANG
  • 1,905
  • 2
  • 15
  • 20
  • 1
    Could you use getPlot to retrieve the ggplot object and then use ggsave on this object? – CMichael Oct 30 '14 at 12:42
  • Something similar to: `graph <- ggplot...` then `ggsave("mygraph.png", graph, width = 6, height = 6)` – KFB Oct 30 '14 at 12:49
  • @KFB But ggsave does not recognize an object that is not a ggplot2 object. I tried to use this but it's not working. – Xinting WANG Oct 30 '14 at 15:15
  • 2
    Reiterating my comment: `getPlot` from GGally returns the ggplot object. – CMichael Oct 30 '14 at 15:41
  • thank you for this question. I am a ggplot native and, while I once used base plotting, it's been a while, and for better or worse, I often tend to google before trying things. – flies Mar 27 '22 at 16:57

1 Answers1

17

While @CMichael's comment is nice (I didn't know that, hence +1), it's applicable only if you want to save a particular plot from GGally-generated plot matrix. I believe that you'd like to save the whole plot matrix - the need, which I've recently also experienced. Therefore, you can use a standard R approach and save the graphics by opening corresponding (to desired format) graphical device, printing the object and closing the device, which will effectively save the graphics in a desired format.

# use pdf() instead of svg(), if you want PDF output
svg("myPlotMatrix.svg", height = 7, width = 7)
g <- ggpairs(...)
print(g)
dev.off()
Aleksandr Blekh
  • 2,462
  • 4
  • 32
  • 64
  • 1
    This works! Actually the method provided by @CMichael works also, but could only retrieve part of the plot. While if you want to save the whole matrix, this is the solution to go. – Xinting WANG Dec 10 '14 at 02:23