数据可视化理论与工具

政务大数据应用与分析 (80700673)

胡悦

清华大学社会科学学院

概要

可视化理论

  • 可视化基本功能
  • 可视化基本原则

可视化工具

  • 可视化编码
  • 可视化通道

可视化操作*

1 可视化理论

1.1 理解可视化功能

  • 记录信息
  • 分析推理
  • 证实假设
  • 交流思想

图之劣势

  • 制作费事
  • “不精确”
  • 形式不统一

表之优势

  • 容易制作
  • 发表常见
  • 有助后续

作图为了什么?

1.2 A Picture is Worth a Thousand Words

Even before he became president, Mr Trump had a history of controversial comments about women. His comments in a 2005 Access Hollywood tape about grabbing women “by the pussy” are perhaps the most notorious, and made headlines in 2016…..”The kind of things that he said about Hillary Clinton, Carly Fiorina, Elizabeth Warren, Heidi Cruz—the list goes on—he talks about women, any prominent, powerful woman, in the most DEMEANING of ways, trivializing them.”(Rogers 2016)

1.3 可视化与人类技术发展

Friendly and Wainer (2021)

Friendly and Wainer (2021)

1.4 可视化与政治学研究

Kastellec and Leoni (2007): APSR,AJPS和PA 2006年五刊

1.5 表转图

丑不丑?

下面的可视化做得如何

1.6 “毒图”(Viral Charts)

1.7 图的问题也是人的问题

可视化原则

1. 准确

1.8 如何识别“毒图” (Smith 2023, 2024)

  • 🚩 Startling trend
  • 🚩 Unlabeled/Dual axis
  • 🚩 No source
  • 👁️ Learn about old tricks
  • 👁️ Check comments
  • 👁️ Check where axis starts
  • 👁️ Check the unit of axes

1.9 做幅合格的图

如何可视化以下数据?

政党 得票率
A 38
B 34
C 24
D 4

蝉联junk chart之冠

蝉联junk chart之冠

1.10 什么是不合格的图

  • 使用多种图形组成要素重复呈现同一种信息
  • 虽未歪曲数据,但可能误导读者

Fung (2007)

Fung (2007)

可视化原则

1. 准确
2. 明确

1.11 Quiz 1

1.12 Call back

1.13 Quiz 2

Marks: Min, 25%, 50%, 75%, Max

1.14 下列数据如何可视化?

墨西哥各政党在大选中得票比例趋势
Party PRI PAN PRD Others
1991 64 17.8 8.2 10
1994 60 23.8 14.2 2
1997 47.8 24.2 25 3
2000 42.2 41.2 10 6.6
2003 44.8 30.2 19.4 5.6

和之前的数据有什么不同?

堆积图

1.15 其他选择

1.16 不是越简单越好

可视化原则

1. 准确
2. 明确
3. 详确

1.17 Quiz 3

补习班有效吗?
学生 原始成绩 补习班周数 进步分数
A 712 18 100
B 1105 4 57
C 690 7 80
D 687 27 191
E 725 2 5
F 1200 25 10
G 470 19 25
H 752 10 123

和之前的数据有什么不同

Pro

  • 散点图变异
  • 通过气泡大小呈现第三维度数据

Con

  • 气泡大小当接近时可能造成辨识困难,信息无法完全精确地传达

1.18 小结

  • 为什么可视化
    • 做得比表好
  • 可视化基本原则
    1. 准确:不歪曲量差和关系
    2. 明确:没有冗余元素
    3. 详确:表达想表达的信息
  • 可视化方式选择
    • 对于饼图,我们的态度是?
    • 对于柱状图,我们的态度是?
    • 对于点和线,我们的态度是?

Bonus

  • Tufte, Edward R. 2001[1983]. The Visual Display of Quantitative Information. Cheshire, Conn: Graphics Press.
  • Cleveland, William S. 1994. The Elements of Graphing Data. Murray Hill, N.J: Hobart Press.
  • Wilkinson, Leland. 2005. The Grammar of Graphics. New York: Springer-Verlag.

2 可视化工具

2.1 可视化编码

  • 数值(numeric)
    • 10厘米,17厘米,23厘米
  • 定序(ordinal)
    • 小,中,大
    • 周日,周一,周二……
  • 定类(nominal)
    • 苹果,桔子,香蕉……
  • 标记:点、线、面

  • 通道:位置、大小、形状、方向、色调、饱和度、亮度……

2.2 通道

2.3 通道选择

统计可视化原则

准确·明确·详确

类型(Type)

  • 是什么/在哪里(what/where)
  • 何种程度(how much)

有效性(Effectiveness)

  • 通道表现力符合属性的重要性
  • 准确性

表现力(Expressiveness)

  • 表达且仅表达数据的完整属性
  • 可辨性、可分离性、视觉突出

2.4 什么叫有效性

2.5 准确性(Accuracy)

\[\displaystyle S (I)=kI^{n},\]

k: proportionality constant that depends on the units used

2.6 可辨性(Discriminability)

2.7 可分离性(Separability)

2.8 视觉突出(Pop-out)

2.9 视觉突出基础:分主次

2.10 通过分组分主次

  • 分组会对大部分任务有效
  • 如果不能进行分组,则需要转换任务目标
  • 避免过多类别
  • 对数据的每个维度指定一种或一个阶层的颜色

2.11 色彩

  • 灰度有序 → 数值型
  • 色调无序 → 类别型

色彩设计基本原则

  • 避免过多颜色交错导致的杂乱无章
  • 注意控制同时对比(Simultaneous contrast)
  • 注意饱和度

Warning

入门容易,坑较深(没大必要入)

2.12 色彩选择

  • 大部分作图软件默认配色已经非常好(也就是说,没事儿别瞎改~)
  • 请在颜色选择过程中体现人文关怀
    • Color blind
    • Hard-copy person
  • 如果非要较真,辅助软件来选择合适的色阶

2.13 调色板

2.14 向大厂学习

2.15 色彩层次的主动性

2.16 小结

  • 编码和通道
  • 通道选择原则
    • 有效性
      • 准确性
    • 表现力
      • 可辨性
      • 可分离性
      • 视觉突出
  • 色彩
    • 相信专家

2.17 Take-Home Points

可视化理论

  • 抉择:精确 vs. 眼球
  • 可视化基本原则
    1. 准确
    2. 明确
    3. 详确

可视化工具

  • 编码和通道
  • 通道选择原则
    • 有效性
      • 准确性
    • 表现力
      • 可辨性
      • 可分离性
      • 视觉突出
  • 色彩
    • 相信专家

2.18 可视化的指导思想

我们进一步深化对中国式现代化的内涵和本质的认识,概括形成中国式现代化的中国特色、本质要求和重大原则,初步构建中国式现代化的理论体系,使中国式现代化更加清晰、更加科学、更加可感可行。(习近平 2024)

The art of any propagandist and agitator consists in his ability to find the best means of influencing any given audience, by presenting a definite truth, in such a way as to make it most convincing, most easy to digest, most graphic, and most strongly impressive (Lenin 1974).

2.19 参考文献

Friendly, Michael, and Howard Wainer. 2021. A History of Data Visualization and Graphic Communication. Harvard University Press.
Fung, Kaiser. 2007. “Adulterated Education.” Junk Charts. July 9, 2007.
Kastellec, Jonathan P., and Eduardo L. Leoni. 2007. “Using Graphs Instead of Tables in Political Science.” Perspectives on Politics 5 (4): 755–71. https://doi.org/10.1017/S1537592707072209.
Lenin, Vladimir. 1974. “The Slogans and Organisation of Social-Democratic Work Inside and Outside the Duma (1911).” In Lenin Collected Works, translated by Dora Cox, 331–41. Moscow: Progress Publishers.
Rogers, Tessa Berenson. 2016. “Watch Trump Call Clinton a ’Nasty Woman’.” Time: Politics Debates, October 20, 2016.
Smith, Noah. 2023. “How Not to Be Fooled by Viral Charts.” Noahpinion. September 13, 2023.
———. 2024. “How Not to Be Fooled by Viral Charts, Part 2.” Noahpinion. June 14, 2024.
习近平. 2024. “以中国式现代化全面推进强国建设、民族复兴伟业.” 求是, December 31, 2024.

3 附录:可视化操作

3.1 可视化平台

常见绘图工具

Tableau (Echarts)
Google Refine

Processing
D3 (JS)
R

常见可视化类型:

  • 描述图
  • 结果图

3.2 描述图:柱状图

ggplot(data = mydata, aes(x = Cut, y = Price)) +
  geom_bar(
    stat = "identity",
    width = 0.8,
    colour = "black",
    size = 0.25,
    fill = "#2ca25f",
    alpha = 1
  )

3.3 分组柱状图

ggplot(data = mydata2,
       aes(Catergory, value,
           fill = variable)) +
  geom_bar(
    stat = "identity",
    color = "black",
    position = position_dodge(),
    width = 0.7,
    size = 0.25
  ) +
  drhutools::scale_fill_gb() +
  theme_minimal() +
  ylim(0, 10) +
  theme(
    axis.title = element_text(size = 15, face = "plain", color = "black"),
    axis.text = element_text(size = 12, face = "plain", color = "black"),
    legend.title = element_text(size = 14, face = "plain", color = "black"),
    legend.background  = element_blank(),
    legend.position = c(0.88, 0.88)
  )

3.4 描述图:雷达图

car id value
数学 1 12
语文 2 2
英语 3 14
政治 4 20
编程 5 18
ggplot() +
  geom_polygon(
    data = mydata,
    aes(x = id, y = value),
    color = "black",
    fill = brewer.pal(7, "Set1")[1],
    alpha = 0.1
  ) +
  geom_point(
    data = mydata,
    aes(x = id, y = value),
    size = 5,
    shape = 21,
    color = 'black',
    fill = brewer.pal(7, "Set1")[1]
  ) +
  coord_radar() + 
  scale_x_continuous(breaks = label_data$id, labels = label_data$car) +
  ylim(0, 22) +
  theme_light() +
  theme(axis.text.x = element_text(
    size = 11,
    colour = "black"
  ))

3.5 描述图:趋势图(点-线)

ggplot(data = mydata, aes(x, y)) +
  geom_point(
    fill = "black",
    colour = "black",
    size = 3,
    shape = 21
  ) +
  geom_smooth(
    method = 'loess',
    span = 0.4,
    se = TRUE,
    colour = "#00A5FF",
    fill = "#00A5FF",
    alpha = 0.2
  ) + 
  scale_y_continuous(breaks = seq(0, 125, 25)) +
  theme(
    text = element_text(size = 15, color = "black"),
    plot.title = element_text(
      size = 15,
      family = "myfont",
      hjust = .5,
      color = "black"
    ),
    legend.position = "none"
  )

3.6 结果图:回归系数

(1) (2) (3)
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
(Intercept) 43.540*** 37.363*** 37.742***
(4.860) (5.972) (6.023)
wt -3.793** -3.714** -3.310**
(1.082) (1.048) (1.159)
cyl -1.784** -1.119 -1.086
(0.614) (0.714) (0.719)
disp 0.007 0.014 0.013
(0.012) (0.012) (0.012)
gear -0.490 0.679 0.120
(0.790) (1.035) (1.234)
hp -0.028 -0.029+
(0.017) (0.017)
am 1.465
(1.743)
Num.Obs. 32 32 32
R2 0.835 0.851 0.855
R2 Adj. 0.811 0.822 0.820
AIC 159.1 157.8 158.9
BIC 167.9 168.1 170.6
Log.Lik. -73.553 -71.906 -71.460
F 34.149 29.723 24.608
RMSE 2.41 2.29 2.26

3.7 Dot-whisker

library(dotwhisker)

dwplot(list(m1, m2, m3), show_stats = TRUE)

3.8 More delicate version

3.9 Small-multiple

3.10 结果图:交互效应(interplot)

data(mtcars) 
m_cyl <- lm(mpg ~ wt * cyl, data = mtcars)

library(interplot)
interplot(m = m_cyl, var1 = "cyl", var2 = "wt")

3.11 More delicate

Numeric Conditioner

Numeric Conditioner

Categorical Conditioner

Categorical Conditioner

3.12 描述性统计

Unique Missing Pct. Mean SD Min Median Max Histogram
mpg 25 0 20.1 6.0 10.4 19.2 33.9
cyl 3 0 6.2 1.8 4.0 6.0 8.0
disp 27 0 230.7 123.9 71.1 196.3 472.0
hp 22 0 146.7 68.6 52.0 123.0 335.0
drat 22 0 3.6 0.5 2.8 3.7 4.9
wt 29 0 3.2 1.0 1.5 3.3 5.4
qsec 30 0 17.8 1.8 14.5 17.7 22.9
carb 6 0 2.8 1.6 1.0 2.0 8.0
N %
vs FALSE 18 56.2
TRUE 14 43.8
am FALSE 19 59.4
TRUE 13 40.6
gear 3 15 46.9
4 12 37.5
5 5 15.6

3.13 回归表格

library(modelsummary)
modelsummary(list(m1, m2, m3), stars = TRUE) |>
  style_tt(fontsize = 0.4)
(1) (2) (3)
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
(Intercept) 43.540*** 37.363*** 37.742***
(4.860) (5.972) (6.023)
wt -3.793** -3.714** -3.310**
(1.082) (1.048) (1.159)
cyl -1.784** -1.119 -1.086
(0.614) (0.714) (0.719)
disp 0.007 0.014 0.013
(0.012) (0.012) (0.012)
gear -0.490 0.679 0.120
(0.790) (1.035) (1.234)
hp -0.028 -0.029+
(0.017) (0.017)
am 1.465
(1.743)
Num.Obs. 32 32 32
R2 0.835 0.851 0.855
R2 Adj. 0.811 0.822 0.820
AIC 159.1 157.8 158.9
BIC 167.9 168.1 170.6
Log.Lik. -73.553 -71.906 -71.460
F 34.149 29.723 24.608
RMSE 2.41 2.29 2.26