Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.