Ask Your Question
2

Why does pdf("my_plot.pdf") output an empty plot?

asked 2021-12-31 11:00:00 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-08-04 08:00:00 +0000

nofretete gravatar image

The reason pdf("myplot.pdf") outputs an empty plot is that it is only activating the PDF graphics device to create a new empty file called "myplot.pdf", but it is not producing any actual content to be saved in the PDF file. You need to first create a plot by specifying the data to be plotted and the type of plot you want to create using one of the plotting functions in R (e.g. plot(), ggplot()). Once you have created the plot, you can then use pdf() to save it to a PDF file.

Here is an example:

```{r}

Creating a sample plot

x <- 1:10 y <- x^2 plot(x, y, main = "My Plot")

Saving the plot to a PDF file

pdf("my_plot.pdf") plot(x, y, main = "My Plot") # this is optional, but it will ensure that the plot is saved correctly dev.off() # this will close the PDF graphics device and save the file ```

In this example, we first create a plot using the plot() function, and then use pdf() to save it to a PDF file called "my_plot.pdf". The dev.off() function is called to close the PDF graphics device and ensure that the file is saved correctly.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-12-31 11:00:00 +0000

Seen: 20 times

Last updated: Aug 04 '21