Basic Scatterplot

Scatterplot Color

Continuous Color

You can also show continuous variables with color in scatterplots

plot_ly(mtcars, x=~wt, y=~mpg, mode="markers", color=~disp)
## 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

Scatterplot Sizing

plot_ly(mtcars, x=~wt, y=~mpg, mode="markers", color=~cyl, size=~hp)
## 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
## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

3D Scatterplot

You can create a 3D scatterplot with the ‘type = “scatter3d”’ argument. If you click and drag these scatterplots you can view them from different angles.

set.seed(2016-07-21)
temp <- rnorm(100, mean=30, sd=5)
pressure <- rnorm(100)
dtime <- 1:100
plot_ly(x=~temp, y=~pressure, z=~dtime, type="scatter3d", mode="markers", color=~temp)

Line Graph

Line graphs are the default graph for ‘plot_ly()’. They’re of course useful for showing change over time.

data("airmiles")
plot_ly(x=time(airmiles),y=airmiles, mode="line")
## 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

Multi Line Graph

You can show multiple lines by specifying the column in the data frame that separates the lines:

library(plotly)
library(tidyr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
data("EuStockMarkets")

stocks <- as.data.frame(EuStockMarkets) %>%
  gather(index, price) %>%
  mutate(time = rep(time(EuStockMarkets), 4))

plot_ly(stocks, x=~time, y=~price, color=~index, mode="line")
## 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

Histogram

plot_ly(mtcars, x=~qsec, type="histogram", breaks=10)
## Warning: 'histogram' objects don't have these attributes: 'breaks'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'uirevision', 'x', 'y', 'text', 'hovertext', 'orientation', 'histfunc', 'histnorm', 'cumulative', 'nbinsx', 'xbins', 'nbinsy', 'ybins', 'autobinx', 'autobiny', 'hovertemplate', 'marker', 'offsetgroup', 'alignmentgroup', 'selected', 'unselected', '_deprecated', 'error_x', 'error_y', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'hoverinfosrc', 'xsrc', 'ysrc', 'textsrc', 'hovertextsrc', 'hovertemplatesrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

Boxplot

plot_ly(mtcars, y=~qsec, color=~cyl, type="box")

Heat map

mat = matrix(rnorm(100*100), nrow=100, ncol=100)
mat[1:50,1:25] = mat[1:50,1:25]+2

plot_ly(z=~mat, type="heatmap")

3D Surface

mat = matrix(rnorm(100*100), nrow=100, ncol=100)
mat[1:50,1:25] = mat[1:50,1:25]+2

plot_ly(z=mat, type="surface")

3D Surface with Smoothing

mat = matrix(sort(rnorm(100*100)), nrow=100, ncol=100)

plot_ly(z=mat, type="surface")

ggplotly

g = ggplot(data=mtcars, aes(x=mpg, y=qsec, color=cyl)) + geom_point()
g

p = ggplotly(g)
p