R语言数据分析R代码

R语言:highcharter包绘制可交互的高级图表(二)

2018-08-26  本文已影响265人  数据老黄

本期介绍在R语言如何使用highcharter包绘制可交互的高级图表,highcharter包是R语言中绘制Highcharts图表的包。Highcharts 是用纯JavaScript编写的一个图表库, 能够很简单便捷的在Web网站或是Web应用程序添 加有交互性的图表,并且免费提供给个人学习、个人网站和非商业用途使用。Highcharts详情:www.hcharts.cn

上一期介绍了如何使用highcharter包绘制可交互的高级图表的一部分,本期介绍高级图表的剩余,包括:箱线图、瀑布图、漏斗图、桑基图、词云图、热力图、帕累托图、矩形树图的绘制。同样的,请大家安装highcharterdata.table包并导入,我们开始详细看一下各图表的制作细节。

箱线图

箱线图又名盒须图、盒式图或箱线图,主要用于反映原始数据分布的特征,以及进行多组数据分布特征的比较。在highcharter中通过函数hcboxplot或函数hc_add_series_boxplot 均可实现箱线图.

函数hc_add_series_boxplot:

box_data <- data.table(type =rep(c("1","2","3", "4","5"),each = 5) ,
                       num = c(760,801,1300,895,965,733,853,939,980,1080,714,762,890,870,918,724,802,806,871,950,834,836,864,882,910))

highchart() %>%
  hc_title(text = "箱线图",align="center")%>%
  hc_yAxis(title = list(text = "观测值"))%>%
  hc_legend(enabled = FALSE)%>%
  hc_add_series_boxplot(x = box_data$num, by = box_data$type, name = "type",
                        #是否显示异常值,默认值是TRUE
                        outliers = TRUE) %>%
  hc_add_theme(hc_theme_ft())

函数hcboxplot:

###箱线图2
hcboxplot(x = box_data$num, var =box_data$type,name = "type" ) %>%
  hc_title(text = "箱线图",align="center")%>%
  hc_yAxis(title = list(text = "观测值"))%>%
  hc_legend(enabled = FALSE) %>%
  hc_add_theme(hc_theme_ft())

瀑布图

瀑布图的实现,相比其他图需要进行较多的设置,尤其是在输入的数据项上面,详情见代码内,瀑布图的type值为waterfall.

waterfall_data <- data.table(name = c("启动资金","产品收入","服务收入","固定成本","可变成本","汇总"),
                             y = c(120000,569000,231000,-342000,-233000,NA),
                             isSUM = c(FALSE,FALSE,FALSE,FALSE,FALSE,TRUE),
                             isIntermediateSum = c(FALSE,FALSE,FALSE,FALSE,FALSE,TRUE),
                             color = c("#008FD5","#008FD5","#008FD5","#FF2700","#FF2700","#77AB43"))

highchart() %>% 
  hc_title(text = "瀑布图",align="center") %>%
  
  hc_xAxis(#坐标轴类型,设置后在hcaes中设置x即可
           type = "category") %>%
  hc_yAxis(title = list(text = "USD")) %>%
  hc_legend(enabled = FALSE) %>%
  hc_plotOptions(series = list(#图内柱子边界线宽,设置为0,取消显示边界线
                               borderWidth = 0))%>%
  hc_tooltip(pointFormat = "<b>${point.y}</b> USD") %>%
  hc_add_series(data = waterfall_data,type = "waterfall",
                hcaes(y = y,x= name,
                      #设置自动计算汇总项
                      isSUM = isSUM,isIntermediateSum =isIntermediateSum,
                      color=color))%>%
  hc_add_theme(hc_theme_538())

漏斗图

漏斗图能够直观形象的展示数据的转化路径,其type的值为funnel,实现相对比较简单。

funnel_data <- data.table(type=c("访问商品","加入购物车","提交订单","点击支付","支付成功"),
                          pv = c(15654,4064,1987,976,451))
