Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress plot window in R when saving output of plot command to variable

Tags:

plot

r

I want to save the matrix of values that the barplot() command returns into a variable, but I don't want the plot window:

x <- 1:5
b <- barplot(x)  # this opens a window with the barplot

I have seen the answers that suggest to create a new function from the barplot function or to send the graphic to a "NULL file", but I'm not happy with either of these options.

Isn't there a way to tell a plot function not to plot, but only return its values? In the same way that you can use type = "n" to suppress the points and axes = FALSE to suppress the axes.

I was also thinking of lm(), which outputs the results, and lm0 <- lm(), which doesn't. Here, assigning the function to a variable suppresses any output to the GUI.


1 Answers

You can specify the plot argument to barplot

b <- barplot(x, plot=FALSE)
like image 195
G5W Avatar answered Dec 02 '25 16:12

G5W