library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
data(iris)
attach(iris)
iris_plot <- plot_ly(iris,
x = Sepal.Length,
y = Sepal.Width,
z = Petal.Length,
type = "scatter3d",
mode = "markers",
size = 0.02)
iris_plot
petal_lm <- lm(Petal.Length ~ 0 + Sepal.Length + Sepal.Width,
data = iris)
library(reshape2)
#load data
petal_lm <- lm(Petal.Length ~ 0 + Sepal.Length + Sepal.Width,data = iris)
graph_reso <- 0.05
#Setup Axis
axis_x <- seq(min(iris$Sepal.Length), max(iris$Sepal.Length), by = graph_reso)
axis_y <- seq(min(iris$Sepal.Width), max(iris$Sepal.Width), by = graph_reso)
petal_lm_surface <- expand.grid(Sepal.Length = axis_x,Sepal.Width = axis_y,KEEP.OUT.ATTRS = F)
petal_lm_surface$Petal.Length <- predict.lm(petal_lm, newdata = petal_lm_surface)
petal_lm_surface <- acast(petal_lm_surface, Sepal.Width ~ Sepal.Length, value.var = "Petal.Length")
hcolors=c("orange","blue","green")[iris$Species]
iris_plot <- plot_ly(iris,
x = ~Sepal.Length,
y = ~Sepal.Width,
z = ~Petal.Length,
text = Species,
type = "scatter3d",
mode = "markers",
marker = list(color = hcolors),
size=0.02)
iris_plot <- add_trace(p = iris_plot,
z = petal_lm_surface,
x = axis_x,
y = axis_y,
type = "surface",mode = "markers",
marker = list(color = hcolors))
iris_plot
## Warning: 'surface' objects don't have these attributes: 'mode', 'marker'
## Valid attributes include:
## '_deprecated', 'autocolorscale', 'cauto', 'cmax', 'cmid', 'cmin', 'coloraxis', 'colorbar', 'colorscale', 'connectgaps', 'contours', 'customdata', 'customdatasrc', 'hidesurface', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'legendgroup', 'legendgrouptitle', 'legendrank', 'lighting', 'lightposition', 'meta', 'metasrc', 'name', 'opacity', 'opacityscale', 'reversescale', 'scene', 'showlegend', 'showscale', 'stream', 'surfacecolor', 'surfacecolorsrc', 'text', 'textsrc', 'type', 'uid', 'uirevision', 'visible', 'x', 'xcalendar', 'xhoverformat', 'xsrc', 'y', 'ycalendar', 'yhoverformat', 'ysrc', 'z', 'zcalendar', 'zhoverformat', 'zsrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'