--- disqus: ahb0222 GA : G-VF9ZT413CG --- # R語言ggplot2散佈圖衍生圖-2D bin plot > [color=#40f1ef][name=LHB阿好伯, 2021/03/27][:earth_africa:](https://www.facebook.com/LHB0222/) ###### tags: `R` `可視化` `ggplot2`  [TOC] # 介紹 在[ggplot2推薦圖形配置](/L6Y0JBFwSHWl5RIk_Tjmsg)中繪製了最經典的散佈圖  在軍中讀書的看到了散佈圖的衍生圖 類似於熱點圖有方形與六角蜂巢型兩種 # 2D bin plot_geom_bin2d() 首先先繪製ggplot2中內建的方形圖 只需要加上`geom_bin2d()`即可 ```r= library(ggplot2) p1 <- ggplot(data = diamonds, mapping = aes(x = price, y = carat, fill = clarity)) #設定資料集與映射資料 p1 + geom_bin2d() ```  ## 更改解析度 可以設定**bins**改變在垂直和水平方向方格的數量 預設為30可以改成50會有更好的呈現結果 ```r= p1 + geom_bin2d(bins = 50) ```  或是也可以設定**binwidth**更改垂直和水平方向上的長度 ``` p1 + geom_bin2d(binwidth = c(200, 0.1)) ```  # 六角蜂巢型_geom_hex() 六角蜂巢型則需要`hexbin`套件 ```r= install.packages("hexbin") library(hexbin) p1 + geom_hex(bins = 50) ```  最後再加上[ggplot2推薦圖形配置](/L6Y0JBFwSHWl5RIk_Tjmsg)文章中所說的設定修改一下圖形配置就可以畫出一張好看的圖片 ```r= library(ggeasy) library(extrafont) #font_import() #載入字形 #loadfonts(device = "win") p1 + geom_hex(bins = 50) + theme_bw() + #更改主題 theme(text=element_text(family = "Times New Roman")) + #修改字體 easy_all_text_size(18) ```  # 延伸合併圖形 最後使用ggplot2甚至能將兩種圖利用更改透明度的方式將散步圖合併再一起 當然這樣做在這裡的意義並不大 也可以看到相較於原本的散佈圖使用蜂巢圖會損失掉一些細節 例如在一個區域上可能有數個以上不等的數據點 但在繪製的蜂巢圖片上可能看不出差異 所以在使用上也需要注意到 ```r= p1 + geom_hex(bins = 50, alpha=0.2) + theme_bw() + #更改主題 theme(text=element_text(family = "Times New Roman")) + #修改字體 easy_all_text_size(18) + #更改字體大小 geom_point(aes(colour = clarity),alpha=0.4) #加上散佈圖 ```  🌟全文可以至下方連結觀看或是補充 https://hackmd.io/@LHB-0222/2Dbinplot 全文分享至 https://www.facebook.com/LHB0222/ https://www.instagram.com/ahb0222/ 有疑問想討論的都歡迎於下方留言 喜歡的幫我分享給所有的朋友 \o/ 有所錯誤歡迎指教 # [:page_with_curl: 全部文章列表](https://hackmd.io/@LHB-0222/AllWritings) 
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up