highchart() %>% 
  hc_title(text = "电商转化漏斗") %>%
  hc_plotOptions(series = list(dataLabels = list(
                                                 format = "<b>{point.name}</b> ({point.y:,.0f})"),
                               #控制漏斗图的长宽
                               neckWidth = "25%",neckHeight = "25%"
                               ))%>%
  hc_legend(enabled = FALSE)%>%
  hc_add_series(name ="电商",type = "funnel",data = funnel_data,hcaes(x= type,y = pv))%>%
  hc_add_theme(hc_theme_elementary())

桑基图

桑基图展现的是数据的流向,有起点和终点,连接起点和终点的曲线宽度表示数值的相对大小。桑基图制作重点是需要注意输入的数据格式,保证其起点和终点顺序清晰明确,这决定了最终呈现的效果。(如执行后图未展示,请以此方式重新安装highcharter包:devtools::install_github("jbkunst/highcharter"))

sankey_data <- data.table(from = c("巴西",    "巴西",   "巴西",   "巴西",   "加拿大",  "加拿大",  "加拿大",  "墨西哥",  "墨西哥",  "墨西哥",  "墨西哥",  "美国",   "美国",   "美国",   "美国",   "葡萄牙",  "葡萄牙",  "葡萄牙",  "葡萄牙",  "法国",   "法国",   "法国",   "法国",   "法国",   "西班牙",  "西班牙",  "西班牙",  "英国",   "英国",   "英国",   "英国",   "南非",   "南非",   "南非",   "安哥拉",  "安哥拉",  "安哥拉",  "塞内加尔", "塞内加尔", "塞内加尔", "马里",   "马里",   "马里",   "摩洛哥",  "摩洛哥",  "摩洛哥"),
                          to = c("葡萄牙", "法国",   "西班牙",  "英国",   "葡萄牙",  "法国",   "英国",   "葡萄牙",  "法国",   "西班牙",  "英国",   "葡萄牙",  "法国",   "西班牙",  "英国",   "安哥拉",  "塞内加尔", "摩洛哥",  "南非",   "安哥拉",  "塞内加尔", "马里",   "摩洛哥",  "南非",   "塞内加尔", "摩洛哥",  "南非",   "安哥拉",  "塞内加尔", "摩洛哥",  "南非",   "中国",   "印度",   "日本",   "中国",   "印度",   "日本",   "中国",   "印度",   "日本",   "中国",   "印度",   "日本",   "中国",   "印度",   "日本"),
                          weight = c(5, 1,  1,  1,  1,  5,  1,  1,  1,  5,  1,  1,  1,  1,  5,  2,  1,  1,  3,  1,  3,  3,  3,  1,  1,  3,  1,  1,  1,  2,  7,  5,  1,  3,  5,  1,  3,  5,  1,  3,  5,  1,  3,  5,  1,  3))

highchart() %>%
  hc_title(text = "桑基图") %>%
  hc_add_series(data = sankey_data,type = "sankey",hcaes(from = from,to = to,weight = weight)) %>%
  hc_add_theme(hc_theme_google())

词云图

词云图用于突出显示文本中高频关键词汇,展示文本的核心重点。同样的,其实现也非常简单。

text_data <- data.table(text =c("巴西",   "加拿大",  "墨西哥",  "美国",   "葡萄牙",  "法国",   "西班牙",  "英国",   "南非",   "安哥拉",  "塞内加尔", "马里",   "摩洛哥",  "中国",   "印度",   "日本"),
                        weight =c(2,    3,  1,  1,  4,  5,  4,  3,  1,  1,  2,  3,  2,  6,  1,  1))

highchart() %>%
  #hc_title(text = "词云图") %>%
  hc_add_series(data = text_data,type = "wordcloud",name= "得分",hcaes(name = text,weight = weight)) %>%
  hc_add_theme(hc_theme_flat())

热力图

热力图的type值为heatmap,需要特别注意的是设置颜色的渐变需通过函数hc_colorAxis,设定其参数minColormaxColor 即可。

