1. 程式人生 > 其它 >原創 | 實戰:R環境下Echart的8種視覺化

原創 | 實戰:R環境下Echart的8種視覺化

本文由CDA資料分析研究院曾珂提供,劉春嬌整理,版權私有,侵權必究,轉載請註明出處。

總結一下2016年5月29日資料科學家訓練營R語言課程中Echart學習成果,也把上課用Echart做的圖表及腳步程式碼和大家分享。先講下大概的內容,方便大家上手~

ECHART 安裝(R-3.3.0測試可用)3/3

install.packages('devtools')
library(devtools)
install_github('yihui/recharts')   ##全域性設定
Sys.setlocale("LC_CTYPE","Chs")
source("F:\ORE\2.ORESERVER\oreSCRIPT\ECHART\echartR.R")   ##全域性設定
install.packages('knitr')
library(knitr)
knitr::opts_chunk$set(message=FALSE,warning=FALSE,results='asis')

讀取資料及資料預處理

#資料集說明:汽車貸款違約資料
#使用的變數N:數值變數/T:因變數
##[N]fico_score信用評分
##[N]purch_price 汽車價格
##[N]loan_amt  貸款金額
##[T]bad_ind    是否違約
#讀取資料與資料預處理
setwd("F:\資料指令碼課件\資料")
data=read.csv('accepts.csv')
data$bad_ind=as.factor(data$bad_ind)
data$bankruptcy_ind=as.factor(data$bankruptcy_ind)
data$used_ind=as.factor(data$used_ind)
#缺失值填補(均值/眾數填補)
#定義缺失值填補函式
tianbu=function(data){
for (i in 1:ncol(data)){
  if (sum(is.na(data[,i]))!=0){
    if (class(data[,i])=='factor'){
     a=(as.data.frame(table(data[,i])))
     b=as.numeric(max(mode))
     mode=as.character(a[which(a$Freq==b),1])
     data[which(is.na(data[,i])=='TRUE'),i]=mode[1]
    } #因子使用眾數填補
    else {
      data[which(is.na(data[,i])=='TRUE'),i]=mean(data[,i],na.rm=T)
    } #非因子使用平均數填補
  }
  else 
    warning(paste('變數',colnames(data)[i],'無缺失無需填補',sep=''))}
}
#進行填補
tianbu(data=data)
data
warnings()

1.多系列散點圖

#echart語法見https://github.com/madlogos/recharts

#標註線(標註線性歸回擬合線)
Line=t(c(1,'lm',"lm",F))
#標註點(標註兩個異常值點)
Points=rbind(c(1,NA,1,111696,111166,F),c(1,NA,2,63700,28700,F))
#橫縱軸設定
x=list(lab='汽車金額',color='blue',rotate=45)
y=list(lab='貸款額',color='blue',rotate=45)
#製圖
echartR(data = data, x = ~purch_price, y = ~loan_amt,
        type = 'scatter', palette='aetnaorange',
        title = '散點圖-汽車金額vs貸款額', 
        subtitle = "(source: 汽車貸款資料)",
        xAxis = x, yAxis = y,
        markLine=Line,
        markPoint=Points)

圖形展示如下:

動態圖見連線:

http://localhost:31357/session/viewhtmlcfc382479f9/index.html

2.多系列散點圖

#標註點(標註兩個異常值點)

Points=rbind(c(2,NA,1,760,96692,F),c(1,NA,2,632,111554,F))
#橫縱軸設定
x=list(lab='信用評分',color='blue')
y=list(lab='貸款額',color='blue')
#製圖
echartR(data = data, x = ~fico_score, y = ~loan_amt, series = ~bad_ind,
        type = 'scatter', palette='aetnaorange', symbolList='circle',
        scale=F, xAxis = x,yAxis = y,
        title = '多系列散點圖-信用評分vs貸款額', 
        subtitle = "(source: 汽車貸款資料)",
        markPoint=Points)

圖形展示如下:

動態圖連結:

http://localhost:31357/session/viewhtmlcfc328666f2/index.html

3. 氣泡圖

#資料集說明:中國各省人口GDP和人均壽命資料
#使用的變數N:數值變數
#Prov:省份
#GDP:GDP
#LIFE:平均壽命
#POPULATION:人口
#讀取資料
China=read.csv('gdp.csv')
#Bubble 氣泡圖
echartR(data = China, x = ~Life, y = ~GDP, 
        weight =Population, series = ~Prov, 
        symbolList=c('circle'), 
        type = 'bubble', palette='aetnaorange',
        title = '部分省人均壽命-GDP-人口', 
        subtitle = '(source: GDP)', xlab = '平均壽命', ylab = 'GDP',
        pos=list(title=6,toolbox=3))

圖形展示如下:

動態圖連結:

http://localhost:31357/session/viewhtmlcfc781586d/index.html

4.柱狀圖

library(reshape2)

#資料描述:某化妝品公司資料集
#dis:大區(series)
#type:產品名(x)
#amount銷量(y)
#讀取資料與資料預處理
setwd("F:\資料指令碼課件\資料")
data=read.csv('col.csv')
#Tiled Column柱狀圖
echartR(data = data, x = ~type, y = ~amount,  
        series = ~dis,stack=F,
        type = 'bar', palette='aetnaorange',
        title = "大區-產品-銷量柱狀圖", 
        subtitle = '(source: col)',
        xlab = 'Parameter', ylab = '銷售額')

