Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save the plots as JPEG or PDF from jupyter notebook with R Kernel

I'm having a trouble with saving the plots as jpeg or pdf format from R kernel of Jupyter Notebook.

Here's the code,

pdf("Hatdog.jpeg")
plot(x,y, col="green")
dev.off() 

I was wondering why the image is corrupted, which when I tried to open the actual file, it states that It looks like we don't support this file format.

enter image description here

Anyone having the same issue? How did you solved it? Thanks.

like image 467
King Avatar asked Sep 19 '25 21:09

King


1 Answers

You are saving a pdf file with a -.jpg extension? To save as a jpeg:

# 1. Open jpeg file
jpeg("rplot.jpg", width = 350, height = 350)
# 2. Create the plot
plot(x = my_data$wt, y = my_data$mpg,
     pch = 16, frame = FALSE,
     xlab = "wt", ylab = "mpg", col = "#2E9FDF")
# 3. Close the file
dev.off()

To save as a pdf:

# Open a pdf file
pdf("rplot.pdf") 
# 2. Create a plot
plot(x = my_data$wt, y = my_data$mpg,
     pch = 16, frame = FALSE,
     xlab = "wt", ylab = "mpg", col = "#2E9FDF")
# Close the pdf file
dev.off() 

(from http://www.sthda.com/english/wiki/creating-and-saving-graphs-r-base-graphs)

like image 149
Arcoutte Avatar answered Sep 22 '25 11:09

Arcoutte