PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, June 27, 2022

[FIXED] When using par(mfrow=c(2,1)) the graphs are not shown while being created

 June 27, 2022     graph, r     No comments   

Issue

I'm using Rstudio and I can't save the plots and at the same time see them while they are being created on the window or plot pane. If I run line by line:

par(mfrow = c(2, 2))
hist(cars$dist)
hist(cars$dist, breaks=10)
hist(cars$dist, main="1920's Vehicles", xlab="Stopping Distance")

the graphs are created and are being shown one by one on the plot pane, but when I add the option to save the file they don't show up when I run line by line. The file is saved correctly, but why can't I see them as usual?

jpeg(file="results/whatever.jpeg")
par(mfrow = c(2, 2))
hist(cars$dist)
hist(cars$dist, breaks=10)
hist(cars$dist, main="1920's Vehicles", xlab="Stopping Distance")

Thanks!

Here it shows what I want to see, and it only happens when I skip the line that saves the plot:

enter image description here


Solution

It is quite simple once you have played through it.

First close all devices that may be open.

graphics.off()

Now device list should be empty.

dev.list()
# NULL

From this point, when we use par, plot, hist or probably any graphics related function, a device is opened.

par(mfrow=c(2, 2))

dev.list()  ## check
# RStudioGD       png 
#         2         3 

Voilá, if you are using RStudio RStudioGD and png now are open devices! They are shown with their ID.

Not exactly sure why RStudio opens two devices (in contrast, e.g. VScode opens only one pdf device), but our current device is:

dev.cur()
# RStudioGD
#         2

Now we usually plot exactly in this device which may be considered as preview device.

hist(cars$dist)
hist(cars$dist, breaks=10)
hist(cars$dist, main="1920's Vehicles", xlab="Stopping Distance")

If we now would use dev.off(), the device is closed and the (preview) plot gone.

When we use a different device such as jpeg, pdf, png, and postscript, it's a little different. Here we need to close the device so that the plot is saved.

jpeg(file="whatever.jpeg")  ## open device

dev.list()  ## check
# jpeg 
#    2 

par(mfrow=c(2, 2))
hist(cars$dist)
hist(cars$dist, breaks=10)
hist(cars$dist, main="1920's Vehicles", xlab="Stopping Distance")

dev.off()  ## close device and save jpeg

Note: And as I said, if for some reason you end up with a lot of open devices, you can close them all at once using graphics.off().

In summary, either you use the preview device or the jpeg device. So you can't have both happenning at the same time.



Answered By - jay.sf
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing