R语言 开始使用Plotly
R编程语言中的 Plotly 允许从’ggplot2’图形和一个自定义接口的JavaScript库’plotly.js’创建交互式网络图形,灵感来自于图形的语法。
安装
要在R编程中使用一个包,必须先安装该包。这项任务可以用 install.packages(“packagename”) 命令完成 。 要安装整个 plotly 软件包,请输入以下命令。
install.packages(“plotly”)
或者通过 devtools 安装最新的开发版本(在 GitHub 上)。
devtools::install_github("ropensci/plotly")
重要功能
- plot_ly: 基本上是启动一个 plotly 的可视化。这个函数将 R 对象映射到 plotly.js,一个基于网络的交互式图表库(MIT 许可)。它提供了做普通事情的抽象,并设置了一些不同的默认值,使界面感觉更像 “R”(即,更接近 plot() 和 ggplot2::qplot() )。
语法
plot_ly(data = data.frame(), ..., type = NULL, name, color, colors = NULL, alpha = NULL, stroke, strokes = NULL, alpha_stroke = 1, size, sizes = c(10, 00), span, spans = c(1, 20), symbol, symbols = NULL, linetype, linetypes = NULL, split, frame, width = NULL, height = NULL, source = "A")
例子
# import plotly library
library(plotly)
# create plotly visualisation
p <- plot_ly(iris, x = ~Sepal.Width,
y = ~Sepal.Length)
# adding markers
add_markers(p, color = ~Petal.Length,
size = ~Petal.Length)
add_markers(p, color = ~Species)
输出
- plotly_build: 这个通用函数创建列表对象发送到 plotly.js 进行渲染。使用这个函数可以在覆盖默认值或调试渲染错误时发挥作用。
语法: plotly_build(p, registerFrames = TRUE)
例子
# import plotly library
library(plotly)
# create plotly visualisation
p <- plot_ly(iris, x = ~Sepal.Width,
y = ~Sepal.Length)
# structure of plotly using
# plotly_build
str(plotly_bulid(p, registerFrames = TRUE))
输出
No scatter mode specified:
Setting the mode to markers
Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
List of 8
x :List of 10
.. visdat :List of 1
.. ..29f4345ac49:function ()
.. cur_data : chr “29f4345ac49”
..attrs :List of 1
.. .. 29f4345ac49:List of 5
.. .. ..x :Class ‘formula’ language ~Sepal.Width
.. .. .. .. ..- attr(*, “.Environment”)=<environment: R_GlobalEnv>
.. .. .. y :Class ‘formula’ language ~Sepal.Length
.. .. .. .. ..- attr(*, “.Environment”)=<environment: R_GlobalEnv>
.. .. ..alpha_stroke: num 1
.. .. .. sizes : num [1:2] 10 100
.. .. ..$ spans : num [1:2] 1 20
……..
- layout: 修改 plotly 视觉化的布局
语法: layout(p, …, data = NULL)
例子
# import plotly library
library(plotly)
# create plotly visualisation
p <- plot_ly(iris, x = ~Sepal.Width,
y = ~Sepal.Length)
layout(p, data = NULL)
输出
- add_trace: 在plotly可视化中添加轨迹。
语法: add_trace(p, …, data = NULL, inherit = TRUE)
例子
# import plotly library
library(plotly)
# create plotly visualisation
p <- plot_ly(iris, x = ~Sepal.Width,
y = ~Sepal.Length)
# adding trace (lines) to plotly
# visualisation
add_trace(p, type = "scatter",
mode = "markers+lines")
输出
- animation_opts: 提供动画配置选项。动画可以通过使用plot_ly()中的帧参数或ggplotly()中的帧ggplot2美学来创建。默认情况下,动画会填充一个播放按钮和滑块组件,用于控制动画的状态(要暂停一个动画,请点击滑块上的相关位置)。播放按钮和滑块组件都是根据animation_opts()指定的规则在帧之间转换。
语法
animation_opts(p, frame = 500, transition = frame, easing = “linear”, redraw = TRUE, mode = “immediate”)
animation_slider(p, hide = FALSE, …)
animation_button(p, …, label)
例子
# import plotly library
library(plotly)
plot_ly(mtcars, x = ~wt, y = ~mpg,
frame = ~cyl) %>%
animation_opts(transition = 0)
输出
- add_data: 将数据添加到plotly可视化中。
语法: add_data(p, data = NULL)
例子
# import plotly library
library(plotly)
plot_ly() %>% add_data(economics) %>%
add_trace(x = ~date, y = ~pce)
输出
- plotly_IMAGE: 为plotly可视化创建一个静态图像。images 端点将一个绘图(可能以多种形式给出)变成所需格式的图像。
语法
plotly_IMAGE(x, width = 1000, height = 500, format = “png”, scale = 1, out_file, …)
例子
# import plotly library
library(plotly)
# create plotly visualisation
p <- plot_ly(iris, x = ~Sepal.Width,
y = ~Sepal.Length)
# importing plotly visualisation
# as image files
Png <- plotly_IMAGE(p,
out_file = "plotly-test-image.png")
Jpeg <- plotly_IMAGE(p, format = "jpeg",
out_file = "plotly-test-image.jpeg")
# importing plotly visualisation
# as vector graphics
Svg <- plotly_IMAGE(p, format = "svg",
out_file = "plotly-test-image.svg")
# importing plotly visualisation as
# pdf file
Pdf <- plotly_IMAGE(p, format = "pdf",
out_file = "plotly-test-image.pdf")
输出
- plotly_empty: 创建一个完整的空 plotly 图。当与 subplot() 一起使用时,它是一个非常有用的函数。
语法: plotly_empty(…)
例子
# import plotly library
library(plotly)
plotly_example(iris)
输出
- plotly_example: 它 运行一个(些)plotly例子。为运行demo、shiny应用程序和Rmd文件提供一个统一的接口,这些都是与软件包捆绑的。
语法: plotly_example(type = c(“demo”, “shiny”, “rmd”), name, edit = TRUE, …)
例子
# import plotly library
library(plotly)
plotly_example(type = c("demo", "shiny", "rmd"),
name, edit = TRUE, ...)
输出
我们可以利用plotly R包来创建各种互动图形。创建plotly对象的两种主要方式:一是将ggplot2对象(通过ggplotly())转换为plotly对象,二是直接用plot_ly()/plot_geo()/plot_mapbox()初始化一个plotly对象。这两种方法在某种程度上有互补的优势和劣势,所以学习这两种方法会有收获。