I´m a R newbie and currently try to plot my data in a 3d chart with a surface. I tried using the package plotly, but fail to do it.
I´ve got a matrix d3 (see below), but in the plot (see link below), either z or x are equal to the data in the matrix. In the plot x is always equal 1 and z is a counting number.
What do I need to change in the code?
Is it maybe how I created the matrix?
d3 <- data.matrix(mydataframe[1:3])
View(d3)
z y x
[1,] 75.33333 1566.6667 0.2727273
[2,] 76.66667 1585.6667 0.2788845
[3,] 75.33333 1462.6667 0.2384615
[4,] 77.66667 1340.6667 0.2489796
[5,] 75.66667 1328.3333 0.3658537
[6,] 83.33333 1315.6667 0.3459716
[...] ...
plot_ly(z = ~d3) %>% add_surface()
Here is an example of my failed Plotly 3d plot
If someone has an idea on how to plot the 3 variables with a surface differently to the package plotly, I really would appreciated it.
This is an example of a 3d regression, I would like to present my data in the same colours
Cheers.
z
should be a matrix. You can use outer
, as follows:
x <- seq(0, 8, length.out=100)
y <- seq(0, 3, length.out=30)
z <- t(outer(x, y, FUN=function(x,y) 5/3*y + 4*exp(-(x-4)^2)))
library(plotly)
plot_ly(x=x, y=y, z=z) %>%
add_surface() %>%
layout(
scene = list(
xaxis= list(title="X"),
yaxis= list(title="Y"),
aspectmode = 'manual',
aspectratio = list(x=1, y=1, z=.5)
)
)