HighChart的原身是由javascrip在做視覺化時轉化而來,所以爬文的時候找到的resource都是該語言的相關資訊,官方介紹我列於下方,而這個套件其主要目的是為了使Rcoder在coding時能夠簡單的用R語法撰寫出互動型態的視覺畫圖形,並且與pipe-operator可以達到接技的效果,並與dygraph達到同樣的效果,並支援rmarkdown、shiny等做進階視覺化。
> #install.packages("highcharter")
> #devtools::install_github("jbkunst/highcharter")
以下會在各種不同圖形開始前做的使用說明,與一些畫圖前的前置作業,並對整體流程做個簡的的說明,方便各位快速上手
> ## 載入套件
> library(tidyr)
> library(dplyr)
> library(highcharter)
> ## 我自行將highchart()解讀為將在這個架構下進行繪圖
> ## 所以我會先另一個新物件為這個function
> hc <- highchart()
打個岔,說明一下為何要從pie開始chart起!?
由於工作關係,常常使用ggplot2進行繪圖但ggplot2在pie的部份因為套件作者的某些原因致使開發並不frendly,所以我在此以pie作為第一部分,彰顯我的不滿!XD
>
> data = mtcars
> data %>% group_by(carb) %>% summarise(total=100*round(length(vs)/32,3)) -> temp
> highchart() %>% hc_title(text = "立筠好可愛") %>% hc_subtitle(text = "大眼睛又大肚子") %>%
+ hc_add_series_labels_values(temp$carb, temp$total,
+ colors = substr(terrain.colors(15)[4:9],1,7),type = "pie",
+ name = "carb", colorByPoint = TRUE, center = c('50%', '50%'),
+ size = 300, dataLabels = list(enabled = T)) %>%
+ hc_tooltip(pointFormat = "{point.y}%")
> ## 因為只是示範,所以我不多加闡述畫出來的東西代表的意義
> data %>% group_by(gear) %>% summarise(total=100*round(length(vs)/32,3)) -> temp1
> ## 只需將
> highchart() %>% hc_title(text = "立筠好可愛") %>% hc_subtitle(text = "大眼睛又大肚子") %>%
+ hc_add_series_labels_values(temp$carb, temp$total,
+ colors = substr(terrain.colors(10)[5:10], 0 , 7), type = "pie",
+ name = "carb", colorByPoint = TRUE, center = c('20%', '50%'),
+ size = 250, dataLabels = list(enabled = T)) %>%
+ hc_tooltip(pointFormat = "{point.y}%") %>%
+ hc_add_series_labels_values(temp1$gear, temp1$total,
+ colors = substr(terrain.colors(23)[21:23], 0 , 7), type = "pie",
+ name = "gear", colorByPoint = TRUE, center = c('80%', '50%'),
+ size = 250, dataLabels = list(enabled = T))
> ## 在這裡我讓她隨機選色
> highchart() %>% hc_title(text = "立筠好可愛") %>% hc_subtitle(text = "大眼睛又大肚子") %>%
+ hc_add_series_labels_values(temp$carb, temp$total,
+ colors = substr(sample(terrain.colors(70),6),0,7), type = "column",
+ name = "carb", colorByPoint = TRUE, center = c('20%', '20%'),
+ size = 100, dataLabels = list(enabled = T)) %>%
+ hc_tooltip(pointFormat = "{point.y}%")
> ## 這邊我們自己建一個dataframe做為示範
> temp = data.frame(月份=1:12,多益分數2015=c(450,600,650,655,400,700,750,770,800,810,800,830))
> temp1 = data.frame(月份=1:12,多益分數2016=c(480,590,700,700,430,780,720,780,810,800,860,880))
> ##
> highchart() %>%
+ hc_title(text = "小0敲可愛") %>%
+ hc_xAxis(categories =temp$月份) %>%
+ hc_add_series(name = "多益分數2015", data = temp$多益分數,color = 'orange') %>%
+ hc_add_series(name = "多益分數2016", data = temp1$多益分數2016,color = 'green') %>%
+ hc_chart(type = "column",
+ options3d = list(enabled = TRUE, beta = 15, alpha = 15)) %>%
+ hc_tooltip(crosshairs = TRUE, backgroundColor = "#FCFFC5",
+ shared = TRUE, borderWidth = 5) %>%
+ hc_legend(align = "center", verticalAlign = "",
+ layout = "vertical", x = 0, y = 100) %>%
+ hc_yAxis(title = list(text = "count"),min=400, max = 990)
> highchart() %>%
+ hc_title(text = "Scatter chart with color") %>%
+ hc_add_series_scatter(mtcars$wt, mtcars$mpg,
+ # 以某變數作為類別訂定顏色
+ color = mtcars$hp)
> highchart() %>%
+ hc_title(text = "Scatter chart with size and color") %>%
+ hc_add_series_scatter(mtcars$wt, mtcars$mpg,
+ mtcars$drat, mtcars$hp)
> ## 珍貴的體重資料?!
> data = data.frame(年度=2005:2012,立筠體重=c(58,60,65,59,66,55,70,71),小0體重=c(50,51,53,55,49,55,60,65),屁宜體重=c(70,71,69,68,72,73,66,80))
> ##
> highchart() %>%
+ hc_title(text = "歷年小0vs立筠體重分布圖") %>%
+ hc_xAxis(categories = data$年度) %>%
+ hc_add_series(name = "立筠體重", data =data$立筠體重) %>%
+ hc_add_series(name = "小0體重", data = data$小0體重,color="red") %>% #顏色color
+ hc_add_series(name = "屁宜體重", data = data$屁宜體重,color="gold")
> data = mtcars
> data %>% group_by(carb) %>% summarise(total=100*round(length(vs)/32,3)) -> temp2
> ## 這邊我們自己建一個dataframe做為示範
> temp = data.frame(月份=1:12,多益分數2015=c(450,600,650,655,400,700,750,770,800,810,800,830))
> temp1 = data.frame(月份=1:12,多益分數2016=c(480,590,700,700,430,780,720,780,810,800,860,880))
> ##
> highchart() %>%
+ hc_title(text = "小0敲可愛") %>%
+ hc_xAxis(categories =temp$月份) %>%
+ hc_add_series(name = "多益分數2015", data = temp$多益分數,color = 'orange') %>%
+ hc_add_series(name = "多益分數2016", data = temp1$多益分數2016,color = 'green') %>%
+ hc_chart(type = "column",
+ options3d = list(enabled = TRUE, beta = 15, alpha = 15)) %>%
+ hc_tooltip(crosshairs = TRUE, backgroundColor = "#FCFFC5",
+ shared = TRUE, borderWidth = 5) %>%
+ hc_legend(align = "center", verticalAlign = "",
+ layout = "vertical", x = 0, y = 100) %>%
+ hc_yAxis(title = list(text = "count"),min=400, max = 990) %>%
+ hc_add_series_labels_values(temp2$carb, temp2$total,
+ ### 以上只是複製上面的例子接下來只要加上一條指令,就可以順利裝上
+ colors = substr(terrain.colors(15)[4:9],1,7),type = "pie",
+ name = "carb", colorByPoint = TRUE, center = c('20%', '20%'),
+ size = 200, dataLabels = list(enabled = T)) %>%
+ hc_tooltip(pointFormat = "{point.y}%")