专栏名称: EasyCharts
EasyCharts,易图表,我们将定期推送各种数据可视化与分析教程,包括Excel(Power BI)、Origin、Sigmaplot、GraphPad、R、Python、Matlab、Tableau、D3.js等。
目录
相关文章推荐
前端大全  ·  前端行情变了,差别真的挺大。。。 ·  昨天  
前端早读课  ·  【开源】TinyEngine开启新篇章,服务 ... ·  昨天  
前端大全  ·  React+AI 技术栈(2025 版) ·  3 天前  
商务河北  ·  经开区“美•强•优”三重奏 ·  2 天前  
奇舞精选  ·  从 DeepSeek 看25年前端的一个小趋势 ·  3 天前  
奇舞精选  ·  从 DeepSeek 看25年前端的一个小趋势 ·  3 天前  
51好读  ›  专栏  ›  EasyCharts

柱状图、箱线图、提琴图、杰特图还有蜂窝图,你要哪个?

EasyCharts  · 公众号  · 前端  · 2019-12-22 00:07

正文

上次提到箱线图等这些类型的图可以展示更多的数据特点,很多人并不推荐使用柱状图。 为了在科研论文中普及箱线图(boxplot)的使用,nature method还专门发文力推。

Visualization samples with box plots

文中详细讲解了箱线图的原理和优点,并与传统柱状图做了对比。柱状图边上标红的 Not recommended 格外显眼!

Box plots are a more communicative way to show sample data

另外,不久前刚获得“统计界诺奖”考普斯会长奖(COPSS Presidents' Award)的Hadley Wickham 还曾在2011年专门写过一篇文章 “40 years of boxplots” 总结了boxplot的发展变化。该文中对boxplot的构成介绍如下图:

Construction of a boxplot

经过很多年的发展,目前有各种各样画风的箱线图:

Box Plot Variations(http://datavizcatalogue.com/blog/box-plot-variations/)

其实在22排小提琴那篇帖子里,小提琴图的内部是有个箱线图的,如果你没注意,可以再去回顾一下。

除了箱线图,提琴图、杰特图和蜂窝图这些也是很好的选择。

如下,我简单画出了这几种统计图,供您作图时参考。

首先给出模拟数据:

1# loading packages
2library(tidyverse)
3library(cowplot)
4set.seed(1234)
5data "a","b"),100),
6                   value = sample(10:30,100, replace = TRUE))

先上柱状图:

 1bar % 
2  group_by(items) %>% 
3  summarise(mean = mean(value),
4            sd = sd(value),
5            se = sd/sqrt(length(value))) %>% 
6  ggplot(aes(items, mean, fill = items))+
7  geom_col(width = 0.5, color = "black")+
8  ggpubr::theme_pubr(14)+
9  theme(legend.position = "")+
10  geom_errorbar(aes(ymin=mean - sd, ymax=mean + sd), width=0.2, size = 0.5)+
11  labs(x = "barplot", y = "")+
12  ylim(0,35)
13bar

箱线图:

1box 2  geom_boxplot(width = 0.5)+
3  ggpubr::theme_pubr(14)+
4  theme(legend.position = "")+
5  labs(x = "boxplot", y = "")+
6  ylim(0,35)
7box

提琴图:

1vio 2  geom_violin(width = 0.5)+
3  ggpubr::theme_pubr(14)+
4  theme(legend.position = "")+
5  labs(x = "violinplot", y = "")+
6  ylim(0,35)
7vio

杰特图:

1jit 2  geom_jitter(width = 0.25)+
3  ggpubr::theme_pubr(14)+
4  theme(legend.position = "")+
5  labs(x = "jitterplot", y = "")+
6  ylim(0,35)
7jit

蜂窝图:

1bwm 2  ggbeeswarm::geom_beeswarm(cex = 2)+
3  ggpubr::theme_pubr(14)+
4  theme(legend.position = "")+
5  labs(x = "beeswarm", y = "")+
6  ylim(0,35)
7bwm

最后,我把除柱状图外的四种图画在一个坐标系上,可对比观察几种图的特点:

 1com  2  geom_boxplot(width = 0.2,position = position_nudge(-0.2), color = "black")+
3  geom_jitter(aes(as.numeric(items) - 0.4,value, color = items), width = 0.08)+
4  geom_violin(width = 0.2,position = position_nudge(0.2), color = "black")+
5  ggbeeswarm::geom_beeswarm(cex = 1)+
6  ggpubr::theme_pubr(14)+
7  theme(legend.position = "")+
8  labs(x = "combined", y = "")+
9  ylim(0,35)
10com

最后的最后,我们用cowplot来拼个图吧~ 做了share legends的复杂拼图。

 1#share legend
2legend  3  bar + 
4    guides(fill = guide_legend(ncol = 1))+
5    theme(legend.position = "right",
6          legend.box.margin = margin(0050, -50))
7)
8
9# 拼图
10plot_grid(
11  plot_grid(bar, box, jit, bwm, labels = c("a""b""c","d"), nrow = 2),
12  plot_grid(
13






请到「今天看啥」查看全文