R: 3D surface plot (persp3d) with fixed coordinates like in Excel - r

I have my R data in x, y, and z format, x and y being vectors of length 10 and 19 respectively and z being a matrix of 10x19 integers. I have the following code that prints my 3D surface:
height <- (z - range(z)[1]) / diff(range(z))
r.prop <- height
g.prop <- 0
b.prop <- 1 - height
color <- rgb(r.prop, g.prop, b.prop, maxColorValue=1)
persp3d(x, y, z, theta=50, phi=25, expand=0.75, col=color, ticktype="detailed",
xlab="filesize [kb]", ylab="record size [kb]", zlab="speed [b/s]",
axes=TRUE)
My problem is that the y axis contains powers of 2 (4, 8, 16, 32, 64, ..., 2048,..), which squeezes most of my data points into the low-range part of the plot. What I want is an even distribution of the data points, i.e. between point 4 and 8 should be the same space as between 1024 and 2048 just like it is in the Excel spreadsheet surface plot.
Can you tell me which command or parameter would make that happen with persp3d?
Thanks a bunch!
Loddi
Addition:
After changing the the axes to log2, or just doing a c(1:length(x)) and c(1:length(y)) instead of the actual axis values, it looks better but now has these weird jigsaw shape that is not at all represented in my data. Do you guys have a clue what is going on here?
see picture here http://i.stack.imgur.com/WBI3r.png

Try log2(y) instead of y
persp3d(x, log2(y), z, theta=50, phi=25, expand=0.75, col=color, ticktype="detailed",
xlab="filesize [kb]", ylab="record size [kb]", zlab="speed [b/s]", axes=FALSE,
normal_x=x, normal_y=y, normal_z=z)

Related

How do i plot a XY plot and XZ plot in a 3D plot?

I'm trying to plot two graph. One XY and one XZ in a 3D graph.
So far i only got this, and my issue is that plot 2 uses my second variable as a y coordinate and i don't know how to make it a Z. I've tried placing a 800 as constant Y, but octave doesn't seem to agree.
clear;
[amp, knak, cap]=textread('Octave.csv');
figure(1);
plot3(amp, knak); %%plot 1
hold on;
plot3(amp, cap); %%plot 2
xlabel('amplitude');
ylabel('knakfrekvens');
xlim([0, 25]);
ylim([800, 1500])
zlim([-2E-06, 0]);
zlabel('capacitet');
legend('capacitet', 'knakfrekvens');
grid on;
hold off;
Making a Z is right direction. However, X and Y are matrix and Z have to be matrix.
Function plot3 takes three cordinates when it tries to plot a line in 3D. If it takes only two cordinates, it will take the cordinates as x and y and plot it on xy plane. You may specify one more cordinate to make plot3 plot the line on the plane you want.
There is an example:
t = 0:0.1:2*pi;
y = sin(t);
z = sin(t+(0.5*pi));
figure(1);
#on -z=1 plane
plot3(t, y, -1*ones(1, length(t)), 'linewidth', 5, "-");
hold("on");
#on y=1 plane
plot3(t, ones(1, length(t)), z, 'linewidth', 5, "-"); %%plot 2
xlabel('X');
ylabel('Y');
zlabel('Z');
grid("on");
hold("off");

Fill under line curve

For the sample dataset below, I would like to just plot the y as a smooth line with fill under the line using R.
I am able to get smooth line but not the color filled curve. Could someone please help me here?
date, y
2015-03-11, 71.12
2015-03-10, 34.0
2015-03-09, 11.1
2015-03-08, 9.62
2015-03-07, 25.97
2015-03-06, 49.7
2015-03-05, 38.05
2015-03-04, 38.05
2015-03-03, 29.75
2015-03-02, 35.85
2015-03-01, 30.65
The code I used to plot the smooth line is as follows. I am unable to get fill the portion under the line with a color
y <- df$y
x <- 1:length(y)
plot(x, y, type='n')
smooth = smooth.spline(x, y, spar=0.5)
lines(smooth)
EDIT
Using the polygon function does not give what is expected. The shaded area should be below the line not above
with(smooth, polygon(x, y, col="gray"))
Describe a polygon by listing its boundary vertices in order as you march around it.
This polygon's boundary consists of the curve plus two more vertices at the bottom right and bottom left. To help you see them, I have overplotted the vertices, varying their colors by position in the sequence.
Here is the R code that did it. It used predict to obtain coordinates of the curve from the spline object, then adjoined the x- and y-coordinates of the two extra points using the concatenation operator c. To make the filling go to the axis, the plot range was manually set.
y <- c(71, 34, 11, 9.6, 26, 50, 38, 38, 30, 36, 31)
n <- length(y)
x <- 1:n
s = smooth.spline(x, y, spar=0.5)
xy <- predict(s, seq(min(x), max(x), by=1)) # Some vertices on the curve
m <- length(xy$x)
x.poly <- c(xy$x, xy$x[m], xy$x[1]) # Adjoin two x-coordinates
y.poly <- c(xy$y, 0, 0) # .. and the corresponding y-coordinates
plot(range(x), c(0, max(y)), type='n', xlab="X", ylab="Y")
polygon(x.poly, y.poly, col=gray(0.95), border=NA) # Show the polygon fill only
lines(s)
points(x.poly, y.poly, pch=16, col=rainbow(length(x.poly))) # (Optional)

