Currently trying to write a simple r script that when passed in 2 vectors of values would calculate some relationship between them (in the given case, r_square) and display it in a graph with the best fit line.
temp1 <- sample(20000,1367,replace=F,prob=NULL)
temp2 <- sample(20000,1367,replace=F,prob=NULL)
fit <- lm(temp1 ~temp2)
plot(temp1,temp2,ann="true")
abline(fit)
(here using sample in lack of real data).
The problem is that i'm trying to add interactivity which would display point's value (X/Y coordinates of sort) on hover.
I've managed to find a few functions that identify them by the their order in the vectors (HWidentify, identify, etc). But none of them give the actual value (x,y) so i was wondering if it's possible to print out coordinates that aren't permanent.
If you're trying to print the coordinates, you could use the labels argument to the identify() function.
identify(temp1, temp2, labels=paste(temp1, temp2, sep=","))
The HWidentify function also has a labels argument that you can set to whatever you want, using the paste function like #JeanV.Adams works similarly and then you have the hover functionability.
Related
I forgot the name of it. But in r, I could see every plot with every x variables against y variable at one time? But if I have lots of x variables, the result image was too huge to recognize. Besides, I couldn't enlarge the image. Is there any good way to get the results at one time and see every result with clear image?
You're possibly recalling the default plot method acting on a data.frame which plots a matrix of pairwise columns.
plot(iris)
If you want to restrict which columns are involved, you subset the data.frame first
plot(iris[, c(2,3,4)])
If you wish to view it larger, you can either enlarge the image window (whatever system you're using) or save the image at a high resolution.
Alternatively, pairs to specify which variables to plot against each other
pairs(iris, horInd = 1, verInd = 1:3)
I'm being threatened with having to hand draw hundreds of circles in PowerPoint with a very short turnaround and so I'm looking to R to do this for me. Forgive me, I've never programmed before, and I'm leaping straight in.
Essentially I'm drawing a scatterplot on a timeline. This I've done easily using ggplot2. Unfortuantely due to the hundreds of dots involved I need to be able to space them out so all can be seen. Jitter is not an option as I don't know my final number of dots. Therefore, I'm trying to build something which will eventually know how many dots are in a quadrant. First of all, I'm trying to assign my co-ordinates a value within a range:
getxcordinate <- function (x) {
x <- as.numeric(x)
runif(1,x-1,x)
}
getycordinate <- function (y) {
y<- as.numeric(y)
runif(1,y-1,y)
}
getxcordinate(x = my.data$t_score[1])
getycordinate(y = my.data$Geography[1])
#Precreate the offset as numeric x-y coordinates that so that the points and the text can refer to the same coordinates later
my.data$moved_t_score <- vapply(my.data$t_score,getxcordinate, any)
my.data$moved_Geography <- vapply(my.data$moved_Geography,getycordinate, any)
From the help I can see that vapply needs something called FUN.VALUE in order to work. I tried "any" but that was optimistic - what is it I should be doing?
Thanks!
Lets consider I am given a plot and I do not have its x and y vectors but I would like to extract them from the plot in Matlab. Also I am interested to know the increment of data (step size) in both horizontal and vertical axis(x and y axis).
I was thinking of using :
h=gca % Get current axis
X=get(h,'xdata');
Y=get(h,'ydata');
stepsize=X(2)-X(1);
But these command produce an error message that :
xdata and ydata are not accessible property of axis. Any suggestion how to find the x and y vectors for any given curve.
If I understand correctly, these are the two things you want to know:
You have a figure containing a plot of some arbitrary 2d line, whose x_vec, y_vec are unknown to you and you want to extract them from the figure\axes.
You want to get the xtick and ytick positions used in the figure you have.
The reason your code does not work, is because you're trying to access a property of the axes, whereas what you want to access is the property of the line (i.e. the curve in the plot).
To solve your first problem, you can resort to the following methods:
Manual: using the edit plot figure tool you can get to the XData and YData properties of the line, in the following manner:
Programmatic: you need to find the handle (i.e. pointer) to the line, and then use your code on that handle (and not on gca):
%// If there's only one entity (child) in the axes:
hLine = get(gca,'Children');
%// If there's more than one child:
hChildren = findobj(gca,'Type','line');
hLine = hChildren(1); %// Or any other logic you need to pick the correct line
%// Then comes your code:
xD = get(hLine,'XData'); yD = get(hLine,'YData');
For the second problem you can use gca to get XTick and YTick:
xT = get(gca,'XTick'); yT = get(gca,'YTick');
To get the step size I'd suggest simply using diff().
I'm not sure I quite understand your question. You mean get x and y data of a curve? If yes, then maybe it'll help looking into 'ginput'.
For example, picking 10 points from a figure window you can use the following command
[x,y] = ginput(10)
I'd like to add a point to an existing filled.contour plot, using the following code:
MyFunction <- function(x,y){
return(dnorm(sqrt(x^2+y^2)))
}
wrapper <- function(x, y, my.fun, ...) {sapply(seq_along(x), FUN = function(i) my.fun(x[i], y[i], ...))}
meshstep <- 0.5
x<- seq(-20,20,meshstep)
y <-seq(-20,20,meshstep)
z <- outer(x,y,FUN = wrapper, my.fun=MyFunction)
filled.contour(x,y,z, col=rev(heat.colors(n=20, alpha=0.7)), nlevels=15)
points(0,0)
I'm pretty surprised that points(0,0) didn't put a point into the origin of the plot, but roughly located at x=10,y=0. Also, locator() seems to be prompting coordinates with respect to that 'new' coordinate system as well. Why is that?
You can find a detailed answer here :
Plotting a box within filled.contour plots in R?
In short, filled.contour use two different coordinates system, one for the filled contour and one for the legend. To solve your problem, you either have to use another function, or to put your points into the plot.axes argument :
filled.contour(x,y,z, col=rev(heat.colors(n=20, alpha=0.7)), nlevels=15,
plot.axes={points(0,0)})
The best option is to use the plot.axes argument as mentioned by #juba. But, if you really need to add something after the plot has finished then you can use locator to click on 2 points in the plot where you know the values of the points in the coordinate system you want to use (opposite corners), then use the updateusr function from the TeachingDemos package to modify the current coordinate system to the one that you want to use. You can then add to the plot using the new coordinate system (you may need to set par(xpd=NA)).
I have a 2D CFD code that gives me the x and y flow velocity at every point on a grid. I am currently visualizing the data using a vector field in gnuplot. My goal is to see how far the plume from an eruption extends, so it would be much cleaner if I could prevent vectors from showing up at all in the field if they fall below a certain magnitude. Does anyone have an idea how to go about this? My current gnuplot script is below. I can also modify the input file as necessary.
reset
set nokey
set term png
set xrange [0:5.1]
set yrange [0:10.1]
do for [i=0:10] {
set title 'Eruption simulation: Timestep '.i
set output 'path/FlowVel'.sprintf('%04.0f',i).'.png'
plot 'path/Flow'.sprintf('%04.0f',i).'.dat' using 1:2:3:4 with vec
}
I guess you want a kind of filtering, which gnuplot doesn't really have, but can be achieved with the following trick (taken from "help using examples" in gnuplot):
One trick is to use the ternary `?:` operator to filter data:
plot 'file' using 1:($3>10 ? $2 : 1/0)
which plots the datum in column two against that in column one provided
the datum in column three exceeds ten. `1/0` is undefined; `gnuplot`
quietly ignores undefined points, so unsuitable points are suppressed.
Or you can use the pre-defined variable NaN to achieve the same result.
So I guess you would want something like this in your case
plot "data.dat" u 1:2:($3**2+$4**2>mag_sq?$3:NaN):($3**2+$4**2>mag_sq?$4:NaN) w vector
where mag_sq is the square of your desired magnitude.