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

echarts4r 教程1:Get Started

写在前面

本文为 R 语言 echarts4r 包的学习笔记。本着自己学习、分享他人的态度,分享学习笔记,希望能对大家有所帮助。软件可能随时更新,建议配合官方文档一起阅读。


目录

  • 1 Video & Article
  • 2 Your first plot
  • 3 Options
  • 4 Navigate options

官网教程:https://echarts4r.john-coene.com/articles/get_started

欢迎来到 echarts4r,让我们一起来探索一下这个包吧。

  1. echarts4r 包的所有函数都以 e_ 开头。
  2. 它所有的 Shiny 代理都以 _p 结尾。
  3. 所有 echarts4r 绘图均使用 e_charts 初始化。
  4. 所有函数都是 |> 友好的。
  5. 大多数函数都有以 _ 结尾的 escape hatches。

1 Video & Article

感谢 Sharon Machlis,提供了一个介绍 echarts4r 的精彩视频和文章。

视频:https://youtu.be/OBJxIWEFHdo

文章:https://www.infoworld.com/article/3607068/plot-in-r-with-echarts4r.html

2 Your first plot

让我们构建一个折线图,加载库并将数据通过管道传输到 e_charts。如果您对 |> 不满意,可以使用 e_charts(mtcars, wt)

# prepare data
df <- state.x77 |> 
  as.data.frame() |> 
  tibble::rownames_to_column("State")

library(echarts4r) # load echarts4r

df |> 
  e_charts(x = State) |> # initialise and set x
  e_line(serie = Population) # add a line
alt

如果您对裸列名称不满意,可以使用以 _ 结尾的 escape hatches。

df |> 
  e_charts_("State") |> 
  e_line_("Population")

最简单的方法是使用 |> 运算符添加绘图和选项。

df |> 
  e_charts(State) |> # initialise and set x
  e_line(Population) |>  # add a line
  e_area(Income) # add area
alt

3 Options

我们还可以改变线条,使它们变得 smooth

