I have a plot like this (black points):
I calculate the average of some points (smoothing - the red points in the plot).
Then I can draw with the lines() function a line between these points (blue in the plot). What I want now is the data of the blue line.
Does somebody know how to do this? Is it even possible?
I used the approx function which interpolates between points:
approx(x = r, y = ionizations, n = 1000)
Related
I have plotted a graph that contains a trig function. Instead of having the x-axis be in radians, I would like it to be in degrees. It's a simple task of multiplying the displayed x-axis values by 180/!pi but I can't figure out how to do it. Let's say I'm trying to graph the cosine function as
q=plot(cos(x))
How can I change the displayed x-axis values to degrees?
Just pass the x-values to the plotting routine:
x = findgen(360)
y = cos(x * !dtor)
plot, x, y
Or, if you are using function graphics:
p = plot(x, y)
I have two vectors representing x and y-coordinates in a scatter plot, and a thrid variable (z) for each (x,y)-coordinate representing the variable from which to draw contour lines. Example data are given as follows:
df<-data.frame(x=runif(n=30,min=-6,max=6),
y=runif(n=30,min=-6,max=10),
z=seq(1,100,length.out=30))
I use the R-package akima to generate the z-matrix for the contour plot
library(akima)
M1 <- interp(x=df$x,y=df$y,z=df$z)
contour(x=M1$x,y=M1$y,z=M1$z)
I now want to draw arrows perpendicular to the contourlines, preferably using something like the function "quiver" in the R-package pracma, with the origin of an arrow at every (x,y)-coordinate and with the arrow pointing in the direction of the gradient of the contourlines. Is there a way to do this?
My best idea so far is to somehow extract (x,y)-gradients of the contourlines and use these as velocities in the quiver function.
Grateful for any assistance.
The pracma package has a gradient function that can do this for you using the original M1$z values. For example, using your code to get M1 after set.seed(123):
contour(x=M1$x,y=M1$y,z=M1$z, asp = 1) # asp = 1 needed so things look perpendicular
library(pracma)
g <- gradient(M1$z, M1$x, M1$y)
x <- outer(M1$x, M1$y, function(x, y) x)
y <- outer(M1$x, M1$y, function(x, y) y)
quiver(x, y, g$Y, g$X, scale = 0.02, col = "blue")
Note that the gradient labels in the quiver plot have been swapped. Maybe I set up the x and y values transposed from the way the package expects. Here's what you get:
I am trying to add dashed lines parallel to x,y,z axises in scatter plot 3D. Can I can modify following code to add dashed lines:
energy <- c(274.7539889,178.8493447,206.8084623,225.2049707,234.91386,359.9859873,251.4635995,406.480808,321.8857666,250.3560571)
time <- c(25.93572586,16.8826942,19.52192798,21.25848803,22.17497005,33.98130057,23.73720217,38.37023385,30.38478544,23.6326544)
cost <- c(6.861252677,13.22453603,13.53984311,4.355987685,21.9010971,16.41429768,15.33270222,27.78572681,26.44744434,24.62564853)
library(scatterplot3d)
with(mtcars, {scatterplot3d(
energy,time,cost,
log="xyz", zlab="Cost", ylab="Time", xlab="Energy",
cex.lab=1.5,font.lab=2,lwd=3,pch=20,
highlight.3d = T, angle = 55
)})
By saving the return value from scatterplot3d you can use some functions from it to plot points and lines. For example:
p3 = scatterplot3d(
energy,time,cost,
log="xyz", zlab="Cost", ylab="Time", xlab="Energy",
highlight.3d = T, angle = 55
)
p3$points3d(x=c(300,300),y=c(25,25),z=c(0,30),type="l",lty=2)
This is documented in the help for scatterplot3d:
Value:
[...]
points3d: function which draws points or lines into the existing plot.
There's also functions for drawing planes and a generic xyz.convert function to convert any x,y,z coordinate to a x,y coordinate on the graphics device.
I would like to plot lines with colors representing the intensity of some measurment in the form of a vector. With scatter you can plot points with the color given by a color vector but in plot I would have to give an RGB vector. I would like to know how can I map my vector to an RGB vector? Or is there some other method?
Thanks a lot, and tell me if I didn't give some information
Depending of the number of lines you have to plot, one solution would be to use the built-in colormaps. Crude example:
intensityVector = [0.1 0.5 1]; % some intensities
% Data you want to plot
x=1:0.1:16;
data1 = zeros(1,length(x));
data2 = sin(x);
data3 = cos(x);
% Let's get an appropriate colormap
cm = colormap(hot);
close; % because colormap call opens a figure
% In order for the line to be differentiable, let's add an offset
offset=10;
figure()
hold on
plot(data1,'Color',cm(1*offset,:),'LineWidth',3);
plot(data2,'Color',cm(2*offset,:),'LineWidth',3);
plot(data3,'Color',cm(3*offset,:),'LineWidth',3);
legend({'data1','data2','data3'});
I want to find the total area from multiple polygons within different contour lines from kernel densities (kde2d).
Here is an image of the kernel density and the 50% contour line. How do I calculate the area within the 50% contour line?
I also created a matrix of lat lon coordinates, which represents the points within this 50% contour line. Would it be easier to calculate the total area using these points.
Any suggestions would be greatly appreciated!
Once you have your coordinates in a cartesian system, and have done the kernel smoothing using those coordinates, you can use the contourLines function to get the coordinates of the lines, and then the areapl function from the splancs package to compute the area of each simple ring.
For example, using the example in help(kde2d):
attach(geyser)
plot(duration, waiting, xlim = c(0.5,6), ylim = c(40,100))
f1 <- kde2d(duration, waiting, n = 50, lims = c(0.5, 6, 40, 100))
image(f1)
contour(f1)
so that's our data set up - suppose we want the area in the 0.008 contour:
C8 = contourLines(f1,level=0.008)
length(C8)
[1] 3
Now C8 is a list of length 3. We need to apply the areapl function over each of these:
> sapply(C8,function(ring){areapl(cbind(ring$x,ring$y))})
[1] 14.65282 12.27329 14.75005
And we can obviously sum:
> sum(sapply(C8,function(ring){areapl(cbind(ring$x,ring$y))}))
[1] 41.67617
Now this only makes sense if the coordinates are cartesian, and if the contour lines are complete loops. If the 0.008 contour was near the edge then its possible for the contour to get clipped to the bounding box and then bad things happen. Check at least that the last point of each ring is the same as the first.