進階可視化效果
什麼是hexbin binning?
如果在同一個地方有很多點(overplotting),我們可以使用hexbin包。六邊形面元劃分是一種二進制直方圖,對大數量級結構的資料集的可視化非常有用。下面是代碼:
>library(hexbin)
>a=hexbin(diamonds$price,diamonds$carat,xbins=40)
>library(rcolorbrewer)
>plot(a)
我們也可以建立一個調色闆,然後用hexbin繪圖功能以獲得更好的視覺效果。下面是代碼:
>rf <- colorramppalette(rev(brewer.pal(40,'set3')))
>hexbinplot(diamonds$price~diamonds$carat, data=diamonds, colramp=rf)
馬賽克拼圖
馬賽克拼圖可以通過資料所占據的面積大小來有效地顯示分類資料的相對比例。
> data(haireyecolor)
> mosaicplot(haireyecolor)
熱圖
熱圖使你能夠以兩個次元為軸,顔色的強度為第三個次元來進行探索性的資料分析。然而,你需要将資料集轉化成矩陣形式。下面是代碼:
> heatmap(as.matrix(mtcars))
您也可以使用image()指令做這種類型的可視化:
> image(as.matrix(b[2:7]))
如何彙總大量資料?
您可以使用tabplot包中的tableplot功能,快速彙總大量資料
地圖可視化
您可以用下面的代碼直接從github安裝leaflet。
devtools::install_github("rstudio/leaflet")
制作上述地圖的代碼是非常簡單的:
library(magrittr)
library(leaflet)
m <- leaflet() %>%
addtiles() %>% # add default openstreetmap map tiles
addmarkers(lng=77.2310, lat=28.6560, popup="the delicious food of chandni chowk")
m # print the map
3維圖
用r語言的的功能讓人閃瞎眼的最簡單的方法之一是通過建立一張3維圖,而不需要用r語言寫一行代碼,并且在3分鐘内就能完成。這樣要求是不是太過分呢?
我們使用r commander包作為圖形使用者界面(gui)。操作步驟如下:
1. 隻需安裝rcmdr包
2. 使用來自圖中的3d繪圖選項
下面的代碼不是使用者輸入的,是自動生成的。
便簽:當我們交換圖的坐标軸時,您應該看到有着相應代碼的圖,我們是如何使用xlab和ylab來傳遞軸标簽,圖示題用main函數,顔色是col參數。
>data(iris, package="datasets")
>scatter3d(petal.width~petal.length+sepal.length|species, data=iris, fit="linear"
>residuals=true, parallel=false, bg="black", axis.scales=true, grid=true, ellipsoid=false)
您還可以使用lattice包來做3維圖。lattice也可以用于xyplot。下面是代碼:
>attach(iris)# 3d scatterplot by factor level
>cloud(sepal.length~sepal.width*petal.length|species, main="3d scatterplot by species")
>xyplot(sepal.width ~ sepal.length, iris, groups = iris$species, pch= 20)
相關圖(guis)
相關圖幫助我們把相關矩陣内的資料可視化。下面是代碼:
> cor(iris[1:4])
sepal.length sepal.width petal.length petal.width
sepal.length 1.0000000 -0.1175698 0.8717538 0.8179411
sepal.width -0.1175698 1.0000000 -0.4284401 -0.3661259
petal.length 0.8717538 -0.4284401 1.0000000 0.9628654
petal.width 0.8179411 -0.3661259 0.9628654 1.0000000
> corrgram(iris)
r語言有三個主要的gui包。rccommander和kmggplot及rattle用于資料挖掘,deducer用于資料可視化。這些有助于自動完成多個工作。
結束語
我真的享受寫這篇文章,r語言所用到的各種方式使得它成為世界上最好的資料可視化軟體。python也許在seaborn(譯者注:seaborn是python中基于matplotlib的統計繪圖子產品)和ggplot(譯者注:ggplot是用于繪圖的r語言擴充包在python的移植)上獲得進展,而沒有什麼能打敗在統計資料可視化上擁有絕對數量巨大軟體包的r語言。
我在本文中已經讨論了各種形式的可視化,是通過用r語言程式設計實作從基礎到進階的有助于展示資料的圖表。
原文釋出時間為:2015-07-27
本文來自雲栖社群合作夥伴“大資料文摘”,了解相關資訊可以關注“bigdatadigest”微信公衆号