R语言在可视化的展示上具有很强大的功能,并且可以跟很多语言进行结合使用构建交互的可视化图像。今天给大家介绍下在R语言中如何生成基于Javascript的可视化界面。首先需要用的R包是rCharts,大家看下包的安装:
代码语言:javascript复制library(devtools)
devtools::install_github('ramnathv/rCharts')
接下来我们利用实例进行可视化的结果展示:
代码语言:javascript复制##基础柱形图的绘制
haireye <-as.data.frame(HairEyeColor)
dat <-subset(haireye, Sex == "Female" & Eye == "Blue")
p1 <-mPlot(x = 'Hair', y = list('Freq'), data = dat, type = 'Bar', labels =list("Count"))
p1
代码语言:javascript复制##多组柱状图的绘制
dat <-subset(haireye, Sex == "Female")
p2 <-mPlot(Freq ~ Eye, group = "Hair", data = dat, type = "Bar",labels = 1:4)
p2
代码语言:javascript复制##柱状图坐标轴转置
hPlot(freq ~Exer, data = plyr::count(MASS::survey, c('Sex', 'Exer')), type = 'bar', group ='Sex', group.na = 'NA's')
代码语言:javascript复制##增加点击事件
a <-hPlot(freq ~ Exer, data = plyr::count(MASS::survey, c('Sex', 'Exer')), type ='bar', group = 'Sex', group.na = 'NA's')
a$plotOptions(bar= list(cursor = 'pointer', point = list(events = list(click = "#!function() { alert ('Category: ' this.category ', value: ' this.y); }!#"))))
a
代码语言:javascript复制##增加直线分割
a <-hPlot(freq ~ Exer, data = plyr::count(MASS::survey, c('Sex', 'Exer')), type ='column', group = 'Sex', group.na = 'NA's')
a$plotOptions(column= list(dataLabels = list(enabled = T, rotation = -90,, color ='#FFFFFF', x = 4, y = 10, style = list(fontSize = '13px', fontFamily ='Verdana, sans-serif'))))
a$xAxis(labels= list(rotation = -45,, style = list(fontSize = '13px',fontFamily = 'Verdana, sans-serif')), replace = F)
a
代码语言:javascript复制##增加各组的子图
a <-rCharts::Highcharts$new()
a$chart(type ="column")
a$series(data =list(
list(y = 15, drilldown = list(data = c(1,2, 3))),
list(y = 20, drilldown = list(data = c(1,2, 3)))), name = "test")
a$xAxis(categories= c("Brand A", "Brand B"))
a$plotOptions(column= list(cursor = 'pointer', point = list(events = list(click =drill_function))))
a
代码语言:javascript复制##为图像增加超链接,点击图像就可以连接到对应的网站。
a <-rCharts::Highcharts$new()
a$series(data =list(
list(y = 8, url ="https://github.com/metagraf/rHighcharts", color ="lightblue"),
list(y = 14, url ="https://github.com/metagraf/rVega", color = "lightpink"),
list(y = 71, url ="https://github.com/ramnathv/rCharts", color ="lightgreen")
), type ="column", name = "Number of Stars")
a$plotOptions(column= list(cursor = 'pointer', point = list(events = list(click = "#!function() { location.href = this.options.url; } !#"))))
a$xAxis(categories= c("rHighcharts", "rVega", "rCharts"), title =list(text = ""))
a$yAxis(title =list(text = ""))
a$legend(enabled= F)
a
代码语言:javascript复制##折线图
data(economics,package = 'ggplot2')
dat <-transform(economics, date = as.character(date))
p3 <-mPlot(x = "date", y = list("psavert", "uempmed"),data = dat, type = 'Line',
pointSize = 0, lineWidth = 1)
代码语言:javascript复制##面积折线图
p3$set(type ='Area')
p3
代码语言:javascript复制##散点图绘制
hPlot(Pulse ~Height, data = MASS::survey, type = "scatter", group ="Exer")
代码语言:javascript复制##颜色的设置,legend设置
a <-hPlot(Pulse ~ Height, data = MASS::survey, type = 'scatter', group = 'Sex',radius = 6, group.na = "Not Available")
a$colors('rgba(223,83, 83, .5)', 'rgba(119, 152, 191, .5)', 'rgba(60, 179, 113, .5)')
a$legend(align= 'right', verticalAlign = 'top', layout = 'vertical')
a$plotOptions(scatter= list(marker = list(symbol = 'circle')))
a$tooltip(formatter= "#! function() { return this.x ', ' this.y; } !#")
a
代码语言:javascript复制##放大并添加下载按钮
a <-hPlot(Pulse ~ Height, data = MASS::survey, type = "bubble", title ="Zoom demo", subtitle = "bubble chart", size ="Age", group = "Exer")
a$chart(zoomType= "xy")
a$exporting(enabled= T)
a
代码语言:javascript复制##饼图的绘制
x <-data.frame(key = c("a", "b", "c"), value = c(1,2, 3))
hPlot(x ="key", y = "value", data = x, type = "pie")
代码语言:javascript复制##混合图像绘制
hPlot(freq ~Exer, data = plyr::count(MASS::survey, c('Sex', 'Exer')), type = c('column','line'), group = 'Sex', radius = 6)
代码语言:javascript复制##地图绘制
map1 = Leaflet$new()
map1$setView(c(45.5236,-122.675), 13)
map1$tileLayer("http://a.tiles.mapbox.com/v3/mapbox.control-room/{z}/{x}/{y}.png",zoom = 8)
map1
##坐标定位
map1 =Leaflet$new()
map1$setView(c(45.50867,-73.55399), 13)
map1
当然,这个工具包结合shiny会发挥更大的价值,所以如果熟悉shiny的朋友可以加以利用。
欢迎大家学习交流!