当前位置: 首页 > article >正文

【R语言编程绘图-plotly】

安装与加载

在R中使用plotly库前需要安装并加载。安装可以通过CRAN进行,使用install.packages()函数。加载库使用library()函数。

install.packages("plotly")
library(plotly)

测试库文件安装情况

# 安装并加载必要的包
if (!requireNamespace("plotly", quietly = TRUE)) {install.packages("plotly")
}
library(plotly)

基本绘图语法

plotly的基本语法围绕plot_ly()函数展开。数据通过参数传递,图形类型由add_trace()或直接指定图形类型(如type = "scatter")决定。

# 使用plot_ly创建散点图
plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, type = "scatter", mode = "markers")

图形类型与参数

plotly支持多种图形类型,包括散点图、线图、柱状图等。通过调整typemode参数可以定义图形样式。

# 线图示例
plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, type = "scatter", mode = "lines")# 柱状图示例
plot_ly(data = iris, x = ~Species, y = ~Sepal.Length, type = "bar")

自定义布局

使用layout()函数可以自定义图形的标题、轴标签、图例等属性。

plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, type = "scatter", mode = "markers") %>%layout(title = "Iris Dataset Scatter Plot",xaxis = list(title = "Sepal Length"),yaxis = list(title = "Petal Length"))

添加多个图形层

通过add_trace()函数可以在同一图形上叠加多个图形层。

plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, type = "scatter", mode = "markers") %>%add_trace(y = ~Petal.Width, mode = "lines")

交互功能

plotly图形默认支持交互功能,如缩放、平移、悬停显示数据等。可以通过layout()中的hovermode等参数调整交互行为。

plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, type = "scatter", mode = "markers") %>%layout(hovermode = "closest")

导出图形

plotly图形可以导出为HTML文件或静态图片,使用htmlwidgets::saveWidget()plotly::export()函数。

p <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, type = "scatter", mode = "markers")
htmlwidgets::saveWidget(p, "plot.html")

以上内容涵盖了plotly库的基本语法规则和常见操作,适用于快速上手和进阶使用。通过组合不同的函数和参数,可以创建丰富多样的交互式图形。

# 安装并加载必要的包
if (!requireNamespace("plotly", quietly = TRUE)) {install.packages("plotly")
}
library(plotly)# 示例数据
data <- data.frame(Group = c("A", "B", "C", "D", "E"),Mean = c(20, 25, 30, 35, 40),SD = c(2, 3, 2.5, 3.5, 4)
)# 创建柱状图
fig <- plot_ly(data, x = ~Group, y = ~Mean, type = 'bar', name = 'Mean',marker = list(color = 'skyblue', line = list(color = 'black', width = 1.5)))# 添加误差线
fig <- fig %>% add_trace(x = ~Group, y = ~Mean, error_y = list(type = 'data', array = ~SD, visible = TRUE,color = 'black', thickness = 2, width = 10),type = 'scatter', mode = 'markers', name = 'Error Bars',marker = list(size = 10, color = 'black')
)# 设置布局
fig <- fig %>% layout(title = 'Bar Plot with Standard Deviation',xaxis = list(title = 'Group'),yaxis = list(title = 'Mean ± SD'),barmode = 'overlay',bargap = 0.2,bargroupgap = 0.1,showlegend = FALSE
)# 显示图形
fig

在这里插入图片描述

http://www.lryc.cn/news/2402362.html

相关文章:

  • float、double 这类 浮点数 相比,DECIMAL 是另一种完全不同的数值类型
  • 通信刚需,AI联手ethernet/ip转profinet网关打通工业技术难关
  • JavaEE->多线程:定时器
  • 6个月Python学习计划 Day 15 - 函数式编程、高阶函数、生成器/迭代器
  • <el-table>构建树形结构
  • linux——磁盘和文件系统管理
  • 云原生 DevOps 实践路线:构建敏捷、高效、可观测的交付体系
  • gateway 网关 路由新增 (已亲测)
  • ArcGIS Pro 3.4 二次开发 - 共享
  • Python html 库用法详解
  • C#异常处理进阶:精准获取错误行号的通用方案
  • 如何快速找出某表的重复记录 - 数据库专家面试指南
  • Python 训练营打卡 Day 33-神经网络
  • resolvers: [ElementPlusResolver()] 有什么用?
  • XHR / Fetch / Axios 请求的取消请求与请求重试
  • 机器学习-ROC曲线​​ 和 ​​AUC指标
  • Spring Boot缓存组件Ehcache、Caffeine、Redis、Hazelcast
  • 【学习记录】深入解析 AI 交互中的五大核心概念:Prompt、Agent、MCP、Function Calling 与 Tools
  • 如何有效删除 iPhone 上的所有内容?
  • AI大模型学习三十二、飞桨AI studio 部署 免费Qwen3-235B与Qwen3-32B,并导入dify应用
  • 操作系统中的设备管理,Linux下的I/O
  • 炉石传说 第八次CCF-CSP计算机软件能力认证
  • AI应用工程师面试
  • LabVIEW与Modbus/TCP温湿度监控系统
  • Cursor 1.0 版本 GitHub MCP 全面指南:从安装到工作流增强
  • 自主设计一个DDS信号发生器
  • 鸿蒙UI(ArkUI-方舟UI框架)- 使用弹框
  • 学习笔记(24): 机器学习之数据预处理Pandas和转换成张量格式[2]
  • 在不同型号的手机或平板上后台运行Aidlux
  • 【SSM】SpringBoot学习笔记1:SpringBoot快速入门