simply_scatter: Dynamic 3D Scatterplot

View source: R/simply_scatter.R

simply_scatterR Documentation

Dynamic 3D Scatterplot

Description

Dynamic 3D Scatterplot

Usage

simply_scatter(
  x,
  y,
  z,
  size = 5,
  color = "#000000",
  opacity = 0.9,
  xlab = "x",
  ylab = "y",
  zlab = "z",
  showscale = FALSE,
  showlegend = FALSE
)

Arguments

x

x-variable

y

y-variable

z

z-variable

size

Pointsize

color

Pointcolor (either a single color or a vector the same length of the data)

opacity

Opacity of the points

xlab

x-axis label

ylab

y-axis label

zlab

z-axis label

showscale

Option for showing or hiding the colorscale

showlegend

Option for showing or hiding the legend

Value

Returns a interactive 3D scatter plot

Examples


## Example 1: Simple scatterplot

simply_scatter(mtcars$hp, mtcars$cyl, mtcars$mpg, xlab = "hp", ylab = "cyl", zlab = "mpg")


## Example 2: Coloring data points differently according to a color variable

colorvariable = rep("cornflowerblue", nrow(mtcars))
colorvariable[which(mtcars$vs == 1)] = "orange"

simply_scatter(mtcars$hp, mtcars$cyl, mtcars$mpg,
  color = colorvariable, xlab = "hp", ylab = "cyl", zlab = "mpg")


## Example 3: Adding a surface to a regression plot (e.g. to plot a linear regression)

# generating data
x = rnorm(1000, 10, 20)
y = rnorm(1000, 20, 20)
z = 4 + 0.3*x + 0.2*y + rnorm(1000, 0, 4)

# plotting scatterplot with simply_scatter
scatter = simply_scatter(x,y,z)

# fitting a linear regression model
mod = lm(z ~ x + y)

# adding model plane into scatterplot
surface(fig = scatter, expr = coef(mod)[1] + coef(mod)[2]*x + coef(mod)[3]*y, x = x, y = y)

# you can also use the pipe operator
require(dplyr)
simply_scatter(x,y,z) %>%
  surface(expr = coef(mod)[1] + coef(mod)[2]*x + coef(mod)[3]*y, x = x, y = y)

# for more complex models defining an expression as above might be tedious.
# You can also just define your model prediction as a function
f = function(x,y){
predict(mod, newdata = data.frame(x,y))
}

surface(fig = scatter, expr = f, x = x, y = y)

janoleko/simply3d documentation built on March 1, 2023, 2:58 a.m.