前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >子弹图(Bullet chart)绘制很难吗?绘制技巧整理送你~~

子弹图(Bullet chart)绘制很难吗?绘制技巧整理送你~~

作者头像
DataCharm
发布2021-09-17 15:02:58
1.1K0
发布2021-09-17 15:02:58
举报

今天小编给大家介绍一下子弹图(Bullet chart) 的绘制方法,主要内容包括:

  • 子弹图(Bullet chart)简单介绍
  • 子弹图(Bullet chart)的R绘制技巧
  • 子弹图(Bullet chart)的Python绘制技巧

子弹图(Bullet chart)简单介绍

子弹图(Bullet chart) 给大多数据人的第一印象可能就只是简单的柱形图叠加,但其所使用的场景和表达的含义却远远超过柱形图,特别是在分析环比、同比时。子弹图的主要构成元素和可视化表示如下:

  • 文字标签和主体条柱
  • 刻度量表
  • 主要标记标识
  • 用于对比的标识(可选)
  • 定性范围标识,一般2-5个即可。

标准子弹图(Bullet chart)样例表示

文末小编还给大家准备了一个介绍子弹图(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

而绘制其他多类别数据的方法也都是类似的,小伙伴们可自行测试。

R-bulletchartr包绘制

R-bulletchartr包则是专门用于绘制子弹图(Bullet chart) 的第三方包,使用也非常方便,且其对绘图数据的格式要求也比较低,详细介绍如下:

「样例一」:bullet_chart()

  • 数据预览:
  • 绘制代码
## load example data
load(read_example("bc_ex.rda"))

bullet_chart(dataframe = bc_ex)

Example Of bullet_chart()

「样例二」:bullet_chart_wide()

  • 数据预览:
  • 绘制代码:
bullet_chart_wide(file_name = read_example("Indicators_Targets_ext.xlsx"),
                  cal_type = "cal")

Example Of bullet_chart_wide()

「样例三」:bullet_chart_vline()

  • 数据预览:
  • 绘制代码:
bullet_chart_vline(file_name = read_example("Indicators_Targets_ext.xlsx"))

Example Of bullet_chart_vline()

更多R-bulletchartr包其他绘图函数和参数设置可参考R-bulletchartr包[1]

Python绘制

Python-matplotlib 绘制

之前小编有转载过一篇文章专门介绍使用Matplotlib绘制子弹图(Python-Matplotlib 子弹图绘制),作者是定义bulletgraph()函数进行绘制,感兴趣的小伙伴可以看下,这里就不再描述。

Python-plotly 绘制

作为Python交互式可视化绘制强大工具之一,plotly库可以绘制多类图表,当然也包括子弹图(Bullet chart),如下:

「样例一」:

import plotly.graph_objects as go

fig = 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 go

fig = 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 plotly

Python-plotly库绘图功能还是非常强大的,后期公众号会推出系列专题。更多plotly库绘制子弹图(Bullet chart) 可参考plotly库绘制子弹图[2]。

总结

今天的这篇推文,小编简单的介绍了子弹图(Bullet chart) 的R和Python绘制方法,希望小伙伴们可以选择自己喜欢的工具进行绘制哈~

子弹图PDF文件获取

整理不易,感谢大家帮忙分享,关注本公众号(DataCharm)然后在公众号后台发送 子弹图 即可获知免费下载链接。

参考资料

[1]

R-bulletchartr包官网: https://acdivocatech.github.io/bulletchartr/index.html。

[2]

plotly库绘制子弹图: https://plotly.com/python/bullet-charts/。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-09-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 DataCharm 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 子弹图(Bullet chart)简单介绍
  • 子弹图(Bullet chart)的R绘制技巧
    • R-ggplot2包绘制
      • R-bulletchartr包绘制
      • Python绘制
        • Python-matplotlib 绘制
          • Python-plotly 绘制
          • 总结
          • 子弹图PDF文件获取
            • 参考资料
            相关产品与服务
            图数据库 KonisGraph
            图数据库 KonisGraph(TencentDB for KonisGraph)是一种云端图数据库服务,基于腾讯在海量图数据上的实践经验,提供一站式海量图数据存储、管理、实时查询、计算、可视化分析能力;KonisGraph 支持属性图模型和 TinkerPop Gremlin 查询语言,能够帮助用户快速完成对图数据的建模、查询和可视化分析。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档