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')


640?wx_fmt=jpeg

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

640?wx_fmt=jpeg

2. 条形图(Barplot)

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

640?wx_fmt=jpeg

3. 箱线图(Boxplot)

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

640?wx_fmt=jpeg

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'))

640?wx_fmt=jpeg

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')

640?wx_fmt=jpeg

二、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"))

640?wx_fmt=jpeg

# 绘制多重条形图> 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'))

640?wx_fmt=jpeg

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

640?wx_fmt=jpeg3. 面积图(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

640?wx_fmt=png

三、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')

640?wx_fmt=jpeg2. 条形图(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')


640?wx_fmt=jpeg

附学习文档:

  1. RPubs - rCharts Package Tutorial

  2. Interactive Charts from R using rCharts

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

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

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

往期精彩:

640?wx_fmt=png

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值