name <- c("Alexander",  "Marie",    "Maximilian",   "Sophia",   "Lukas",    "Maria",    "Leon", "Anna", "Tim",  "Laura")
weekday <- c("Monday", "Tuesday",   "Wednesday",    "Thursday", "Friday")
heatmap_data = data.table(name =rep(name,each = length(weekday)),
                          weekday =rep(weekday,times = length(name)),
                          sales = round(runif(length(weekday) * length(name),min=20,max=100),0))

highchart() %>%
  hc_title(text = "每周销售数据",align="center")%>%
  hc_xAxis(categories = name) %>%
  hc_yAxis(categories = weekday)%>%
  hc_colorAxis(min = 0,minColor = "#FFFFFF") %>%
  hc_legend(align = "right",layout = "vertical",margin = 0,verticalAlign = "top",y = 25,symbolHeight = 400) %>%
  hc_tooltip(formatter = JS("function () {return '<b>' + this.series.xAxis.categories[this.point.x] + '</b> sold <br><b>' +
                                    this.point.value + '</b> items on <br><b>' + this.series.yAxis.categories[this.point.y] + '</b>';}")) %>%
  hc_add_series(name = "Sales per employee",data = heatmap_data,type = "heatmap",
                hcaes(x = name,y = weekday,value = sales),
                dataLabels = list(enabled = TRUE)) %>%
  hc_add_theme(hc_theme_538())

帕累托图

帕累托图是将出现的问题或改进项目按照重要程度依次排列而采用的一种图表,是二八法则的图形体现,用于分析问题或项目的核心影响因素。帕累托图有条形图和折线图组合而成,其中的折线数据需要我们在作图之前自行进行一步计算。

type = c("价格过高",    "分量过小", "等待时间过长",   "食物难吃", "氛围不好", "不卫生",  "太吵",   "服务态度不好")
num  =  c(755, 222, 151, 86, 72, 51, 36, 10)
rate = round(cumsum(num) / sum(num) *100,2)
highchart() %>%
  hc_title(text = "餐厅投诉情况",align = "center") %>%
  hc_plotOptions(series = list(#图内柱子边界线宽,设置为0,取消显示边界线
                                borderWidth = 0)) %>%
  hc_xAxis(categories = type) %>%
  hc_yAxis_multiples(
           list(
             title = list(text = ""),
             tickPositions = c(0,200,400,600,800)),
           list(
             title = list(text = ""),
             minPadding = 0,
             maxPadding = 0,
             max = 100,
             min = 0,
             opposite = TRUE,
             labels = list(format = "{value}%"),
             tickPositions = c(0,25,50,75,100)
           )
           )%>%
  hc_tooltip(pointFormat = "{series.name} {point.y:.2f} %")%>%
  hc_add_series(name = "投诉次数",type = "column",zIndex=1,data = num) %>%
  hc_add_series(type = "line",name = "累计比率",data = rate,yAxis =1,zIndex = 2) %>%
  hc_add_theme(hc_theme_economist())

矩形树图

矩形树图类似热力图,通过函数hc_colorAxis设置颜色随值的变化,不同是还需在hc_add_series中设置colorValue的值才能最终实现颜色变化。另外矩形树图的type值为treemap.

tree_data <- data.table(country_name =c("巴西",   "加拿大",  "墨西哥",  "美国",   "葡萄牙",  "法国",   "西班牙",  "英国",   "南非",   "安哥拉",  "塞内加尔", "马里",   "摩洛哥",  "中国",   "印度",   "日本"),
                        value =c(2, 3,  1,  1,  4,5,    4,  3,  1,  1,  2,  3,  2,  6,  1,  1))

highchart() %>%
  hc_colorAxis(     maxColor = "#0043AE")%>%
  hc_title(text = "矩形树图")%>%
  hc_add_series(data = tree_data,type = "treemap",hcaes(name = country_name,value = value,colorValue =value),layoutAlgorithm = "squarified") %>%
  hc_add_theme(hc_theme_google())
上一篇下一篇

猜你喜欢

热点阅读