#install.packages("plotly") # interactive web graphics
library(plotly) # share your visualization https://plot.ly/
## Warning: package 'plotly' was built under R version 3.3.2
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.3.2
##
## 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
Basic Scatter plot:
data("mtcars")
plot_ly(data=mtcars, x=~wt, y=~mpg, mode="markers")
## 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
scatter plot with colors (factors):
mtcars$cyl = as.factor(mtcars$cyl)
plot_ly(data=mtcars, x=~wt, y=~mpg, mode="markers", color = ~cyl)
## 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
scatter plot with colors (continuous):
plot_ly(data=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
scatter plot with size:
plot_ly(data=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
3D scatterplot:
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)
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
## A line object has been specified, but lines is not in the mode
## Adding lines to the mode...
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
## A line object has been specified, but lines is not in the mode
## Adding lines to the mode...
## A line object has been specified, but lines is not in the mode
## Adding lines to the mode...
## A line object has been specified, but lines is not in the mode
## Adding lines to the mode...
## A line object has been specified, but lines is not in the mode
## Adding lines to the mode...
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', 'hoverinfo', 'stream', 'x', 'y', 'text', 'orientation', 'histfunc', 'histnorm', 'autobinx', 'nbinsx', 'xbins', 'autobiny', 'nbinsy', 'ybins', 'marker', 'error_y', 'error_x', '_deprecated', 'xaxis', 'yaxis', 'xsrc', 'ysrc', 'textsrc', 'key'
Box-plot:
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