Question: I want to use ggplot in ipython notebook. I pass in an array into R using the rmagic and can plot it to an output cell. However, saving the plot to a file does not seem to work. 

```
%load_ext rpy2.ipython

```

New cell. My python data is called data.

```
%%R -i data
library(ggplot2)

df=data.frame(x=seq(1,length(data)),y=data)
pdf('plot.pdf')
ggplot(data=df, aes(x=x,y=y)) + geom_point()
dev.off()

```

When I click on the file plot.pdf I get the error 'File cannot be opened. It may be damaged or use a file format that Preview doesn’t recognize.'

Answer: 
If opening a particular device (here a PDF file), you will have to call `print` (same as if you were having that code in a non-interactive R).

```
df=data.frame(x=seq(1,length(data)),y=data)
pdf('plot.pdf')
p <- ggplot(data=df, aes(x=x,y=y)) + geom_point()
print(p)
dev.off()

```

This might be more explicit than a call to ggsave that appear to guess what should be plotted when having several plots.
Try this:

```
df <- data.frame(x=seq(1,length(data)),y=data)
ggplot(data=df, aes(x=x,y=y)) + geom_point()
ggsave('plot.pdf', width=8.27, height= 11.69) #A4 size in inches
dev.off()

```