In [1]:
library("plotly")
In [2]:
set.seed(123)
x <- rnorm(1000)
y <- rchisq(1000, df = 1, ncp = 0)
group <- sample(LETTERS[1:5], size = 1000, replace = T)
size <- sample(1:5, size = 1000, replace = T)
In [3]:
ds <- data.frame(x, y, group, size)
In [4]:
p <- plot_ly(ds, x = x, y = y, mode = "markers", group = group, size = size) %>% layout(title = "Scatter Plot")
In [5]:
embed_notebook(p)
In [6]:
library(plotly)
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
p <- qplot(carat, price, data=dsamp, colour=clarity)
embed_notebook(p)
In [9]:
# Filled Line Chart
library(plotly)
library(PerformanceAnalytics)
#Load data
data(managers)
# Convert to data.frame
managers.df <- as.data.frame(managers)
managers.df$Dates <- index(managers)
# See first few rows
head(managers.df)
# Plot
p <- plot_ly(managers.df, x = managers.df$Dates, y = managers.df$HAM1, type = "scatter", mode = "lines", name = "Manager 1", fill = "tonexty") %>%
layout(title = "Time Series plot")
embed_notebook(p)
In [11]:
# Heatmap
library(plotly)
library(mlbench)
# Get Sonar data
data(Sonar)
# Use only numeric data
rock <- as.matrix(subset(Sonar, Class == "R")[,1:59])
mine <- as.matrix(subset(Sonar, Class == "M")[,1:59])
# For rocks
p1 <- plot_ly(z = rock, type = "heatmap", showscale = F)
# For mines
p2 <- plot_ly(z = mine, type = "heatmap", name = "test") %>%
layout(title = "Mine vs Rock")
# Plot together
p3 <- subplot(p1, p2)
embed_notebook(p3)
In [12]:
library(plotly)
# initiate a 100 x 3 matrix filled with zeros
m <- matrix(numeric(300), ncol = 3)
# simulate a 3D random-walk
for (i in 2:100) m[i, ] <- m[i-1, ] + rnorm(3)
# collect everything in a data-frame
df <- setNames(
data.frame(m, seq(1, 100)),
c("x", "y", "z", "time")
)
# create the plotly
p <- plot_ly(df, x = df$x, y = df$y, z = df$z, color = time, type = "scatter3d")
embed_notebook(p)
In [13]:
library(plotly)
# Note that volcano is a numeric matrix that ships with R
plot_ly(z = volcano, type = "surface")
# 2D kernel density estimation
kd <- with(MASS::geyser, MASS::kde2d(duration, waiting, n = 50))
p <- plot_ly(x = kd$x, y = kd$y, z = kd$z) %>% add_surface()
In [ ]: