今天我们来一起学习一下 R 语言中交互式柱状图的绘制。

还有这个:

目 录
- 1. 加载R包
- 2. 构造绘图数据
- 3. 基础柱形图
- 4. 百分比堆叠柱状图
- 5. 堆叠柱形图
- 6. 分组堆叠柱状图
- 7. 角锥柱形图
- 8. 3D 圆柱图
- 9. 水平柱状图
1. 加载R包
首先我们加载 highcharter 包和 tidyverse 包:
install.packages("highcharter") # 安装包
install.packages("tidyverse") # 安装包
library(highcharter) # 加载包
library(tidyverse) # 加载包
2. 构造绘图数据
df <- tibble(x = c('苹果', '橘子', '梨', '葡萄', '香蕉'),
小张 = c(5, 3, 4, 7, 2),
小彭 = c(2, 2, 3, 2, 1),
小潘 = c(3, 4, 4, 2, 5),
小王 = c(3, 0, 4, 4, 3))
df # 查看数据框

非常简单的一个数据框。
3. 基础柱形图
首先我们绘制一幅基础柱状图:
原图来自:
https://www.highcharts.com.cn/demo/highcharts/bar-basic
highchart() %>%
hc_chart(type = "column") %>%
hc_title(text = '基础柱形图') %>%
hc_xAxis(categories = df$x) %>%
hc_yAxis(min = 0, title = list(text = "水果消费总量")) %>%
hc_add_series(data = df$小张, name = "小张") %>%
hc_add_series(data = df$小彭, name = "小彭") %>%
hc_add_series(data = df$小潘, name = "小潘") %>%
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")

4. 百分比堆叠柱状图
在基础柱状图的基础上设置 hc_plotOptions(column = list(stacking = 'percent'))
即可得到百分比堆叠柱状图:
highchart() %>%
hc_chart(type = "column") %>%
hc_title(text = '百分比堆叠柱形图') %>%
hc_xAxis(categories = df$x) %>%
hc_yAxis(min = 0, title = list(text = "水果消费总量")) %>%
hc_tooltip(pointFormat = '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b>({point.percentage:.0f}%)<br/>',
shared = T) %>%
hc_plotOptions(column = list(stacking = 'percent')) %>%
hc_add_series(data = df$小张, name = "小张") %>%
hc_add_series(data = df$小彭, name = "小彭") %>%
hc_add_series(data = df$小潘, name = "小潘") %>%
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")

