一、參數(shù)說明
echartr(data, x, y, <t>, <type>)
參數(shù)說明
二系宜、數(shù)據(jù)準備
從百度熱詞榜獲取熱詞照激,并解析為數(shù)據(jù)框,包含熱詞和頻數(shù)盹牧。
為此構建一個函數(shù)getBaiduHot俩垃,解析百度熱詞趨勢 網(wǎng)頁。
getBaiduHot <- function(url, top=30, HTMLencoding=NULL){
baiduhot <- paste0(readLines(url), collapse="")
charset <- gsub('^.+charset=([[:alnum:]-]+?)[^[:alnum:]-].+$', "\\1",
baiduhot)
if (is.null(HTMLencoding)) if (!is.null(charset)) HTMLencoding <- charset
baiduhot <- stringr::str_conv(baiduhot, HTMLencoding)
hotword <- gsub(".+?<a class=\"list-title\"[^>]+?>([^<>]+?)</a>.+?<span class=\"icon-(rise|fair|fall)\">(\\d+?)</span>.+?","\\1\t\\3\t\\2\t", baiduhot)
hotword <- enc2native(gsub("^(.+?)\t{4,}.+$","\\1", hotword))
hotword <- t(matrix(unlist(strsplit(hotword,"\t")), nrow=3))
hotword <- as.data.frame(hotword, stringsAsFactors=FALSE)
names(hotword) <- c("Keyword", "Freq", "Trend")
hotword$Freq <- as.numeric(hotword$Freq)
hotword <- hotword[order(hotword$Freq, decreasing=TRUE),]
return(hotword[1:top,])
}
hotword <- getBaiduHot("http://top.baidu.com/buzz?b=1", HTMLencoding='GBK')
knitr::kable(hotword)
三欢策、圖表展現(xiàn)
echartr(hotword,Keyword,Freq,type = 'wordCloud')
詞云圖
四吆寨、帶時間軸的圖表
- 數(shù)據(jù)處理
比較實時赏淌、今日和七日熱詞趨勢踩寇。
首先,獲取今日和七日兩個榜單的網(wǎng)頁并轉(zhuǎn)為數(shù)據(jù)框六水,合并俺孙。
hotword$t <- 'Realtime'
hotword1 <- getBaiduHot("http://top.baidu.com/buzz?b=341&fr=topbuzz_b1",
HTMLencoding = 'GBK')
hotword1$t <- 'Today'
hotword2 <- getBaiduHot("http://top.baidu.com/buzz?b=42&c=513&fr=topbuzz_b341",
HTMLencoding = 'GBK')
hotword2$t <- '7-days'
hotword <- do.call('rbind', list(hotword, hotword1, hotword2))
hotword$t <- factor(hotword$t, levels=c('Realtime', 'Today', '7-days'))
- 圖表展現(xiàn)
echartr(hotword, Keyword, Freq, t=t, type='wordCloud')
詞云圖
更多功能請戳官方鏈接:madlogos.github.io/recharts/Basic_Plots_01_Scatterplot_cn.html#-en