How can I plot 3D function in r?
You can also use the Lattice wireframe
function. (Using @user1020027's data)
fdejong <- function (x, y) {
return (x^2 + y^2)
}
x <- seq(-10, 10, length= 30)
y <- x
z <- outer(x, y, fdejong)
z[is.na(z)] <- 1
require(lattice)
wireframe(z, drape=T, col.regions=rainbow(100))
How to rotate 3D Plotly in R, update?
R plotly 4.10.0 recently updated the underlying plotly.js library from v1.57.1 to v2.5.1. This includes many breaking changes - With version 2.0 of plotly.js the function Plotly.plot was dropped.
To get back the old behaviour Plotly.plot
can be replaced by Plotly.update
:
library(shiny)
library(plotly)
library(htmlwidgets)
ui <- fluidPage(
plotlyOutput("graph")
)
server <- function(input, output, session) {
N <- 100
x <- rnorm(N, mean = 50, sd = 2.3)
y <- runif(N, min = 0, max = 100)
z <- runif(N, min = 4, max = 70)
luci.frame <- data.frame(x, y, z)
output$graph <- renderPlotly({
plot_ly(
type = "scatter3d",
mode = "markers",
data = luci.frame,
x = ~ x,
y = ~ y,
z = ~ z
) %>%
layout(scene = list(camera = list(
eye = list(
x = 1.25,
y = 1.25,
z = 1.25
),
center = list(x = 0,
y = 0,
z = 0)
))) %>%
onRender("
function(el, x){
var id = el.getAttribute('id');
var gd = document.getElementById(id);
Plotly.update(id).then(attach);
function attach() {
var cnt = 0;
function run() {
rotate('scene', Math.PI / 180);
requestAnimationFrame(run);
}
run();
function rotate(id, angle) {
var eye0 = gd.layout[id].camera.eye
var rtz = xyz2rtz(eye0);
rtz.t += angle;
var eye1 = rtz2xyz(rtz);
Plotly.relayout(gd, id + '.camera.eye', eye1)
}
function xyz2rtz(xyz) {
return {
r: Math.sqrt(xyz.x * xyz.x + xyz.y * xyz.y),
t: Math.atan2(xyz.y, xyz.x),
z: xyz.z
};
}
function rtz2xyz(rtz) {
return {
x: rtz.r * Math.cos(rtz.t),
y: rtz.r * Math.sin(rtz.t),
z: rtz.z
};
}
};
}
")
})
}
shinyApp(ui, server)
Get grid on the surface of a persp3d chart in R
You can call surface3d
two times after you first call persp3d
to add additional elements to the plot:
library(rgl)
x <- seq(-pi, pi, len = 20)
y <- seq(-pi, pi, len = 20)
z <- outer(x, y, function(x, y) sin(sqrt(x^2 + y^2)))
persp3d(x, y, z, col = "blue")
surface3d(x, y, z, back = "lines")
surface3d(x, y, z, front = "lines")
plot a 3 axis graph as a mesh
If your x & y points topologically lie on a grid, then you can use MESH. They don't need to have even spacing; they just need to be organized so that x(r:r+1,c:c+1) and y(r:r+1,c:c+1) define a quadrilateral on your mesh, for each row r and column c.
If your data do not lie on a grid, but you know what the faces should be, look at the PATCH function.
If you only have points and you don't know anything about the surface, you need to first solve the surface reconstruction problem. I've used cocone; there are other good packages there too. Once you have the reconstructed surface, then you can use PATCH to display it.
Related Topics
How to Access Global/Outer Scope Variable from R Apply Function
Create Barplot from Data.Frame
Find Consecutive Values in Vector in R
Element-Wise Concatenation of String Vectors
Rcpp Function to Select (And to Return) a Sub-Dataframe
Dplyr Rowwise Sum and Other Functions Like Max
Twitter Data Analysis - Error in Term Document Matrix
Difference Between 'Names(Df[1]) <- ' and 'Names(Df)[1] <- '
Partially Read Really Large CSV.Gz in R Using Vroom
Knit One Markdown File to Two Output Files
Change the Number of Breaks Using Facet_Grid in Ggplot2
Number Format, Writing 1E-5 Instead of 0.00001
Export Fitted Regression Splines (Constructed by 'Bs' or 'Ns') as Piecewise Polynomials
Get Margin Line Locations in Log Space
Dictionary() Is Not Supported Anymore in Tm Package. How to Emend Code