How to save a rpart.Plot chart?

Asked

Viewed 101 times

2

I am working with Décision Tree and at the end I have the chart that represents it. However, as it is a different object from other types of the ggplot or ggpubr family, I am not able to save by the script. The solution has been by the Export/Save Image button.

This solution limits some things, among them the automation of the process. What is the solution to this problem?

# Exemplo para geração do plot

require(rpart.plot) 
binary.model <- rpart(survived ~ ., data = ptitanic, cp = .02)

rpart.plot(binary.model,
           main = "titanic survived\n(binary response)")

rpart.plot(binary.model, type = 3, clip.right.labs = FALSE,
           branch = .4,
           box.palette = "Grays",       # override default GnBu palette
           main = "type = 3, clip.right.labs = FALSE, ...\n")

1 answer

2


The R has a series of commands to save figures in bmp, jpeg, png, tiff and pdf formats. Below is an example of how to create a graph in png format using one of these commands.

png("arquivo.png")
rpart.plot(binary.model, type = 3, clip.right.labs = FALSE,
           branch = .4,
           box.palette = "Grays",       # override default GnBu palette
           main = "type = 3, clip.right.labs = FALSE, ...\n")
dev.off()

Experiment with the arguments of the function that saves the image in the desired format so that it is possible to configure the resolution of the image according to your need.

  • Ok! Solve the save part. However, it just went to the directory. That is, I could not control, for example the width, nor height, as well as the Scale for the resolution of the image. Procedures that are easy on ggplot graphics with ggsave().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.