How to keep previous plots and windows in Gnuplot? - plot

I have some problems of using gnuplot. I just begin with it.
(1)What is the command for keeping the previous plot when I plot new data? Do I have to plot the old data and the new data at the same time?
(2)What is the command for open a new window while keep the old ones? Do I have to set the window's id by using, e.g.,
set terminal wxt 3
, before each plot?
Can anyone give me some help or some good references?

Usually, to plot several data set you would use
plot 'data1.dat', 'data2.dat'
You could also use replot to add one of the data sets later
plot 'data1.dat'
...
replot 'data2.dat'
To open a new window, you must use the window's id like with set terminal wxt 2. The old windows stay open, but you cannot interact with them anymore (zooming, scrolling etc.). See also the discussion to the quesiton Two interactive windows in Gnuplot with wxt terminal.

You already wrote the answers of your questions.
1.: You can use the replot command:
plot sin(x)
replot cos(x)
but this just expands to
plot sin(x), cos(x)
So, it replots all data and does not just add the cos(x).
2.: Yes, you can also switch between the windows to update the plots. But note that settings like ranges and labels are not stored per window / plot, but globally. If they are different for different plots, you have to change them every time.
You may also have a look at "set multiplot" to put several plots on one window / picture. But it is not so nice for interactive plotting, as you will notice.
Also, output terminals supporting multiple pages like pdfcairo will add a new page for each plot.

Related

Gnuplot: multiple plots in multiple windows

I want to do multiple plots related to differents data columns of the same file but I want a new window for each plot. I don't want attached plots associated to the command set multiplot layout 1,2.
The idea is represented by the following pseudocode:
>gnuplot
>plot "myfile.txt" u 1:4
>#make me plot another file without deleting the previous one i.e. open a new window on which data is plotted
>instructions I don't know
>plot "myfile.txt" u: ($3*$3)
>#I obtain two windows
You don't write which terminal you are using. I understand you want to have two windows next to each other, and not two graph as file on disk. In gnuplot console check for example: help wxt. In case you want two files on disk you have to select another terminal, e.g.
set term pngcairo
set output 'myOutput1.png'
plot x
set output 'myOutput2.png'
plot x**2
set output
So with the interactive wxt terminal the following works for me:
Code:
### several terminals
reset session
set term wxt 1 size 500,400
plot sin(x)
set term wxt 2 size 500,300
plot cos(x)
set term wxt 3 size 300,200
plot x**2
### end of code
Result:

How do you delete the current (but not all) plots in the RStudio plotting device?

How do you delete the current (but not all) plots in the RStudio plotting device?
dev.off() will remove all plots, but what if I just want to remove one? I don't want to have to press that red 'x' button because I want to remove one plot without pressing a button.
In R, you would just use dev.new() before each plot, so you dev.off() to only clear the last plot.
In RStudio, you can use x11(), windows() or quartz() (depending on your device) before each plot. Then call dev.off() to clear last plot. You can also use dev.set() to choose specific plots that way.
If your question is specifically asking to delete the last plot within the same RStudio window (rather than making new windows), not sure if it's possible, since RStudio treats that window as one device. An idea would be to look at a way to call the C++ function removePlot() in the RStudio project.
I found in the Github repository for RStudio the C++ code:
display.removePlot(display.activePlotIndex());
You could output the plots and manage the files that way.

How to reset 'par' (pty='s') value in R

