科研绘图模板之柱状图
柱状图是一种用于显示数据分布的图表类型,通常用于展示不同类别之间的比较。柱状图以矩形条(柱子)的高度来表示数据的数量或频率,每个柱子通常对应数据集中的一个类别。以下是柱状图的一些关键要素和常见用途:
柱状图的要素:
-
柱子(Bars): 表示不同类别的矩形条,其高度(或长度)与相应类别的数据值相关联。
-
X轴(横轴): 表示不同类别的轴线,每个柱子通常对应于X轴上的一个刻度。
-
Y轴(纵轴): 表示数据值的轴线,用于度量和比较不同类别的数量或频率。
-
填充颜色: 可以用不同颜色填充柱子,以区分不同的类别或组。
-
边框颜色和宽度: 可以为柱子设置边框,以增强可视效果。
-
标题和标签: 柱状图通常包括标题和轴标签,以提供更多的信息和上下文。
柱状图的用途:
-
比较类别间的数量: 柱状图常用于比较不同类别之间的数量或频率。每个柱子的高度直观地反映了相应类别的数据量。
-
显示趋势: 柱状图可以用来显示数据随时间或其他有序变量的变化趋势。
-
排名和排序: 通过对柱子进行排序,可以清晰地看到哪些类别具有最高或最低的值。
-
部分和整体关系: 可以将柱状图用于展示整体和各个部分之间的关系,特别适用于堆叠柱状图和分组柱状图。
-
异常值检测: 异常值通常会在柱状图中表现为高度明显不同的柱子。
示例
library(ggplot2)
library(cowplot)
library(tidyverse)
data("diamonds")
small_diamonds <- filter(diamonds,
cut == c('Very Good', 'Premium', 'Ideal'))
p1 <- ggplot(data = small_diamonds, aes(x = color)) +
geom_bar(aes(fill = cut),
position = 'stack',
color = 'black',
width = .7) +
scale_fill_manual(values = c("#B2DF8A","#4DBBD5", "#4DAF4A")) +
scale_y_continuous(expand = c(0, 0)) +
theme_half_open() +
ggtitle('position_stack')
p2 <- ggplot(data = small_diamonds, aes(x = color)) +
geom_bar(aes(fill = cut),
position = 'fill',
color = 'black',
width = .7) +
scale_fill_manual(values = c("#B2DF8A","#4DBBD5", "#4DAF4A")) +
scale_y_continuous(expand = c(0, 0)) +
theme_half_open()+
ggtitle('position_fill')
p3 <- ggplot(data = small_diamonds, aes(x = color)) +
geom_bar(aes(fill = cut),
position = 'dodge',
color = 'black',
width = .7) +
scale_fill_manual(values = c("#B2DF8A","#4DBBD5", "#4DAF4A")) +
scale_y_continuous(expand = c(0, 0)) +
theme_half_open() +
ggtitle('position_dodge')
![](https://img.haomeiwen.com/i27313279/f1e25a1655cdd710.png)
这段代码使用了 ggplot2
库在 R 中创建一个柱状图,以下是对代码的解释:
上述代码首先使用 filter
函数从数据框 diamonds
中选择 cut
列为 'Very Good'、'Premium' 和 'Ideal' 的行,将结果存储在 small_diamonds
中。
接下来的代码用于创建柱状图:
-
ggplot(data = small_diamonds, aes(x = color))
: 创建一个ggplot
对象,使用small_diamonds
数据框,设置 x 轴为颜色 (color
)。 -
geom_bar(aes(fill = cut), position = 'stack', color = 'black', width = .7)
: 添加柱状图层,设置填充颜色为cut
变量,摆放方式为堆叠,边框颜色为黑色,柱状的宽度为 0.7。 -
scale_fill_manual(values = c("#B2DF8A", "#4DBBD5", "#4DAF4A"))
: 使用scale_fill_manual
函数手动指定填充颜色为三个不同的颜色。 -
scale_y_continuous(expand = c(0, 0))
: 设置 y 轴的范围扩展为 0,即 y 轴不留空白。 -
theme_half_open()
: 使用自定义的主题theme_half_open
,这可能是用户定义的主题。 -
ggtitle('position_stack')
: 设置图表标题为 'position_stack'。
添加标签
在柱子上面添加标签,对geom_text()进行count变换
ggplot(data = small_diamonds, aes(x = color)) +
geom_bar(aes(fill = cut),
position = 'stack',
color = 'black',
width = .7) +
geom_text(stat = 'count',
aes(label = ..count..),
position = 'stack',
vjust = -0.5) +
scale_fill_manual(values = c("#B2DF8A","#4DBBD5", "#4DAF4A")) +
scale_y_continuous(expand = c(0, 0), limits = c(0, 3500)) +
theme_half_open() +
ggtitle('position_stack')
逐行解释:
-
ggplot(data = small_diamonds, aes(x = color))
: 创建一个ggplot
对象,使用small_diamonds
数据框,设置 x 轴为颜色 (color
)。 -
geom_bar(aes(fill = cut), position = 'stack', color = 'black', width = .7)
: 添加柱状图层,设置填充颜色为cut
变量,摆放方式为堆叠,边框颜色为黑色,柱状的宽度为 0.7。 -
geom_text(stat = 'count', aes(label = ..count..), position = 'stack', vjust = -0.5)
: 添加文本标签,显示每个柱子的数量。stat = 'count'
表示使用统计计数,aes(label = ..count..)
设置标签内容为柱子的数量,position = 'stack'
表示文本标签与柱子堆叠,vjust = -0.5
表示垂直方向上相对于柱子位置的调整。 -
scale_fill_manual(values = c("#B2DF8A", "#4DBBD5", "#4DAF4A"))
: 使用scale_fill_manual
函数手动指定填充颜色为三个不同的颜色。 -
scale_y_continuous(expand = c(0, 0), limits = c(0, 3500))
: 设置 y 轴的范围扩展为 0,同时限制 y 轴的范围为 0 到 3500。 -
theme_half_open()
: 使用自定义的主题theme_half_open
,这可能是用户定义的主题。 -
ggtitle('position_stack')
: 设置图表标题为 'position_stack'。
![](https://img.haomeiwen.com/i27313279/73e68f77a57456d5.png)
- 如果需要对每个cut分别添加标签,就需要提前处理数据
small_diamonds_count <- group_by(small_diamonds, color, cut) %>% # 按照color和cut两列进行分组
summarise(count = n()) %>% # 对每个组计算观测值数量,并创建一个新的列count
arrange(color, desc(cut)) %>% # 按照color升序、cut降序排列数据
mutate(cumsum = cumsum(count), # 计算count列的累计和,并创建一个新的列cumsum
prop = count / sum(count), # 计算每个组占总体的比例,并创建一个新的列prop
cumprop = cumsum(count) / sum(count)) # 计算每个组累计占总体的比例,并创建一个新的列cumprop
逐行解释:
-
group_by(small_diamonds, color, cut) %>%
: 对small_diamonds
数据框按照color
和cut
两列进行分组。 -
summarise(count = n()) %>%
: 对每个组计算观测值数量,并创建一个新的列count
,表示每个组中的观测数量。 -
arrange(color, desc(cut)) %>%
: 对结果进行排序,首先按照color
升序排列,然后按照cut
降序排列。 -
mutate(cumsum = cumsum(count), prop = count / sum(count), cumprop = cumsum(count) / sum(count))
: 使用mutate
函数添加三个新的列:-
cumsum
: 计算count
列的累计和,表示每个组的累计数量。 -
prop
: 计算每个组占总体的比例,即每个组的数量除以总数量。 -
cumprop
: 计算每个组累计占总体的比例,即每个组的累计数量除以总数量。
-
最终,small_diamonds_count
数据框包含了按照 color
和 cut
分组的统计信息,包括每个组的数量、累计数量、占比和累计占比。
ggplot(data = small_diamonds_count) +
geom_col(aes(x = color, y = count, fill = cut),
position = 'stack',
color = 'black',
width = .7) +
geom_text(aes(x = color, y = cumsum - count / 2, label = count)) +
scale_fill_manual(values = c("#B2DF8A","#4DBBD5", "#4DAF4A")) +
scale_y_continuous(expand = c(0, 0)) +
theme_half_open()+
ggtitle('position_stack')
逐行解释:
-
ggplot(data = small_diamonds_count)
: 创建一个ggplot
对象,使用small_diamonds_count
数据框。 -
geom_col(aes(x = color, y = count, fill = cut), position = 'stack', color = 'black', width = .7)
: 添加堆叠柱状图层。通过aes
函数,设置 x 轴为颜色 (color
),y 轴为数量 (count
),填充颜色为cut
变量。使用position = 'stack'
表示堆叠排列,边框颜色为黑色,柱状的宽度为 0.7。 -
geom_text(aes(x = color, y = cumsum - count / 2, label = count))
: 添加文本标签,显示每个柱子的数量。通过aes
函数,设置 x 轴为颜色 (color
),y 轴为累计数量减去数量一半,标签内容为柱子的数量。 -
scale_fill_manual(values = c("#B2DF8A", "#4DBBD5", "#4DAF4A"))
: 使用scale_fill_manual
函数手动指定填充颜色为三个不同的颜色。 -
scale_y_continuous(expand = c(0, 0))
: 设置 y 轴的范围扩展为 0,即 y 轴不留空白。 -
theme_half_open()
: 使用自定义的主题theme_half_open
,这可能是用户定义的主题。 -
ggtitle('position_stack')
: 设置图表标题为 'position_stack'。
![](https://img.haomeiwen.com/i27313279/ea658178739c5277.png)