Inputting a fixed colour input vector with RGL in R

Is there any way to input a fixed vector of colours to any 3D rgl plots? If so it would be possible to extrude a map tile to a 3D surface based on a raster of the same area. But I'm finding the surface3d function behaves the same as raster::plot by insisting on mapping the input colour vector to the z variable. Is this beyond rgl's functionality at present?
I don't actually know if what you say about the coloring is correct for all rgl coloring functions, but it is not correct for rgl.surface(). This is a corruption of the example on the ?rgl.surface page. The color vector index was formed from the x-y (actually x-z) coordinates and gives a striping effect because they were modulo-ized to pull values from from a limited range.
library(rgl)
data(volcano)
y <- 2 * volcano
x <- 10 * (1:nrow(y))
z <- 10 * (1:ncol(y))
ylim <- range(y)
ylen <- ylim[2] - ylim[1] + 1
colorlut <- terrain.colors(ylen)
col <- colorlut[(x+length(x)*y +1)%%ylen ]
rgl.open()
rgl.surface(x, z, y, color=col, back="lines")
rgl.snapshot("striped_volcano.png")

How to specify z axis range and add add circle or ellipse in 3D plot in R

3-D graphing with Google(http://www.r-bloggers.com/3-d-graphing-with-google/)
(mu1=0 mu2=0 sigma1=1 sigma2=1 pho=0)
exp((-1/2)*(x^2+y^2))/(2*pi) from -3 to 3
The rotate plot will be showd from google. The profile was a circle.
Dear Prof. Bolker gave me the R code:
library("emdbook")
library("rgl")
curve3d(dmvnorm(c(x,y),mu=c(0,0),Sigma=diag(2)),
sys3d="rgl",front="line",back="line",
xlim=c(-3,3),ylim=c(-3,3))
How to specify z axis range and get the plot like google's plot?
If pho=0 then the profile parallel to XY plane was circle.If pho<>0 then the profile parallel to XY plane was ellipse. How to add circle or ellipse in 3D plot? Thanks.
I am not sure that I fully understand your question but:
1/ I do not think rgl allow to specify z axis range (and curve3d seems to allow it only for xlim, ylim) so you probably need to do it by hand
2/ You can rescale axis in rgl using rgl.viewpoint : e.g., rgl.viewpoint(scale=c(1,1,0.1))
3/ You can draw circle or ellipse using:
t <- matrix(seq(-pi/2,pi/2, len=50), 50, 50, byrow=TRUE)
p <- matrix(seq(-pi, pi, len=50), 50, 50)
r <- 10
x <- r*cos(t)*cos(p)
y <- r*cos(t)*sin(p)
z <- r*sin(t)
persp3d(x, y, z)

Using scatterplot.density with x-y coordinates causes error in image.default

I intend to plot x-y coordinates of a traced object with scatterplot.density to use colour to show the density of points in the scatterplot instead of just displaying the points in a smooth scatterplot (smoothScatter).
When I run the following short segment of the trace:
library(aqfig)
x <- c(69.8, 69.8, 70.07, 70.87, 70.87,72.48,73.02, 73.02, 74.36, 74.63)
y <- c(97.99,97.45,96.91,96.11,96.91,96.91,97.72,99.06,100.94,103.36)
par(mfrow = c(1, 1))
scatterplot.density(x, y)
I get the following error:
Error in image.default(x = 1, y = z, z = matrix(z, nrow = 1, ncol = length(col)), : increasing 'x' and 'y' values expected
I can blot the points in a smoothScatter without a problem, but this would not help once I add the full trace with 1500 x-y coordinates.
Any suggestions and help with the scatterplot.density problem would be highly appreciated!
Thanks!
What I've found is that if the argument num.bins is high enough to seperate each point into a single bin then you will recieve the error. Try
scatterplot.density(x, y, num.bins=11)
vs.
scatterplot.density(x, y, num.bins=10)
the default is 64. That means 64 bins in the x axis and y axis. You either need to be lower you're bin number, add more points, or be content with the error. Hope that helps.

Resources