I was trying to plot two graphs simultaneously. I did it usingpar(mfrow=c(2,1) and reset the par to default with par(mfrow=c(1,1).
I was trying to make the size of dots in the scatter plot and ended in trouble. I mistakenly used par(mfrow=c(,1),pty='s') and my plot got re-sized instead of re-sizing the size of scatter dots.
Sorry since Im new to R; I want to reset the size to default value. ie, the value for pty='s' should go to default. How can I do that!! I tried with par(opar) and par(resetPar()) which I found from stackoverflow, but both returning could not find error.
Also, may I know how to increase the size of scatter dot(s)? Should I ask this as separate question?
Thank you for your help..
Before modifying the graphical parameters with par it may be useful to store the previous parameters:
old_par = par()
Then you'll be able to come back to previous settings by typing par(old_par). For your current problem, default value for pty is "m".
In any case, if you don't want to close your current graphical device to get the old_par parameter, you can still open a new one x11() then the par function will concern the new window, and then close it dev.off()

How to add points to two plots parallelly ? (in R)

I am looking for ways to add points to three different plots in parallel.
I have three scatter plots named s3d1, s3d2 and s3d3 in a single window
layout(matrix(c(1,2,1,3),2, 2, byrow = TRUE))
s3d1<-scatterplot3d(mtcars[,3],mtcars[,4],mtcars[,5],main="common",pch=20)
s3d2<-scatterplot3d(mtcars[,3],mtcars[,4],mtcars[,5],main="common",pch=20)
s3d3<-scatterplot3d(mtcars[,3],mtcars[,4],mtcars[,5],main="common",pch=20)
If I try to add points to s3d1,
s3d1$points3d(mtcars[,3],mtcars[,4],mtcars[,5],col="red")
The points would go to s3d3 but not s3d1. What am I missing ?
More info : I obtain data points while running a program. So, I need to add points to each of these plots as-and-when I get the data specific to that particular plot.
Update :
Tried par() function as well
par(fig=c(0,0.65,0,1), new=TRUE)
s3d1<-scatterplot3d(mtcars[,3],mtcars[,4],mtcars[,5],main="common",pch=20)
par(fig=c(0.7,1,0.5,1), new=TRUE)
s3d2<-scatterplot3d(mtcars[,3],mtcars[,4],mtcars[,5],main="common",pch=20)
par(fig=c(0,0.65,0,1), new=TRUE)
s3d1$points3d(mtcars[,3],mtcars[,4],mtcars[,5],col="red")
s3d1$points3d doesn't add new points to s3d1 (and not even to s3d2). Any ideas ?
If you look at the source of points3d() by just trying to execute s3d1$points3d, you'll see that it just adds points to an existing plot that is assumed to be already open. In other words, the points/plot are not stored in the s3d1,2,3 objects, but simply the transformation info needed to plot to the different views.
Soo, to do what you're trying to do, you'll just have to use the normal graphics device commands. For instance, dev.new will open a new plot window, and dev.set can switch between the active ones. You could do something like:
dev.new(); h1=dev.cur()
s3d1<-scatterplot3d(mtcars[,3],mtcars[,4],mtcars[,5],main="common",pch=20)
dev.new(); h2=dev.cur()
s3d2<-scatterplot3d(mtcars[,3],mtcars[,4],mtcars[,5],main="common",pch=20)
dev.new(); h3=dev.cur()
s3d3<-scatterplot3d(mtcars[,3],mtcars[,4],mtcars[,5],main="common",pch=20)
dev.set(h1)
s3d1$points3d(mtcars[,3],mtcars[,4],mtcars[,5],col="red")
Also check out ?dev.new for more info.

Repeat plot command with minor changes in R

I made a plot in R and I want to repeat all the commands (like plot(), legend() or line()) that were carried out for this plot, with some minor changes. For example I want to set the axes to logarithmic scale and change the title of the plot.
In gnuplot I would use the replot command.
plot ...
set title "The same plot with logarithmic axes"
set logscale
replot
Is something like this possible in R. The only thing that comes to my mind of doing this (besides changing the values manually and re-run the lines of codes) would be setting up a function, that asks for all parameters that might be changed by the user.
Thanks for your help,
Sven
R uses a pen and paper graphics model - once the plot has been drawn on the device that is it. If you want to change some aspect of the plot, you need to replay the graphics function calls that produce the plot with the changes made to the code.
Depending on what you are really doing there are two options:
If this is just for you, write code in a text editor / IDE that knows R and can send chunks of code at a time to R. That way the code to produce the figure is recorded in a separate script which you can paste into/send to R making the changes you need each time to the script.
If you are going to be doing this often, then write yourself a wrapper plotting function that encapsulates the plot code you want but allows you to pass in arguments to alter the aspects you want.
Lattice and ggplot2 are a little different as they are based on grid graphics and create objects that when printed produce a plot on the device. One can manipulate that object to alter what is drawn, and with grid one can push and pop things on to / off a viewport.

Resources