df |> 
  e_charts(State) |> # initialise and set x
  e_line(Population, smooth = TRUE) |>  # add a line
  e_area(Income, smooth = TRUE# add area
alt

让我们使用便捷函数 e_axis_labels 来标记轴。

df |> 
  e_charts(State) |> # initialise and set x
  e_line(Population, smooth = TRUE) |>  # add a line
  e_area(Income, smooth = TRUE) |>  # add area
  e_axis_labels(x = "State"# axis labels
alt

我们可以使用 13 个内置主题之一,请参阅 ?e_theme 获取完整列表,我们还将使用 e_title 添加标题。

df |> 
  e_charts(State) |> # initialise and set x
  e_line(Population, smooth = TRUE) |>  # add a line
  e_area(Income, smooth = TRUE) |>  # add area
  e_axis_labels(x = "State") |> # axis labels
  e_title("US States""Population & Income") |>  # Add title & subtitle
  e_theme("infographic"# theme
alt

图例和标题有点接近,让我们将图例移到画布的另一部分。

df |> 
  e_charts(State) |> # initialise and set x
  e_line(Population, smooth = TRUE) |>  # add a line
  e_area(Income, smooth = TRUE) |>  # add area
  e_axis_labels(x = "State") |> # axis labels
  e_title("US States""Population & Income") |>  # Add title & subtitle
  e_theme("infographic") |>  # theme
  e_legend(right = 0# move legend to the bottom
alt

添加一个 tooltip,其中有很多选项,这里我们使用 trigger = "axis" 来通过轴而不是单个数据点来触发 tooltip。

df |> 
  e_charts(State) |> # initialise and set x
  e_line(Population, smooth = TRUE) |>  # add a line
  e_area(Income, smooth = TRUE) |>  # add area
  e_axis_labels(x = "State") |> # axis labels
  e_title("US States""Population & Income") |>  # Add title & subtitle
  e_theme("infographic") |>  # theme
  e_legend(right = 0) |>  # move legend to the bottom
  e_tooltip(trigger = "axis"# tooltip
alt

最后,我们目前正在同一轴上绘制人口和收入,让我们通过为收入指定一个额外的轴,将它们分别放在各自的 y 轴上。

注意:双 y 轴是一个糟糕的主意,它仅用于演示目的。

df |> 
  e_charts(State) |> # initialise and set x
  e_line(Population, smooth = TRUE) |>  # add a line
  e_area(Income, smooth = TRUE, y_index = 1) |>  # add area
  e_axis_labels(x = "State") |> # axis labels
  e_title("US States""Population & Income") |>  # Add title & subtitle
  e_theme("infographic") |>  # theme
  e_legend(right = 0) |>  # move legend to the bottom
  e_tooltip(trigger = "axis"# tooltip
alt

4 Navigate options

echarts4r 是高度可定制的,有太多的选项,无法将它们全部硬编码在包中,但请放心;它们可用,并且可以传递给 ...。您将在官方文档中找到所有这些选项: https://echarts.apache.org/en/option.html

例如,tooltip 的文档如下所示:

alt

因此,如果我们想将 tooltip 更改为 axisPointer,我们可以通过将列表传递给 axisPointer 来实现。

df |> 
  e_charts(State) |> # initialise and set x
  e_line(Population, smooth = TRUE) |>  # add a line
  e_area(Income, smooth = TRUE) |>  # add area
  e_x_axis(name = "States") |>  # add x axis name
  e_title("US States""Population & Income") |>  # Add title & subtitle
  e_theme("infographic") |>  # theme
  e_legend(right = 0) |> # move legend to the bottom
  e_tooltip(
    axisPointer = list(
      type = "cross"
    )
  ) 
alt

一旦你意识到 R 中的 JSON ~= list,那就很容易了。

您已经了解了基础知识,请转到 advanced 部分或浏览网站以了解如何添加多个链接图表、在地球仪上绘图、使用 shiny 包等等。


--------------- 结束 ---------------

注:本文为个人学习笔记,仅供大家参考学习,不得用于任何商业目的。如有侵权,请联系作者删除。

alt

本文由 mdnice 多平台发布

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

相关文章:

  • 蚁群算法(Ant Colony Optimization, ACO)
  • 使用IDEA构建springboot项目+整合Mybatis
  • 苹果系统中利用活动监视器来终止进程
  • 宝塔安装雷池网站防护
  • JavaScript完整原型链
  • Vue 内置组件 keep-alive 中 LRU 缓存淘汰策略和实现
  • 李宏毅机器学习课程知识点摘要(14-18集)
  • 《AI大模型开发笔记》Faster-Whisper 免费开源的高性能语音识别模型
  • 蓝队基础,网络七杀伤链详解
  • golang开发一个海盗王的登录更新器
  • 李宏毅机器学习课程知识点摘要(6-13集)
  • 003 STM32基础、架构以及资料介绍——常识
  • 【大语言模型】ACL2024论文-20 SCIMON:面向新颖性的科学启示机器优化
  • 开源可视化工具对比:JimuReport VS DataEase
  • 2024年亚太地区数学建模大赛A题-复杂场景下水下图像增强技术的研究
  • shell与QQ邮箱的连接
  • 11.21 深度学习-tensor常见操作
  • 【MySQL课程学习】:MySQL安装,MySQL如何登录和退出?MySQL的简单配置
  • 基于官网的Vue-router安装(2024/11)
  • 未来已来:少儿编程竞赛聚焦物联网,激发创新潜力
  • archlinux安装waydroid
  • Oralce数据库巡检SQL脚本
  • CentOS使用中遇到的问题及解决方法
  • ThinkPad t61p 作SMB服务器,打印服务器,pc ,android ,ipad利用此服务器互传文件
  • php:使用Ratchet类实现分布式websocket服务
  • 储能场站安全风险挑战
  • Ubuntu系统为同一逻辑网口配置不同网段的IP
  • MySQL出现Waiting for table metadata lock的原因以及解决方法(已亲测)
  • 学会Lambda,让程序Pythonic一点
  • GDPU 信息安全 期末复习