distribute R pop-up figures on different parts of the screen - r

If I have multiple pop-up figures in R initiated with window(). However, they are all plotted in the same spot, masking each other. Is there a way to automatically space them out on the screen(s). What I am looking for is an R implementation of this Matlab function: https://se.mathworks.com/matlabcentral/fileexchange/37176-distribute-figures.
I am aware of the par(mfrow) and the layout() options for multipanel figures but they are not what I am looking for.

The function name is windows, not window. (It is only available in MS Windows, not other platforms.) It has arguments xpos and ypos that set the position. If you want your windows in random positions, do something like this:
windows(xpos = -300*runif(1), ypos = 200*runif(1))
(The negative value for xpos measures from the right side of the screen.)
If the graphics windows already exist, you can use arrangeWindows() to distribute them on your screen. (Again, only in MS Windows.)

Related

Locator() does not work on graphic device

I'm using RStudio and the RPGR package to elaborate some data. I need to obtain the coordiates of a mouse click on a plot. It works using the locator() function but i need a bigger version of my plot so i need to use a graphic device. I tried with window() and X11(), i manage to plot the data and my cursor becomes a cross, meaning it reads the locator function, but it does not record my clicks. Am I doing something wrong?
Iread that there are some problems with locator() and RStudio, could it be the cause?

Make Octave set a figure to be the active window (come to the "front of the screen")

When Octave draws a plot, I would like it to set that to be the active window automatically, so that it becomes visible and I don't have to click back and forth between windows to see if the code and plot have finished. Is this possible? Since it would require reaching outside of Octave and controlling the OS, I'm not sure; it depends on whether or not that capability is part of Octave but I haven't found a reference for it yet.
I can always tell Octave to close the figure before opening a new one in the code, but that could prevent me from drawing multiple plots on the same axes, and it would require me to code that command in every time. It would be nice if there were a direct way just to bring the plot to be visible and take dominance over other windows.
EDIT: Somehow, although I noted that Octave would be required to control the OS to achieve this, I completely forgot to mention what that was... I'm running Windows 10 with the default window manager; I believe that would be the Desktop Window Manager.
When you plot something on a figure (whether you specify the figure you're plotting to within the plot command explicitly or simply let it plot into the currently active figure implicitly), this does not automatically raise the figure window to the forefront.
To do so, call the figure again using the figure function, along with the handle that you want to raise.
Alternatively, if you're sure that the figure you want to raise is the currently active one, you can simply use the shg command (which is effectively equivalent to figure(gcf))
E.g.
Fig1 = figure; % (or figure(1) if you want to be explicit)
Fig2 = figure; % (or figure(2) if you want to be explicit)
figure(Fig1); plot(1:10); % raise Fig1 to the forefront, and plot.
PS: Note that there was a bug affecting this behaviour until recently (coincidentally submitted by yours truly :p See https://savannah.gnu.org/bugs/?45943 ). This is fixed in the latest version of octave though (i.e. 5.1.0)

How do I make the table fit in the image created by tableGrob?

I am using tableGrob in R to create a .png image of a formatted table, for inclusion in a MS Word report. For small tables this works, but for larger ones they do not fit in the image. I haven't been able to find any parameters that allow me to either force it to fit automatically, or to manually control the height and width of the image window.
I would be very grateful if somebody could show me how to do this.
I am using R version 3.3.1, via Rstudio version 1.0.136, on Linux Mint on a 64-bit PC. R packages used include gridExtra, gridGraphics, gtable and png.
The table (tabsave) is a simple data frame with 34 rows and six columns, the first being chr and all the others num.
Here is what the output looks like. You can see how the table extends beyond both the upper and lower borders of the image:
Here is the relevant code
gtab<-tableGrob(tabsave,rows=NULL,cols=nm,theme = ttheme_default(base_size=10)) # gtab is the graphical version of the table, for printing
png('test.png')
grid.draw(gtab)
dev.off()
Thank you for any help you can provide.
The solution is to give width and height parameters to the png function. They both default to 480, in the default unit of pixels (px).
So I just changed
png('test.png')
to
png('test.png',height=1200)
I played around with the width and height parameters until it gave the coverage I needed.
When the plotting is to the interactive plot device, rather than to a file (ie not using a function like png) it's just a matter of first resizing the plot window so it's big enough.

Is it possible to split a plotting device screen in R without losing the previous plot?

Is there an underlying reason within R's architecture that prevents the use of a function that updates a plotting device with a second high-level plotting function?
Matlab has some functions, like colorbar, that seem to work in this way - by shifting the original plot to make room for the new element: http://uk.mathworks.com/help/matlab/ref/colorbar.html
It seems to me that this would require splitting the screen (e.g. split.screen, layout) while moving the previous plot to one of the subareas.
For this to work, I guess that the calls to the previous device must be stored in order to update the new split screen - or is there another way?

Plot zoom and locator in RStudio

Is there a way to enable locator() functionality in the RStudio plot zoom? This only works in the smaller window (default bottom right) of RStudio but when you click on a viewer already open as a separate window, no coordinates are captured:
plot(iris$Petal.Width, iris$Petal.Length)
locator()
Perhaps the answer here is that is not currently implemented and that is why I couldn't find mention of it online.
I'm using RStudio version 0.99.491.
Thanks in advance.
This does not directly use RStudio's "Zoom" function, but gets pretty close at what you're probably after:
df <- data.frame(1:4)
windows()
plot(df)
locator(1)
A couple of notes:
You can't dynamically resize the window. If you want to zoom in, you first need to call windows(), then resize the window, then execute plot(df).
Be careful to specify the n argument for locator(). Otherwise it will crash your R session because of this bug. (Which hasn't been resolved AFAIK)
But if your purpose is to be able to use locator() on a zoomed version of a plot (i.e. if you have a very crowded plot), this should do the trick.

Resources