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!
Related
I need to plot all the elements of a[n] 0<=n<=30. Is that possible in maxima?
These are the numbers i need to plot
by the way, you can help others help you by typing your work so far into your question. That way people can see it immediately instead of having to go look for it.
About plotting the points, remember that a[n] as you have defined it is a so-called array function instead of a list or array. The plotting functions can work with lists, I believe. So you need to evaluate your function at a number of points and then plot the points.
Something like: mypoints: makelist([n, a[n]], n, 1, 30); to generate the points. Then with the points in hand, there are different ways to plot them. You can try: plot2d([discrete, mypoints]); There are other ways; see wxdraw2d for example.
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)).
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.
When doing matrix operations, I would like to be able to see what the results of my calculations are, at least to get a rough idea of the nature of the matrices going in and coming out of the operation.
How can I plot a matrix of real numbers, so that the x axis represents columns, the y represents rows, and the color or size of a point represents the cell value?
Ultimately, I would like to display multiple plots, e.g. the right and left hand sides of an equation.
Here is some example code:
a <- matrix(rnorm(100), ncol = 10)
b <- diag(1,10)
c <- a*b
par(mfrow = c(1,3))
plot.matrix.fn <- function(m) {
#enter answer to this question here
}
lapply(list(a,b,c), plot.matrix.fn)
update: since posting this question, I found that there are some great examples here: What techniques exists in R to visualize a "distance matrix"?
You could try something like (adjusting the parameters to your particular needs)
image(t(m[nrow(m):1,] ), axes=FALSE, zlim=c(-4,4), col=rainbow(21))
producing something like
See ?image for a single plot (note that row 1 will be at the bottom) and ?rasterImage for adding 1 or more representations to an existing plot. You may want to do some scaling or other transformation on the matrix first.
Not an answer but a longer comment.
I've been working on a package to plot matrices using grid.raster, but it's not quite ready for release yet. Your example would read,
library(gridplot)
row_layout(a, b, c)
I found that writing custom functions was probably easier than tweaking 10s of parameters in lattice or base graphics, and ggplot2 lacks some control over the axes.
However, writing graphics functions from scratch also means reinventing non-trivial things like layout and positioning; hopefully Hadley's scales and guides packages can make this easier. I'll add the functions to gridExtra when the overall design seems sound and more stable.