Plot zoom and locator in RStudio - r

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.

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 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.

Wrong output size when plotting over an image in R

My goal is to read an image file in either the PNG or JPEG format and plot various data over said image and save it to disk.
I also want the image to take up all available space in the produced plot, no axes or labels or anything. I'm a bit concerned that this might be relevant to my problem.
Code example
Below is my current code that currently only tries to output the same image as you put in. Later I plan on plotting data points corresponding to coordinates over the image. I've used some sample code found here in order to remove the axes and be able to have the image in the background of the plot.
library(jpeg)
library(grid)
img <- readJPEG(system.file("img", "Rlogo.jpg", package="jpeg"),native=TRUE)
jpeg(filename = "Rlogo-2.jpg", width=100,height=76, quality = 100,res=299)
op<-par(mar=rep(0,4))
plot(0:100,type="n", axes="FALSE",ann="FALSE")
lim <- par()
rasterImage(img, lim$usr[1], lim$usr[3], lim$usr[2], lim$usr[4])
dev.off()
Example output
This is an example output of my above code in a comparison with the original image:
The image to the left is the original and the right one is the modified one. As you can see it seems as if the image I read and plot somehow is smaller than the original image and when saved to the original dimensions it appears blurred.
I've been pulling my hair over this one for hours and I don't seem to get anywhere. This is my first attempt to plot data over images and I'm aware of my lack of knowledge about how R represents images and I've mostly been using the basic graphics to do relatively simple plots before.
I'm currently considering doing this in Python instead but I'm afraid that'll come back and bite me when it comes to the actual plotting of the data.
I run R version 3.1.0 on x86_64 running Windows 7.
Just to summarize, since you already found the culprit, there are two issues present here:
Firstly, the blurring appears to be caused by the jpeg device on Windows. There is no such problem on Ubuntu Linux and it disappears if you use the Cairo-device instead, as you did already discover. Cairo-devices are great for pdf:s too since they embed all the fonts etc. making the figure look the same across platforms.
Secondly, R adds 4% extra margin to the x and y axes by default to prevent graphics from being chopped off near the edge of the plot area. It can be corrected by setting xaxs="i" and yaxs="i".
par(mfrow=1:2)
plot(1:5, 1:5) # Left
plot(1:5, 1:5, xaxs="i", yaxs="i") # Right
In your case the difference is subtle but still would cause everything to be slightly misaligned.

Can I navigate, zoom in and zoom out R plots?

I would like to know if R allows to pan and zoom images produced with the "plot" function. I would like to obtain the plot transformation using the mouse and not through the command line, is it possible?
Thanks in advance.
It's a bit old question but for future references, I've wrote a package doing precisely that (based on zoomplot {in pkg:TeachingDemos}). It's called zoom:
The CRAN page on zoom
And you are welcome to check the latest and greatest and expand the package on Github
Usage -
library(zoom) # Invoke the Library
# Call plot
zm()
The instructions for Usage in a normal plot is
Mouse:
Scroll to zoom in and out
Hold left mouse button to move
Keyboard:
Left/Right (h/l): move left/right
Up/Down (k/j) : move up/down
+ or i / - or o : zoom in/out
L/H : zoom in/out (x-axis only)
K/J : zoom in/out (y-axis only)
p : print to file
r : reset limits
s : show limits
q on the graphic window to quit
Not with the default plotting device as its internal model is 'static'.
There are workarounds, tough: look for example at package playwith, or at a Java-based device such as the iplots package on Simon's rforge. There are also extensions like ggobi and other on-going work (such as the Qt-based device which will in time replace ggobi) which try to make R-based graphing more dynamic.
But the main takeaway is: not, not really -- due to the way R graphics devices are designed.
To add to Dirk's answer: see also the iWebPlots and RnavGraph packages.
You should look at Greg Snows zoomplot {in pkg:TeachingDemos}. You may be able to call that function with cursor interface using the tk toolkit. I think Greg's package also has examples of such an interface.
To add to existing answers: I like to use the identify() function to manually add labels just to particular points I want.
You can also add R graphs to plot.ly . This should be enough for the most common stuff.
Stefan
Now there are also http://www.htmlwidgets.org/ that add a great set of interactive visualizations capabilities to R. Most of them can easily be zoomed and panned.

Resources