圖形展示如下:

動態圖連結:

http://localhost:31357/session/viewhtmlcfc4981968/index.html

5.餅圖

#資料準備
setwd("F:\資料指令碼課件\資料")
data=read.csv('col.csv')
#1.ECHART PIE 餅圖
echartR(data, x = ~type,  y = ~amount, type='pie',
        palette='aetnaorange', 
        title='化妝品產品餅圖',
        subtitle = '(source: col)')

圖形展示如下:

動態圖連結:

http://localhost:31357/session/viewhtmlcfc4ca6c45/index.html

6.環圖

echartR(data, x = ~type,  y = ~amount, type='ring',

        palette='aetnaorange', 
        title='化妝品產品餅圖',
        subtitle = '(source: col)')

圖形展示如下:

動態圖連結:

http://localhost:31357/session/viewhtmlcfc67d01c83/index.html

7.地圖

#資料準備

setwd('F:\資料指令碼課件\資料')
dtgdp1=read.csv('CHINA-GDP-12-14.csv')[2:4]


#區域標註
echartR(dtgdp1, x = ~Prov, y = ~GDP, series= ~Year, 
        type=c('map','china','area'),
        title="title",
        subtitle='(source: title)',
        dataRangePalette='heat(5)',
        dataRange=c('High',"Low"),splitNumber=5,
        pos=list(toolbox=3))
#區域標註
worldgdp=data.frame(
  country=c('United States of America','China','Japan','Germany',
            'United Kingdom','France','Brazil', 'Italy','India','Russia',
            'Canada','Australia','South Korea','Spain','Mexico','Indonesia',
            'Netherlands','Turkey','Saudi Arabia','Switzerland'),
  GDP=c(17418925,10380380,4616335,3859547,2945146,2846889,2353025,2147952,
        2049501,1857461,1788717,1444189,1416949,1406855,1282725,888648,866354,
        806108,752459,712050))


echartR(worldgdp, x = ~country, y = ~GDP, type=c('map','world','area'),
        title="Nations with top 20 GDPs, 2014 (Million USD)",
        subtitle = '(source: Wikipedia)',
        dataRangePalette='heat(5)', dataRange=c("High","Low"), 
        splitNumber=10, pos=list(toolbox=3))

圖形展示如下:

動態圖連結:

http://localhost:31357/session/viewhtmlcfc73be3b89/index.html

http://localhost:31357/session/viewhtmlcfc66dc5ced/index.html

#Point 點標註
#資料讀取
chinapm=read.csv('chinapm25.csv',stringsAsFactors=F,header=T,sep=',')[,2:5]
#點標註
top5=head(chinapm[order(chinapm$PM,decreasing=T),],5)
top5$Name="Top 5"
top5$effect=T
top5=top5[,c(5,1,2,4,3,6)]
#製圖
echartR(chinapm, x=~City, y=~PM25, xcoord=~xcoord, ycoord=~ycoord,
        type=c('map','china','point'),
        title='中國各市PM2.5情況',
        subtitle="(source: chinapm)",
        dataRange=c("High","Low"), pos=list(toolbox=3), 
        dataRangePalette='heat(7)',
        splitNumber=7,
        markPoint=top5)

圖形展示如下:

動態圖連結:

http://localhost:31357/session/viewhtmlcfc36cf1824/index.html

#Line 線標註
flight=read.csv('flight.csv',stringsAsFactors=F,header=T,sep=',')
flight$y=''
names(flight)
echartR(flight, x=~From, x1=~To, y=~y, series=~From, xcoord=~Xcoord.x, ycoord=~Ycoord.x,
        xcoord1=~Xcoord.y, ycoord1=~Ycoord.y, type=c('map','china','line'),
        pos=list(toolbox=3), title="南方航空公司主要航班線路")

圖形展示如下:

圖形連結:

http://localhost:31357/session/viewhtmlcfc6518ec5/index.html

8.詞雲

#獲取資料
setwd("F:\資料指令碼課件\資料")
santi=read.csv('santi.csv')
santi
#構建詞雲
echartR(santi[1:30,], x=~word, y=~TFIDF, type="wordcloud", 
title="《三體》特徵詞", palette='aetnaorange', 
subtitle="來源:三體")

圖形展示如下:

動態圖連結:

http://localhost:31357/session/viewhtmlcfc6e7cdef/index.html

9.力導向佈局圖

setwd('F:\資料指令碼課件\資料')
wujiandao=read.csv('netLink.csv',stringsAsFactors=F)
echartR(wujiandao,
        x=~Link,
        y=~weight,
        x1=~NodeVal,
        series=~Series,type='force',
        title='無間道',
        pos=list(title=5,legend=10),
        palette=c('orange','lightblue','orange','lightblue','orange','lightblue'))

圖形展示如下:

動態圖連結:

http://localhost:31357/session/viewhtmlcfc29f8671/index.html