5. 堆叠柱形图
如果我们设置 column = list(stacking = 'normal')
就可以得到堆叠柱形图:
highchart() %>%
hc_chart(type = "column") %>%
hc_title(text = '堆叠柱形图') %>%
hc_xAxis(categories = df$x) %>%
hc_yAxis(min = 0, max = 13,
title = list(text = "水果消费总量"),
stackLabels = list(
enabled = T,
style = list(
fontWeight = 'bold',
color = JS("(Highcharts.theme && Highcharts.theme.textColor) || 'gray'")
)
)) %>%
hc_tooltip(formatter = JS("function () {
return '<b>' + this.x + '</b><br/>' +
this.series.name + ': ' + this.y + '<br/>' +
'总量: ' + this.point.stackTotal;
}")) %>%
hc_plotOptions(column = list(stacking = 'normal'),
dataLabels = list(enabled = T,
color = JS("(Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'"),
style = list(textOutline = '1px 1px black'))) %>%
hc_add_series(data = df$小张, name = "小张") %>%
hc_add_series(data = df$小彭, name = "小彭") %>%
hc_add_series(data = df$小潘, name = "小潘") %>%
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_legend(align = 'right',
x = -30,
verticalAlign = 'top',
y = 25,
floating = TRUE,
backgroundColor = JS("(Highcharts.theme && Highcharts.theme.background2) || 'white'"),
borderColor = '#CCC',
borderWidth = 1,
shadow = F) %>%
hc_credits(enabled = T,
text = "©RStata 绘制",
href = "https://tidyfriday.cn")

6. 分组堆叠柱状图
在 hc_add_series
里面设置 stack
参数即可得到分组堆叠柱状图:
highchart() %>%
hc_chart(type = "column") %>%
hc_title(text = '分组堆叠柱形图') %>%
hc_xAxis(categories = df$x) %>%
hc_yAxis(min = 0, max = 15,
title = list(text = "水果消费总量"),
stackLabels = list(
enabled = T,
style = list(
fontWeight = 'bold',
color = JS("(Highcharts.theme && Highcharts.theme.textColor) || 'gray'")
)
)) %>%
hc_tooltip(formatter = JS("function () {
return '<b>' + this.x + '</b><br/>' +
this.series.name + ': ' + this.y + '<br/>' +
'总量: ' + this.point.stackTotal;
}")) %>%
hc_plotOptions(column = list(stacking = 'normal'),
dataLabels = list(enabled = T,
color = JS("(Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'"),
style = list(textOutline = '1px 1px black'))) %>%
hc_add_series(data = df$小张, name = "小张",
stack = 'male') %>%
hc_add_series(data = df$小彭, name = "小彭",
stack = 'female') %>%
hc_add_series(data = df$小潘, name = "小潘",
stack = 'male') %>%
hc_add_series(data = df$小王, name = "小王",
stack = 'male') %>%
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_legend(align = 'right',
x = -30,
verticalAlign = 'top',
y = 25,
floating = TRUE,
backgroundColor = JS("(Highcharts.theme && Highcharts.theme.background2) || 'white'"),
borderColor = '#CCC',
borderWidth = 1,
shadow = F) %>%
hc_credits(enabled = T,
text = "©RStata 绘制",
href = "https://tidyfriday.cn")

7. 角锥柱形图
使用 hc_chart(type = "columnpyramid")
即可得到角锥柱形图:
highchart() %>%
hc_chart(type = "columnpyramid") %>%
hc_title(text = '角锥柱形图') %>%
hc_xAxis(categories = df$x) %>%
hc_yAxis(min = 0, title = list(text = "水果消费总量")) %>%
hc_add_series(data = df$小张, name = "小张") %>%
hc_add_series(data = df$小彭, name = "小彭") %>%
hc_add_series(data = df$小潘, name = "小潘") %>%
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")

8. 3D 圆柱图
3D 圆柱图需要基于 highchartzero()
绘制:
highchartzero() %>%
hc_chart(type = "cylinder",
options3d = list(
enabled = TRUE,
alpha = 15,
beta = 15,
depth = 50,
viewDistance = 25
)) %>%
hc_title(text = '3D 圆柱图') %>%
hc_xAxis(categories = df$x) %>%
hc_yAxis(min = 0, title = list(text = "水果消费总量")) %>%
hc_add_series(data = df$小张, name = "小张") %>%
hc_add_series(data = df$小彭, name = "小彭") %>%
hc_add_series(data = df$小潘, name = "小潘") %>%
hc_add_dependency("highcharts-3d.js") %>%
hc_add_dependency("modules/cylinder.js") %>%
hc_exporting(enabled = T) %>%
hc_credits(enabled = T,
text = "©RStata 绘制",
href = "https://tidyfriday.cn")

使用 highchartzero() 绘制的图似乎不支持主题的设置,希望以后能改进下。
9. 水平柱状图
上面一些柱状图可以绘制水平版本,使用 bar 代替 column 即可,这里仅仅演示基础柱状图:
highchart() %>%
hc_chart(type = "bar") %>%
hc_title(text = '基础柱形图') %>%
hc_xAxis(categories = df$x) %>%
hc_yAxis(min = 0, title = list(text = "水果消费总量")) %>%
hc_add_series(data = df$小张, name = "小张") %>%
hc_add_series(data = df$小彭, name = "小彭") %>%
hc_add_series(data = df$小潘, name = "小潘") %>%
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")

是不是挺简单的!如果感觉有用的话欢迎帮助转发和转载~