R語言繪圖-highcharter包繪製可互動圖表

箱線圖

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())

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())

瀑布圖

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 = “${point。y} 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())

漏斗圖

funnel_data <- data。table(type=c(“訪問商品”,“加入購物車”,“提交訂單”,“點選支付”,“支付成功”), pv = c(15654,4064,1987,976,451))highchart() %>% hc_title(text = “電商轉化漏斗”) %>% hc_plotOptions(series = list(dataLabels = list( format = “{point。name} ({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())

桑基圖

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())

熱力圖

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 ‘’ + this。series。xAxis。categories[this。point。x] + ‘ sold
’ + this。point。value + ‘ items on
’ + this。series。yAxis。categories[this。point。y] + ‘’;}”)) %>% 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())

矩形樹圖

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())

R語言繪圖-highcharter包繪製可互動圖表