zoukankan      html  css  js  c++  java
  • R语言可视化之 echarts4r

    ECharts,一个使用 JavaScript 实现的开源可视化库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(IE8/9/10/11,Chrome,Firefox,Safari等),底层依赖矢量图形库 ZRender,提供直观,交互丰富,可高度个性化定制的数据可视化图表。

    echarts4r 包是 ECharts 的 R 语言接口,目前可以从 CRAN 是直接安装。echarts4r 语法结构简单,易用,可读性很好,是很好的交互式绘图包。

    安装echarts4r包,具体操作见上篇 R包 安装教程

    教程下载:https://cran.r-project.org/web/packages/echarts4r/index.html

    install.packages("echarts4r")
    

      

    散点图

    echarts4r 作图第一步用e_charts函数创建一个 echarts4r 对象,函数第一个参数为数据,第二个参数为 x 轴数据,第二步以及后续都是用%>%管道操作符来进一步作图。

    绘制散点图,用iris数据,x 轴为 Sepal.Length。y 轴为 Petal.Length,在e_scatter中定义为serie。通过group_by根据 Sepal.Length 进行分组,在图中表现为不同颜色。散点大小通过size参数设置。

    library(echarts4r)
    iris %>%
        group_by(Species) %>%
        e_charts(x = Sepal.Length) %>%
        e_scatter(serie = Petal.Length, size = Sepal.Width)
    

      对比一下 ggplot2 的散点图语法及效果

    library(ggplot2)
    iris %>%
      ggplot(aes(x=Sepal.Length,y=Petal.Length,size=Sepal.Width,col=Species))+
      geom_point()
    

      

    柱状图

    library(echarts4r)
    df <- data.frame(
      x = seq(50),
      y = rnorm(50, 10, 3),
      z = rnorm(50, 11, 2),
      w = rnorm(50, 9, 2)
    )
     
    df %>%
      e_charts(x) %>%
      e_bar(y, name = "bar") %>%
      e_title("Bar and step charts")
    

      

    热力图

    library(echarts4r)
    v <- LETTERS[1:10]
    matrix <- data.frame(
      x = sample(v, 300, replace = TRUE),
      y = sample(v, 300, replace = TRUE),
      z = rnorm(300, 10, 1),
      stringsAsFactors = FALSE
    ) %>%
      dplyr::group_by(x, y) %>%
      dplyr::summarise(z = sum(z)) %>%
      dplyr::ungroup()
     
    matrix %>%
      e_charts(x) %>%
      e_heatmap(y, z) %>%
      e_visual_map(z) %>%
      e_title("Heatmap")
    

     

    仪表盘 

    library(echarts4r)
    e_charts() %>%
      e_gauge(41, "PERCENT") %>%
      e_title("Gauge")
    

      

     词云

    library(echarts4r)
    words <- function(n = 5000) {
      a <- do.call(paste0, replicate(5, sample(LETTERS, n, TRUE), FALSE))
      paste0(a, sprintf("%04d", sample(9999, n, TRUE)), sample(LETTERS, n, TRUE))
    }
     
    tf <- data.frame(terms = words(100),
      freq = rnorm(100, 55, 10)) %>%
      dplyr::arrange(-freq)
     
    tf %>%
      e_color_range(freq, color) %>%
      e_charts() %>%
      e_cloud(terms, freq, color, shape = "circle", sizeRange = c(3, 15)) %>%
      e_title("Wordcloud", "Random strings")
    

      

    echarts4r 教程下载https://cran.r-project.org/web/packages/echarts4r/index.html 

  • 相关阅读:
    ajax post 数组
    Hello 2018 ABC
    A. The Way to Home
    Codeforces Round #453 (Div. 2) ABC
    Codeforces Round #452 (Div. 2) D
    Codeforces Round #452 (Div. 2) ABC
    Python 常用技巧
    Codeforces Round #451 (Div. 2) E
    Codeforces Round #451 (Div. 2) D. Alarm Clock
    Codeforces Round #451 (Div. 2) ABC
  • 原文地址:https://www.cnblogs.com/adam012019/p/14655756.html
Copyright © 2011-2022 走看看