An old man in the sea.
An old man in the sea.

Reputation: 1518

RStudio doesn't save picture

I'm trying to save a plot zoom I got from using R.

My Rstudio has the option of saving that plot, but it doesn't seem to be working. There's no error message. I go through everything as normal, and then the pic never shows up in the file that it's supposed to. I've tried open the picture, and the save in the default directory, but it still doesn't show up...

Upvotes: 17

Views: 19158

Answers (7)

Francis
Francis

Reputation: 1

If you're not already - you need to put the code for saving before the code for plotting. For example;

png(file = 'myplot.png', width = 480, height = 480)

hist(x)

dev.off()

Then it should save to your working directory or any folder you set file = to.

Upvotes: 0

Callen Bragdon
Callen Bragdon

Reputation: 15

I know I'm late to the party here, but I think I have a clue as to why the zoom figure doesn't save.

If you right click on the zoom image and either

  1. Click "copy image" and try to paste in nautilus
  2. Click "copy image address" and paste in web browser url bar

You'll get a "Refused to connect" response. Looking at my image address http://127.0.0.1:14481/graphics/etc

It seems RStudio makes the plot by trying to use port 14481 for some reason.

My suspicion, though I could be wrong, is that you would need to configure your firewall/iptables to listen to port 14481, and then give it a go.

Though, given the other answers, I wouldn't bother unless you REALLY wanted to ;)

Upvotes: 0

An old man in the sea.
An old man in the sea.

Reputation: 1518

The best way to do this, pressing zoom button in RStudio, then copy that pic to paint (which works), and then save it.

Upvotes: 0

RHertel
RHertel

Reputation: 23798

I can confirm this behavior in the latest release of RStudio (v. 0.99.902). A zoomed plot cannot be saved using the menus of RStudio, only the small image in the preview panel is saved with the "Export" pull-down menu. A right-click on the zoomed image, selecting "Save Image" does not work. There is no error message but nothing happens after the output directory and file name is selected and the "Save" button is clicked. I assume it's a bug. FWIW, I'm using ubuntu 16.04.

A quick workaround is to take a screenshot of the window containing the zoomed figure. There are specific OS-dependent keyboard combinations for this, like Alt+Print. Then one can use any image editor to crop the image and remove the window frame.

Other useful options have been posted as answers here, but what I've seen so far is unrelated to RStudio.

Upvotes: 11

user3603486
user3603486

Reputation:

Either of the following commands allows you to save a picture you have already created, without rerunning any code. This is often easier than using pdf, jpeg and friends.

# on mac:
quartz.save("test.png")
# on Windows/Linux:
savePlot("test.png")

Upvotes: 0

Arun kumar mahesh
Arun kumar mahesh

Reputation: 2359

You can use jpeg function for saving your plots

jpeg("plot.jpeg", width = 480, height = 480) # height and width can choose as your wish 
plot(x,y)
dev.off()

Upvotes: 1

Qaswed
Qaswed

Reputation: 3879

You can produce pictures automatically:

pdf("test.pdf")
plot(1, main = "my test PDF")
dev.off()

You can replace pdf("test.pdf") by png("test.png") or other formats of your choice.

Upvotes: 1

Related Questions