R语言:交互可视化之rCharts包

文章转自知乎专栏 Jason

前言

rCharts是一个专门用来在R中绘制交互式图形的第三方包。关于交互式图形,之前我们在学习ggplot2包时略有提及(plotly包),今天所学习的rCharts包不仅是以交互图形的展示为目的,更重要的是该包有着一套自己的绘图风格,这为我们使用R进行可视化展示提供了更多的选择。

由于rCharts包目前并没有收录进CRAN,而是托管在GitHub上,所以我们需要先安装devtools包:


rCharts包的绘图函数类似lattice包,通过formula、data指定数据源和绘图方式,并通过type指定图表类型。基本格式如下:

> graph_function(formula,data=,option)


一、Polychart

使用Polychart的rPlot()函数进行可视化展示。

Ploychart:a javascript charting library based on the grammar of graphics, and inspired by ggplot2

1. 散点图(Scatter Plot)

> library(rCharts)# 指定展示尺寸> options(RCHART_WIDTH = 700, RCHART_HEIGHT = 500)# 使用内置数据集iris# 按照Species的不同类型进行分面和颜色,指定绘图类型为"point"> rPlot(SepalLength ~ SepalWidth | Species, data = iris, color = 'Species', type = 'point')


R语言:交互可视化之rCharts包_第1张图片

# 多重分面> rPlot(mpg ~ wt | am + cyl, data = mtcars, type = 'point',color = 'gear')

R语言:交互可视化之rCharts包_第2张图片

2. 条形图(Barplot)

> p <- rPlot(Freq ~ Hair, color = 'Eye', data = hair_eye, type = 'bar')# 进行分面操作及指定行> p$facet(var = 'Eye', type = 'wrap', rows = 2)> p

R语言:交互可视化之rCharts包_第3张图片

3. 箱线图(Boxplot)

> rPlot(x = 'Species', y = 'box(SepalLength)', data =iris, type = 'box')

R语言:交互可视化之rCharts包_第4张图片

4. 柱状图(Column Plot)

> library(plyr)> data <- count(mtcars, .(gear, cyl))> data  gear cyl freq1    3   4    12    3   6    23    3   8   124    4   4    85    4   6    46    5   4    27    5   6    18    5   8    2> rPlot(x = 'bin(gear,1)', y = 'freq', data = data, type = 'bar',list(var = 'cyl', type = 'wrap'))

R语言:交互可视化之rCharts包_第5张图片

5. 热力图(Heat Map)

# 数据准备> data <- expand.grid(x = 1:5, y = 1:5)> data <- transform(data, value = iris$Sepal.Length)# 指定类型为tile> rPlot(x = 'bin(x, 1)', y = 'bin(y, 1)', color = 'value', data = data, type = 'tile')

R语言:交互可视化之rCharts包_第6张图片

二、Morris

使用MorrisJS库的mPlot()函数进行可视化绘图。

Morris.js is the library that powers the graphs on howmanyleft.co.uk/ . It's a very simple API for drawing line, bar, area and donut charts.

1. 条形图(Bar Chart)

# 数据准备> haireye <- as.data.frame(HairEyeColor)> data    Hair  Eye    Sex Freq21 Black Blue Female    922 Brown Blue Female   3423   Red Blue Female    724 Blond Blue Female   64> mPlot(x = 'Hair', y = list('Freq'), data = data, type = 'Bar', + labels = list("Count"))

R语言:交互可视化之rCharts包_第7张图片

# 绘制多重条形图> data <- subset(haireye, Sex == "Female")> head(data)    Hair   Eye    Sex Freq17 Black Brown Female   3618 Brown Brown Female   6619   Red Brown Female   1620 Blond Brown Female    421 Black  Blue Female    922 Brown  Blue Female   34> mPlot(Freq ~ Eye, group = "Hair", data = data, type = "Bar", + labels = c('a','b','c','d'))

R语言:交互可视化之rCharts包_第8张图片

2. 折线图(Line Chart)

# 使用ggplot2的economics数据集> library(ggplot2)> data <- transform(economics,date=as.character(date))> p <- mPlot(x = 'date', y = c('psavert', 'uempmed'), type = 'Line',data = data)> p$set(pointSize = 0, lineWidth = 1)> p

R语言:交互可视化之rCharts包_第9张图片3. 面积图(Area Chart)

> data <- transform(economics,date=as.character(date))> p <- mPlot(x = "date", y = list("psavert", "uempmed"), data = data, type = 'Line',pointSize = 0, lineWidth = 1)> p$set(type = 'Area')> p

R语言:交互可视化之rCharts包_第10张图片

三、xCharts

xCharts is a D3-based library for building custom charts and graphs. Written and maintained by tenXer.
调用函数: xPlot()

1. 折线图(Line Chart)

# 使用melt()函数进行数据重塑> library(reshape2)> uspexp <- melt(USPersonalExpenditure)> head(uspexp)                 Var1 Var2  value1    Food and Tobacco 1940 22.2002 Household Operation 1940 10.5003  Medical and Health 1940  3.5304       Personal Care 1940  1.0405   Private Education 1940  0.3416    Food and Tobacco 1945 44.500> names(uspexp)[1:2] <- c('category', 'year')> xPlot(value ~ year, group = 'category', data = uspexp, + type = 'line-dotted')

R语言:交互可视化之rCharts包_第11张图片2. 条形图(Bar Chart)

# 数据准备> haireye <- subset(as.data.frame(HairEyeColor), Sex == "Male")> head(haireye)   Hair   Eye  Sex Freq1 Black Brown Male   322 Brown Brown Male   533   Red Brown Male   104 Blond Brown Male    35 Black  Blue Male   116 Brown  Blue Male   50> xPlot(Freq ~ Hair, group = 'Eye', data = haireye, type = 'bar')


R语言:交互可视化之rCharts包_第12张图片

附学习文档:

  1. RPubs - rCharts Package Tutorial

  2. Interactive Charts from R using rCharts

  3. Editor: 利用R语言进行交互数据可视化 | 统计之都

  4. GitHub - ramnathv/rCharts: Interactive JS Charts from R

——————————————

往期精彩:

  • 我造的假我自己打,Adobe推出“反PS”

  • 微软删除人脸识别,除了隐私,更重要的可能是性别歧视与种族主义

  • 亚马逊在中国失败,而中国却在亚马逊成功

640?wx_fmt=png

你可能感兴趣的:(R语言:交互可视化之rCharts包)