I'm beginner with plotting in 3D in R and I need help. I try to plot some easy paraboloid
library(rgl)
x <- seq(-1,1, 0.2)
y <- x
f <- function(x,y){
-(x^2+y^2)
}
z <- outer(x,y, "f")
persp3d(x, y, z, col="gray")
So, my questions are:
Can I draw only grid, or make color transparent to see also the part of "at the back"?
How to add points to the plot (on the surface, e.g to draw in other color point (1,1,2))?
See ?material3d for information on surface properties. Most of these properties, such as alpha or front="line" or back="line", can be passed directly to persp3d(). Add points with points3d() (or spheres3d()).
persp3d(x, y, z, col="gray", alpha=0.5)
points3d(1,1,2,col="red")
persp3d(x, y, z, col="gray", front="line", back="line")
spheres3d(1,1,2,col="red",radius=5) ## appropriate radius: I used x <- y <- 1:20
Related
I would like to plot two planes in a 3D plot. I have tried persp3d and it generates two planes. But instead of the whole two planes, I just want to show parts of them divided by the intersection line, i.e, "left" part of the blue plane, and "upper" part of the red plane. I tried xlim, ylim, but it seems my lims are not single values, but functions.
library(rgl)
x <- seq(-10, 10, length = 30)
y <- x
region = expand.grid(x=x, y=y)
z1 = region$x+2*region$y + 2
z2=3*region$x+region$y
persp3d(x,y,z1,col="steelblue")
persp3d(x,y,z2,col="red",add=TRUE)
grid = mesh(x,y)
z = with(grid,ifelse(x+2*y>3*x+y,x+2*y,3*x+y))
persp3D(z = z, x = x, y = y,col = NULL)
for (i in 1:900){
z[i] = ifelse(region$x[i]+2*region$y[i] + 2 >
3*region$x[i]+region$y[i],region$x[i]+2*region$y[i] + 2,3*region$x[i]+region$y[i])}
persp3d(x,y,z,col="steelblue")
This is inspired by Huang Rui's suggestion
In the above image the surface colored in z axis (like heat map). I am using plot3d()
plot3d(data$x, data$y, data$z, name = 'Plotly3D graph', type = 'l', axes=F)
I have to repeat the same color as of in the image. By using above code I can get the 3D square but I dont know where to set the color of z axis as same as the image. Please help me in plot3D. If full code is needed will post if required.
Here is sample code:
data1 <- read.csv(file.choose(),1)
# retrieve age column from csv file
var1 <- data1$age
z1 <- rep(1, times=length(var1))
plot3d(var1, length(var1), z1, type="l", xaxt='n', yaxt='n', xlab="Jitter in
ns", ylab="Counts", size=0.05,expand=0.75, col=color[zcol],
ticktype="detailed", zlab="")
Here's one version, using a function from one of the answers to How do I generate a mapping from numbers to colors in R?.
# A function based on Dave X's answer to the colour mapping question
map2color <- function(x, pal, limits = range(x)){
pal[findInterval(x, seq(limits[1], limits[2], length.out = length(pal) + 1),
all.inside=TRUE)]
}
persp3d(volcano, col = map2color(volcano, rainbow(100)))
This produces this image:
To produce solid edges ("curtains" in plot3D), just surround the data with extra rows and columns of its minimum value. For example,
m <- min(volcano)
volcano2 <- cbind(m, rbind(m, volcano, m), m)
To make the edges look flat, you need to add x and y values, just a tiny bit outside the original ones:
x <- c(0.9999, 1:nrow(volcano), nrow(volcano) + 0.0001)
y <- c(0.9999, 1:ncol(volcano), ncol(volcano) + 0.0001)
persp3d(x, y, volcano2, col = map2color(volcano2, heat.colors(100)))
I switched the palette to heat.colors just for some variety.
I'm sure this is a very simple problem, but somehow I can not find the answer. So in 2D if I want to display predictions on top actual values I do something like this:
plot(x, y, type = “l”, col = “green")
lines(x`, y`, type = “l”, col = "blue")
but I can not figure out how to do this in 3d (I’m using scatterplot3d)
I manage to display actual values
s3d<-scatterplot3d(x, y, z, color = “blue”, type = “l”, …)
s3d.coords <- s3d$xyz.convert(x,y,z)
D3_coord=cbind(s3d.coords$x,s3d.coords$y)
but how do I draw a graph for predicted values on top of that?
Thank you in advance.
I'm not sure if this is what you are going for, but here is one option (notice the differing data structure as input to scatterplot3d - a vector rather than a matrix for z):
library(scatterplot3d)
n <- 10
x <- seq(-10,10,,n)
y <- seq(-10,10,,n)
grd <- expand.grid(x=x,y=y)
z <- matrix(2*grd$x^3 + 3*grd$y^2, length(x), length(y))
image(x, y, z, col=rainbow(100))
plot(x, y, type = "l", col = "green")
X <- grd$x
Y <- grd$y
Z <- 2*X^3 + 3*Y^2
s3d <- scatterplot3d(X, Y, Z, color = "blue", pch=20)
s3d.coords <- s3d$xyz.convert(X, Y, Z)
D3_coord=cbind(s3d.coords$x,s3d.coords$y)
lines(D3_coord, t="l", col=rgb(0,0,0,0.2))
I have 3 data ranges using to plot in R:
x <- c(1,2,3,4,5)
y <- c(2,4,6,8,10)
z <- c(100,240,480,580,880)
How to plot a 3D graphic with those data in R (a 3d scatterplot) ?
There are many examples of this available with a bit of searching.
Some ideas:
install.packages("scatterplot3d")
library(scatterplot3d)
s3d <-scatterplot3d(x,y,z, pch=16, highlight.3d=TRUE,
type="h", main="3D Scatterplot")
Sometimes it is nice if you can rotate it:
install.packages("rgl")
library(rgl)
plot3d(x, y, z, col="red", size=3)
If you're looking for another option, you could use the plotly package for R.
library(plotly)
x <- c(1,2,3,4,5)
y <- c(2,4,6,8,10)
z <- c(100,240,480,580,880)
plot_ly(x = x, y = y, z = z, type="scatter3d", mode="markers")
I'm a beginner in R and needs a bit of help for my scripting.
I managed to generate scale color gradient using library(ggplot2) on my 2D plots as follows;
z <- c(data$conf)
d <- qplot(x, y, xlab="Dimension 1", ylab="Dimension 2", colour=z)
d
d + scale_colour_gradient(limits=c(0, 1), data=data$conf, low="blue", high="red"))
I'm now trying to reproduce this gradient on a 3D plot, I used scatterplot3d or plot3d. I believe the colorRampPalette create a color gradient based on 327 rows (1…327) while I'm interested in a gradient that is function of values in data$conf. I need a connection, but where?
attach(data)
t1 <- c(data$conf)
jet.colors <- colorRampPalette(c("blue", "red"))
e <- plot3d(x, y, z, col=jet.colors(327))
If you can help me that will be great – Or if you know any 3D plot/scale gradient package that can do a better job, cool too.
You are on the right track with colorRampPalette(), but really need something more like colorRamp(), which 'returns a function that maps values between 0 and 1'.
Even better would be a function -- call it myColorRamp() -- which is like colorRamp() but instead: (a) maps values between min(values) and max(values); and (b) returns the colors as 7-character sRGB strings (e.g. "#F60008"), a format that plot3d() understands.
library(rgl)
myColorRamp <- function(colors, values) {
v <- (values - min(values))/diff(range(values))
x <- colorRamp(colors)(v)
rgb(x[,1], x[,2], x[,3], maxColorValue = 255)
}
x <- sin((1:100)/10)
y <- cos((1:100)/10)
z <- seq(-20, 20, length.out=100)
cols <- myColorRamp(c("red", "blue"), z)
plot3d(x = x, y = y, z = z, col = cols)