子弹图(Bullet chart)绘制很难吗?绘制技巧整理送你~~
(好久沒更新可視化繪制技巧了,繼續(xù)走著)
今天小編給大家介紹一下子彈圖(Bullet chart) 的繪制方法,主要內容包括:
子彈圖(Bullet chart)簡單介紹
子彈圖(Bullet chart)的R繪制技巧
子彈圖(Bullet chart)的Python繪制技巧
子彈圖(Bullet chart)簡單介紹
子彈圖(Bullet chart) 給大多數(shù)據(jù)人的第一印象可能就只是簡單的柱形圖疊加,但其所使用的場景和表達的含義卻遠遠超過柱形圖,特別是在分析環(huán)比、同比時。子彈圖的主要構成元素和可視化表示如下:
文字標簽和主體條柱
刻度量表
主要標記標識
用于對比的標識(可選)
定性范圍標識,一般2-5個即可。
文末小編還給大家準備了一個介紹子彈圖(Bullet chart) 設計的pdf文件,感興趣的可以學習下(下載方式文末給出)~,更多關于子彈圖(Bullet chart)的介紹,小伙伴可自行探索哈~
子彈圖(Bullet chart)的R繪制技巧
得益于R語言在可視化繪制上的便捷性,小編這里分別提供R-ggplot2和R-bulletchartr包繪制子彈圖(Bullet chart) 的技巧分享。
R-ggplot2包繪制
R-ggplot2包繪制子彈圖(Bullet chart) 還是非常方便的,如下:
「樣例一」:
library(tidyverse) tibble(name?=?"Example",quant_value?=?75,qualitative?=?100 )?%>%?ggplot(aes(x?=?quant_value,?y?=?name))?+geom_col(aes(x?=?qualitative),?fill?=?"grey")?+geom_col(width?=?0.5,?fill?=?"black")?+coord_cartesian(ylim?=?c(0.3,?1.7))?+theme_minimal()?+theme(panel.grid.major.y?=?element_blank())Example01 Of Bullet chart in ggplot2「樣例二」:Add a target
ex_df?<-?bind_rows(tibble(name?=?rep("Ex?1",?2),group?=?c("Qualitative",?"Measure"),color?=?c("grey",?"black"),value?=?c(100,?75),width?=?c(0.9,?0.5),target?=?rep(82,?2),ymin?=?rep(0.7,?2),ymax?=?rep(1.3,?2)),tibble(name?=?rep("Ex?2",?2),group?=?c("Qualitative",?"Measure"),color?=?c("grey",?"black"),value?=?c(88,?64),width?=?c(0.9,?0.5),target?=?rep(77,?2),ymin?=?rep(1.7,?2),ymax?=?rep(2.3,?2)) )ex_df?%>%?ggplot(aes(x?=?value,?y?=?name,?fill?=?color))?+geom_col(width?=?c(0.9,?0.5,?0.9,?0.5))?+geom_linerange(aes(x?=?target,?ymin?=?ymin,?ymax?=?ymax),size?=?2,?color?=?"red")?+coord_cartesian(ylim?=?c(0.3,?2.7))?+scale_fill_identity()?+theme_minimal()?+theme(panel.grid.major.y?=?element_blank())Example02 Of Bullet chart in ggplot2而繪制其他多類別數(shù)據(jù)的方法也都是類似的,小伙伴們可自行測試。
R-bulletchartr包繪制
R-bulletchartr包則是專門用于繪制子彈圖(Bullet chart) 的第三方包,使用也非常方便,且其對繪圖數(shù)據(jù)的格式要求也比較低,詳細介紹如下:
「樣例一」:bullet_chart()
數(shù)據(jù)預覽:
繪制代碼
「樣例二」:bullet_chart_wide()
數(shù)據(jù)預覽:
繪制代碼:
「樣例三」:bullet_chart_vline()
數(shù)據(jù)預覽:
繪制代碼:
更多R-bulletchartr包其他繪圖函數(shù)和參數(shù)設置可參考R-bulletchartr包[1]
Python繪制
Python-matplotlib 繪制
之前小編有轉載過一篇文章專門介紹使用Matplotlib繪制子彈圖(Python-Matplotlib 子彈圖繪制),作者是定義bulletgraph()函數(shù)進行繪制,感興趣的小伙伴可以看下,這里就不再描述。
Python-plotly 繪制
作為Python交互式可視化繪制強大工具之一,plotly庫可以繪制多類圖表,當然也包括子彈圖(Bullet chart),如下:
「樣例一」:
import?plotly.graph_objects?as?gofig?=?go.Figure(go.Indicator(mode?=?"number+gauge+delta",?value?=?220,domain?=?{'x':?[0,?1],?'y':?[0,?1]},delta?=?{'reference':?280,?'position':?"top"},title?=?{'text':"<b>Profit</b><br><span style='color:?gray;?font-size:0.8em'>U.S.?$</span>",?'font':?{"size":?14}},gauge?=?{'shape':?"bullet",'axis':?{'range':?[None,?300]},'threshold':?{'line':?{'color':?"red",?'width':?2},'thickness':?0.75,?'value':?270},'bgcolor':?"white",'steps':?[{'range':?[0,?150],?'color':?"cyan"},{'range':?[150,?250],?'color':?"royalblue"}],'bar':?{'color':?"darkblue"}})) fig.update_layout(height?=?250) fig.show()Example01 Of Bullet chart in plotly「樣例二」:Multi Bullet
import?plotly.graph_objects?as?gofig?=?go.Figure()fig.add_trace(go.Indicator(mode?=?"number+gauge+delta",?value?=?180,delta?=?{'reference':?200},domain?=?{'x':?[0.25,?1],?'y':?[0.08,?0.25]},title?=?{'text':?"Revenue"},gauge?=?{'shape':?"bullet",'axis':?{'range':?[None,?300]},'threshold':?{'line':?{'color':?"black",?'width':?2},'thickness':?0.75,'value':?170},'steps':?[{'range':?[0,?150],?'color':?"gray"},{'range':?[150,?250],?'color':?"lightgray"}],'bar':?{'color':?"black"}}))fig.add_trace(go.Indicator(mode?=?"number+gauge+delta",?value?=?35,delta?=?{'reference':?200},domain?=?{'x':?[0.25,?1],?'y':?[0.4,?0.6]},title?=?{'text':?"Profit"},gauge?=?{'shape':?"bullet",'axis':?{'range':?[None,?100]},'threshold':?{'line':?{'color':?"black",?'width':?2},'thickness':?0.75,'value':?50},'steps':?[{'range':?[0,?25],?'color':?"gray"},{'range':?[25,?75],?'color':?"lightgray"}],'bar':?{'color':?"black"}}))fig.add_trace(go.Indicator(mode?=?"number+gauge+delta",?value?=?220,delta?=?{'reference':?200},domain?=?{'x':?[0.25,?1],?'y':?[0.7,?0.9]},title?=?{'text'?:"Satisfaction"},gauge?=?{'shape':?"bullet",'axis':?{'range':?[None,?300]},'threshold':?{'line':?{'color':?"black",?'width':?2},'thickness':?0.75,'value':?210},'steps':?[{'range':?[0,?150],?'color':?"gray"},{'range':?[150,?250],?'color':?"lightgray"}],'bar':?{'color':?"black"}})) fig.update_layout(height?=?400?,?margin?=?{'t':0,?'b':0,?'l':0})fig.show()Example02 Of Bullet chart in plotlyPython-plotly庫繪圖功能還是非常強大的,后期公眾號會推出系列專題。更多plotly庫繪制子彈圖(Bullet chart) 可參考plotly庫繪制子彈圖[2]。
總結
今天的這篇推文,小編簡單的介紹了子彈圖(Bullet chart) 的R和Python繪制方法,希望小伙伴們可以選擇自己喜歡的工具進行繪制哈~
參考資料
[1]
R-bulletchartr包官網(wǎng): https://acdivocatech.github.io/bulletchartr/index.html。
[2]plotly庫繪制子彈圖: https://plotly.com/python/bullet-charts/。
E?N?D
各位伙伴們好,詹帥本帥假期搭建了一個個人博客和小程序,匯集各種干貨和資源,也方便大家閱讀,感興趣的小伙伴請移步小程序體驗一下哦!(歡迎提建議)
推薦閱讀
牛逼!Python常用數(shù)據(jù)類型的基本操作(長文系列第①篇)
牛逼!Python的判斷、循環(huán)和各種表達式(長文系列第②篇)
牛逼!Python函數(shù)和文件操作(長文系列第③篇)
牛逼!Python錯誤、異常和模塊(長文系列第④篇)
總結
以上是生活随笔為你收集整理的子弹图(Bullet chart)绘制很难吗?绘制技巧整理送你~~的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 全球最优秀的14位程序员
- 下一篇: Scrapy爬取整个美女网爬下来,要多少