1

I am using the MatchIt package to generate a matched dataset. I wonder how to save a plot showing the absoluted SMDs before and after the matching, i.e. plot(summary(my_matchitinstall.packages("MatchIt").

Please see reproducible example below:

#install.packages("MatchIt")
library(MatchIt)
data("lalonde")
# Default: 1:1 NN PS matching w/o replacement
m.out1 <- matchit(treat ~ age + educ + race + nodegree +
                    married + re74 + re75, data = lalonde)
m.out1
summary(m.out1)
my_plot <- plot(summary(m.out1))
Pontus Hedberg
  • 301
  • 1
  • 2
  • 9
  • 2
    Does this answer your question? [How to save a plot as image on the disk?](https://stackoverflow.com/questions/7144118/how-to-save-a-plot-as-image-on-the-disk) – jdobres Apr 16 '22 at 14:06
  • I tried to save it as .pdf using: pdf(file = "my_plot.pdf", onefile = FALSE, width = 15, height 7.5) my_plot dev.off() but this didn't work. I would prefer to save it in vector format. – Pontus Hedberg Apr 16 '22 at 14:08
  • You should say what you mean by "vector format". If it means as an SVG file then there is an `svg` function that should be an effective graphics device. – IRTFM Aug 26 '23 at 23:27

1 Answers1

0

To finalize the file, you need to close the device with dev.off():

pdf(file = "my_plot.pdf", onefile = FALSE, width = 15, height = 7.5)
my_plot <- plot(summary(m.out1))
dev.off()

enter image description here

jdobres
  • 11,339
  • 1
  • 17
  • 37