In [1]:
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


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")


Warning message in plot_ly(ds, x = x, y = y, mode = "markers", group = group, size = size):
“The group argument has been deprecated. Use `group_by()` or split instead.
See `help('plotly_data')` for examples”

In [5]:
embed_notebook(p)


No trace type specified:
  Based on info supplied, a 'scatter' trace seems appropriate.
  Read more about this trace type -> https://plot.ly/r/reference/#scatter

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)


HAM1HAM2HAM3HAM4HAM5HAM6EDHEC LS EQSP500 TRUS 10Y TRUS 3m TRDates
1996-01-31 0.0074 NA 0.0349 0.0222 NA NA NA 0.0340 0.00380 0.00456 1996-01-31
1996-02-29 0.0193 NA 0.0351 0.0195 NA NA NA 0.0093 -0.03532 0.00398 1996-02-29
1996-03-31 0.0155 NA 0.0258 -0.0098 NA NA NA 0.0096 -0.01057 0.00371 1996-03-31
1996-04-30-0.0091 NA 0.0449 0.0236 NA NA NA 0.0147 -0.01739 0.00428 1996-04-30
1996-05-31 0.0076 NA 0.0353 0.0028 NA NA NA 0.0258 -0.00543 0.00443 1996-05-31
1996-06-30-0.0039 NA -0.0303 -0.0019 NA NA NA 0.0038 0.01507 0.00412 1996-06-30

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)


No scatter3d mode specifed:
  Setting the mode to markers
  Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode

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()


Error in with(geyser, MASS::kde2d(duration, waiting, n = 50)): object 'geyser' not found
Traceback:

1. with(geyser, MASS::kde2d(duration, waiting, n = 50))

In [ ]: