大家快来看,这个柱状图会下腰!如何使用 R 语言绘制径向柱状图

大家快来看,这个柱状图会下腰!如何使用 R 语言绘制径向柱状图

想要让柱状图不同高度的柱条看起来差异大一些怎么办?你可以试试这款径向条形图!

我们先画一幅普通的堆叠柱状图:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
library(tidyverse)
library(highcharter)

highchart() %>%
hc_chart(type = "column") %>%
hc_xAxis(tickInterval = 1,
lineWidth = 0,
categories = c('Norway', 'United States', 'Germany', 'Canada', 'Austria', 'Sweden', 'Switzerland', 'Russia', 'Netherlands', 'Finland')) %>%
hc_yAxis(
lineWidth = 0,
tickInterval = 25,
reversedStacks = F,
endOnTick = T,
showLastLabel = T
) %>%
hc_plotOptions(
column = list(
stacking = 'normal',
borderWidth = 0,
pointPadding = 0,
groupPadding = 0.15
)
) %>%
hc_add_series(data = c(132, 105, 92, 73, 64, 57, 55, 47, 45, 43),
name = "金牌") %>%
hc_add_series(data = c(125, 110, 86, 64, 81, 46, 46, 38, 44, 63),
name = "银牌") %>%
hc_add_series(data = c(111, 90, 60, 62, 87, 55, 52, 35, 41, 61),
name = "铜牌") %>%
hc_title(text = '冬季奥运会金牌榜 (TOP 10)') %>%
hc_tooltip(outside = TRUE) %>%
hc_pane(size = '85%', endAngle = 270) %>%
hc_add_theme(hc_theme_sandsignika(
chart = list(
divBackgroundImage = NULL,
style = list(background = "url(https://www.highcharts.com/samples/graphics/sand.png)",
fontFamily = "Source Han Serif")
)
)) %>%
hc_credits(enabled = T,
text = "©RStata 绘制",
href = "https://tidyfriday.cn")

可以看到,这十个国家的各种奖牌的数量差距不是很大,假如我想将这种差距方法该怎么做呢?可以试试径向柱形图:

这个时候各个柱条看起来是不是差异巨大!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
highchart() %>% 
hc_chart(type = "column",
inverted = TRUE,
polar = TRUE) %>%
hc_xAxis(
tickInterval = 1,
labels = list(
align = 'right',
useHTML = TRUE,
allowOverlap = TRUE,
step = 1,
y = 4,
x = -4,
style = list(
fontSize = '12px'
)
),
lineWidth = 0,
categories = c(
'Norway <span class="f16"><span id="flag" class="flag no"></span></span>',
'United States <span class="f16"><span id="flag" class="flag us"></span></span>',
'Germany <span class="f16"><span id="flag" class="flag de"></span></span>',
'Canada <span class="f16"><span id="flag" class="flag ca"></span></span>',
'Austria <span class="f16"><span id="flag" class="flag at"></span></span>',
'Sweden <span class="f16"><span id="flag" class="flag se"></span></span>',
'Switzerland <span class="f16"><span id="flag" class="flag ch"></span></span>',
'Russia <span class="f16"><span id="flag" class="flag ru"></span></span>',
'Netherlands <span class="f16"><span id="flag" class="flag nl"></span></span>',
'Finland <span class="f16"><span id="flag" class="flag fi"></span></span>'
)
) %>%
hc_yAxis(
lineWidth = 0,
tickInterval = 25,
reversedStacks = F,
endOnTick = T,
showLastLabel = T
) %>%
hc_plotOptions(
column = list(
stacking = 'normal',
borderWidth = 0,
pointPadding = 0,
groupPadding = 0.15
)
) %>%
hc_add_series(data = c(132, 105, 92, 73, 64, 57, 55, 47, 45, 43),
name = "金牌") %>%
hc_add_series(data = c(125, 110, 86, 64, 81, 46, 46, 38, 44, 63),
name = "银牌") %>%
hc_add_series(data = c(111, 90, 60, 62, 87, 55, 52, 35, 41, 61),
name = "铜牌") %>%
hc_title(text = '冬季奥运会金牌榜 (TOP 10)') %>%
hc_tooltip(outside = TRUE) %>%
hc_pane(size = '85%', endAngle = 270) %>%
hc_add_theme(hc_theme_sandsignika(
chart = list(
divBackgroundImage = NULL,
style = list(background = "url(https://www.highcharts.com/samples/graphics/sand.png)",
fontFamily = "Source Han Serif")
)
)) %>%
hc_exporting(enabled = T) %>%
hc_credits(enabled = T,
text = "©RStata 绘制",
href = "https://tidyfriday.cn") -> hc

library(htmltools)
browsable(
tagList(list(
tags$head(
tags$link(href = "https://github.com/downloads/lafeber/world-flags-sprite/flags16.css", rel = "stylesheet")
),
hc
))
)

这里的国旗我是使用 span 标签添加的,需要额外加载 flags16.css 才能渲染出来国旗,所以我使用了 htmltools 包的相关函数为创建的 hc 绑定 flags16.css。

阅读更多:https://mp.weixin.qq.com/s/pYjBhCt72a8Moe38jxKVIg

大家快来看,这个柱状图会下腰!如何使用 R 语言绘制径向柱状图

https://tidyfriday.cn/posts/16509/

作者

Painter

发布于

2021-02-28

更新于

2021-05-31

许可协议

评论