Learn R | 交互可視化之rCharts包(二)
一、NVD3
NVD3 is d3js library, which produces amazing interactive visualizations with little customization.
NVD3是一個旨在建立可復用的圖表和組件的d3.js項目,它可以讓我們通過處理複雜的數據集,創建更高級的可視化圖表。在rCharts包中使用nPlot()函數來實現。
1. 散點圖(Scatter Chart)
> nPlot(Sepal.Length~Sepal.Width,group=Species,data=iris,n+ type=scatterChart) n
可以通過圖形右上角選擇需要查看或隱藏的類別(默認全部類別顯示)。
# showDistX and showDistYn> p <- nPlot(Sepal.Length~Sepal.Width,group=Species,data=iris,n+ type=scatterChart) n> p$chart(showDistX = TRUE, showDistY = TRUE)n> p n
2. 多重條形圖(MultiBar Chart)
> hair_eye <- as.data.frame(HairEyeColor)n> head(hair_eye,5)n Hair Eye Sex Freqn1 Black Brown Male 32n2 Brown Brown Male 53n3 Red Brown Male 10n4 Blond Brown Male 3n5 Black Blue Male 11n> nPlot(Freq ~ Hair, group = Eye, data = subset(hair_eye, Sex == "Female"),n+ type = multiBarChart)n
# 對顏色進行指定n> p$chart(color = c(brown, blue, #594c26, green))n
# 橫向放置n> nPlot(Freq ~ Hair, group = Eye, data = subset(hair_eye, Sex == "Female"),n+ type = multiBarHorizontalChart)n
如果選擇左上角的"Stacked",我們可以使各柱形使用疊加的方式進行擺放。
3. 餅圖(Pie Chart)# 使用ggplot2包diamonds數據集n> library(ggplot2)n> nPlot(~cut, data = diamonds, type = pieChart)n
# 在餅圖的基礎上繪製圓環圖n> p <-nPlot(~cut, data = diamonds, type = pieChart)n> p$chart(donut=TRUE)n> pn
4. 折線圖(Line Chart)
> nPlot(uempmed ~ date, data = economics, type = lineChart)n
# 使用reshape2包的melt()函數對economics數據集進行重塑n# 選取economics數據集的date、uempmed和psavert三列數據n> ecm <- reshape2::melt(economics[,c(date, uempmed, psavert)], id = date)n> head(ecm)n date variable valuen1 1967-07-01 uempmed 4.5n2 1967-08-01 uempmed 4.7n3 1967-09-01 uempmed 4.6n4 1967-10-01 uempmed 4.9n5 1967-11-01 uempmed 4.7n6 1967-12-01 uempmed 4.8n> p <- nPlot(value ~ date, group = variable, data = ecm, n+ type = lineWithFocusChart)n# 對X軸進行修改n> p$xAxis( tickFormat="#!function(d) {return d3.time.format(%b %Y)n+ (new Date( d * 86400000 ));}!#" )n> pn
6. InteractiveGuidline(Multi-Tooltips) on Line
通過useInteractiveGuideline=TRUE選項,我們可以同時獲取在橫縱坐標軸指定的情況下不同折線上的數值。
> p <- nPlot(value ~ date, group = variable, data = ecm, n+ type = lineChart)n> p$xAxis( tickFormat="#!function(d) {return d3.time.format(%b %Y)n+ (new Date( d * 86400000 ));}!#" )n> p$chart(useInteractiveGuideline=TRUE)n> pn
7. Sparklines
> p <- nPlot(uempmed ~ date, data = economics, n+ type = sparklinePlus,height=300,width_=700)n> p$chart(xTickFormat="#!function(d) {return d3.time.format(%b %Y)n+ (new Date(d * 86400000 ));}!#")n> pn
二、HighCharts
Highcharts是一個製作圖表的純Javascript類庫,支持大部分的圖表類型。在rCharts包中使用hPlot()函數來調用。
1. 散點圖(Scatter Chart)
> hPlot(Sepal.Length~Sepal.Width,data=iris,type=scatter,group=Species)n
我們可以在圖形下方選擇需要查看或隱藏的類別(默認全部顯示)。
# 自定義顏色、圖例位置、點類型等n> p <- hPlot(Pulse ~ Height, data = MASS::survey, type = scatter, n+ group = Sex, radius = 6, group.na = "Not Available")n> p$colors(rgba(223, 83, 83, .5), rgba(119, 152, 191, .5), n+ rgba(60, 179, 113, .5))n> p$legend(align = right, verticalAlign = top, layout = vertical)n> p$plotOptions(scatter = list(marker = list(symbol = circle)))n> pn
2. 氣泡圖(Bubble Chart)
> hPlot(mpg~hp,data=mtcars,type=bubble,group=am,title = "Zoom demo",n+ subtitle = "bubble chart",size=qsec)n
3. 餅圖(Pie Chart)
> summary(ggplot2::diamonds$cut)n Fair Good Very Good Premium Ideal n 1610 4906 12082 13791 21551n> data <- dataframe(key=c(Fair,Good,Very Good,Premium,Ideal),n+ value=c(1610,4906,12082,13791,21551))n> hPlot(x=key,y=value,data=data,type=pie)n
4. Multi Chart
# 同時繪製條形圖和折線圖n> head(plyr::count(ggplot2::diamonds,c(cut,color)))n cut color freqn1 Fair D 163n2 Fair E 224n3 Fair F 312n4 Fair G 314n5 Fair H 303n6 Fair I 175n> p <- hPlot(freq~cut,data=plyr::count(ggplot2::diamonds,c(cut,color)),n+ type = c(column, line), group = color)n> pn
# 同時繪製折線圖、散點圖和氣泡圖n> hPlot(mpg~hp,data=mtcars,type=c(scatter,bubble,line),n+ group=cyl,size=qsec)n
5. Bar Chart
> head(plyr::count(mtcars,c(cyl,am)))n cyl am freqn1 4 0 3n2 4 1 8n3 6 0 4n4 6 1 3n5 8 0 12n6 8 1 2n> p <- hPlot(freq ~ cyl, data = plyr::count(mtcars,c(cyl,am)),n+ type = bar, group =am)n> p$colors(rgba(223, 83, 83, .5), rgba(119, 152, 191, .5))n> pn
附學習文檔:
- RPubs - rCharts Package Tutorial
- Interactive Charts from R using rCharts
- Editor: 利用R語言進行交互數據可視化 | 統計之都
- GitHub - ramnathv/rCharts: Interactive JS Charts from R
推薦閱讀:
※給你的圖表增值加分(1):簇狀柱形圖上顯示增幅百分比
※Python數據分析及可視化實例之熱力雲圖
※小心,這餅有毒!論餅圖的正確打開方式
※Power BI應用場景賞析-財務篇
※[E1-06]數據錄入