R可视化数据包rCharts的探索运用( 一)

1 篇文章 0 订阅
1 篇文章 0 订阅

前言:

        学习R将近大半年了,从小白步入了门槛。一直对可视化很感兴趣,很早就看到rCharts这个数据包了,总想下一个玩玩,但苦于在Rstudio的install.package的安装数据包中没找到,又由于忙着学一些建模知识后来也就没在意。最近在自己买的一本书里发现了该包的运用,遂写下该篇文章进行分享。


一、安装及相关安装准备:

安装rCharts需要安装以下包:

安装小tips:

当然你可以选择先安装devtools包,在安装此包时他会附带的把其他包一起打包下载:如RCurl、RJSONIO等,最后把安装错误的包再单独安装下。


安装好之后我们就可以开始愉快的安装rCharts包了

devtools::install_github("ramnathv/rCharts")

PS:ramnathv代表这个包的作者,rCharts代表这个包的名称

    devtools::install_github表示运用devtools下的install_github函数


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

载入数据包:

library(rCharts)

首先调用ggplot2中的diamonds砖石数据集

data("diamonds",  package = "ggplot2")

1.散点图(point)

以切割进行分类展示,观测价格与钻石重量(克拉)的关系

rPlot(price ~ carat | cut, data = diamonds, 

         type = "point", color = "cut") 

以下为输出结果:

鼠标移动到图片上会显示相关数据,

与此也可以对多重分面进行展示:

比如还想观察颜色和切割的下价格,克拉关系

rPlot(price ~ carat | cut + color, data = diamonds, 

         type = "point", color = "cut") 

本次运行是笔记本,中等配置,运行后会卡一会,卡的时候请耐心等待,第一次尝试的时候由于比较手贱,多点了几下,程序崩溃了。



2. 条形图(bar)

调用HairEyeColor数据集

data("HairEyeColor")

将数据集转化为数据框格式,并仅选男性数据

hair_eye_male <- subset(as.data.frame(HairEyeColor), Sex = "male")

p <- rPlot(Freq ~ Hair, color = 'Eye', data = hair_eye_male, type = 'bar')

直接作条形图,堆叠在一起

以下进行修改,以eye分类,wrap(自动转行)

p$facet(var = 'Eye', type = 'wrap', rows = 2)

p


3. 箱线图(Box)

务必要给y变量加上box(xxx)

rPlot(x = 'Species', y = 'box(SepalWidth)', data =iris, type = 'box', color = 'Species')


二、使用rCharts的nPlot()函数进行可视化展示。(NVD3复用图表和组件d3.js项目。)

1.条形图(multiBarChar)

将数据转化为数据框格式

hair_eye_male <- subset(as.data.frame(HairEyeColor), Sex = "male")

str(hair_eye_male)

nPlot(Freq~Hair, group = "Eye", data = hair_eye_male, type = "multiBarChart")

红框可以选择是分组还是堆叠显示,并且在右上角可以筛选需要观测的数据,

注:当type = "muLtiBarHorizontaChart"条形图为横向


2.气泡图(scatterChart)

a <- nPlot(Sepal.Length ~ Sepal.Width, group = "Species", data = iris, 
           type = "scatterChart")

设置xy轴名称(这个气泡图不会显示xy轴的名称)需要手动设置

a$xAxis(axisLabel = "Sepal.Width")
a$yAxis(axisLabel = "Sepal.Length")

设置刻度

a$chart(showDistX = TRUE, showDistY = TRUE)

设置颜色

a$chart(color= c("red" ,"blue", "green"))
a



3. 饼图(PieChart)

x <- nPlot(~color, data = diamonds, type = "pieChart")

当选择diamonds,cut切割时图例颜色能正确使用(图例个数较少),color图例显示有问题

x$chart(color = c("Red", "orange", "yellow", "green", "blue", "indigo", "purple"))

x

当设置donut=TRUE时显示为环形图

x$chart(donut=TRUE)

4.折线图(Line)

数据集来自于ggplot2的economics

data <- transform(economics, date = as.character(date))

mPlot(x= "date", y = list("psavert", "uempmed"),

      data = data,type = "Line", pointSize = 0, lineWidth = 1)





参考书籍:《R语言与数据挖掘》

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值