科研绘图模板之柱状图

2024-01-17  本文已影响0人  Bioinfor生信云

柱状图是一种用于显示数据分布的图表类型,通常用于展示不同类别之间的比较。柱状图以矩形条(柱子)的高度来表示数据的数量或频率,每个柱子通常对应数据集中的一个类别。以下是柱状图的一些关键要素和常见用途:

柱状图的要素:

  1. 柱子(Bars): 表示不同类别的矩形条,其高度(或长度)与相应类别的数据值相关联。

  2. X轴(横轴): 表示不同类别的轴线,每个柱子通常对应于X轴上的一个刻度。

  3. Y轴(纵轴): 表示数据值的轴线,用于度量和比较不同类别的数量或频率。

  4. 填充颜色: 可以用不同颜色填充柱子,以区分不同的类别或组。

  5. 边框颜色和宽度: 可以为柱子设置边框,以增强可视效果。

  6. 标题和标签: 柱状图通常包括标题和轴标签,以提供更多的信息和上下文。

柱状图的用途:

  1. 比较类别间的数量: 柱状图常用于比较不同类别之间的数量或频率。每个柱子的高度直观地反映了相应类别的数据量。

  2. 显示趋势: 柱状图可以用来显示数据随时间或其他有序变量的变化趋势。

  3. 排名和排序: 通过对柱子进行排序,可以清晰地看到哪些类别具有最高或最低的值。

  4. 部分和整体关系: 可以将柱状图用于展示整体和各个部分之间的关系,特别适用于堆叠柱状图和分组柱状图。

  5. 异常值检测: 异常值通常会在柱状图中表现为高度明显不同的柱子。

示例

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')

这段代码使用了 ggplot2 库在 R 中创建一个柱状图,以下是对代码的解释:

上述代码首先使用 filter 函数从数据框 diamonds 中选择 cut 列为 'Very Good'、'Premium' 和 'Ideal' 的行,将结果存储在 small_diamonds 中。

接下来的代码用于创建柱状图:

添加标签

在柱子上面添加标签,对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')

逐行解释:

  1. ggplot(data = small_diamonds, aes(x = color)): 创建一个 ggplot 对象,使用 small_diamonds 数据框,设置 x 轴为颜色 (color)。

  2. geom_bar(aes(fill = cut), position = 'stack', color = 'black', width = .7): 添加柱状图层,设置填充颜色为 cut 变量,摆放方式为堆叠,边框颜色为黑色,柱状的宽度为 0.7。

  3. geom_text(stat = 'count', aes(label = ..count..), position = 'stack', vjust = -0.5): 添加文本标签,显示每个柱子的数量。stat = 'count' 表示使用统计计数,aes(label = ..count..) 设置标签内容为柱子的数量,position = 'stack' 表示文本标签与柱子堆叠,vjust = -0.5 表示垂直方向上相对于柱子位置的调整。

  4. scale_fill_manual(values = c("#B2DF8A", "#4DBBD5", "#4DAF4A")): 使用 scale_fill_manual 函数手动指定填充颜色为三个不同的颜色。

  5. scale_y_continuous(expand = c(0, 0), limits = c(0, 3500)): 设置 y 轴的范围扩展为 0,同时限制 y 轴的范围为 0 到 3500。

  6. theme_half_open(): 使用自定义的主题 theme_half_open,这可能是用户定义的主题。

  7. ggtitle('position_stack'): 设置图表标题为 'position_stack'。

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

逐行解释:

  1. group_by(small_diamonds, color, cut) %>%: 对 small_diamonds 数据框按照 colorcut 两列进行分组。

  2. summarise(count = n()) %>%: 对每个组计算观测值数量,并创建一个新的列 count,表示每个组中的观测数量。

  3. arrange(color, desc(cut)) %>%: 对结果进行排序,首先按照 color 升序排列,然后按照 cut 降序排列。

  4. mutate(cumsum = cumsum(count), prop = count / sum(count), cumprop = cumsum(count) / sum(count)): 使用 mutate 函数添加三个新的列:

    • cumsum: 计算 count 列的累计和,表示每个组的累计数量。
    • prop: 计算每个组占总体的比例,即每个组的数量除以总数量。
    • cumprop: 计算每个组累计占总体的比例,即每个组的累计数量除以总数量。

最终,small_diamonds_count 数据框包含了按照 colorcut 分组的统计信息,包括每个组的数量、累计数量、占比和累计占比。

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')

逐行解释:

  1. ggplot(data = small_diamonds_count): 创建一个 ggplot 对象,使用 small_diamonds_count 数据框。

  2. 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。

  3. geom_text(aes(x = color, y = cumsum - count / 2, label = count)): 添加文本标签,显示每个柱子的数量。通过 aes 函数,设置 x 轴为颜色 (color),y 轴为累计数量减去数量一半,标签内容为柱子的数量。

  4. scale_fill_manual(values = c("#B2DF8A", "#4DBBD5", "#4DAF4A")): 使用 scale_fill_manual 函数手动指定填充颜色为三个不同的颜色。

  5. scale_y_continuous(expand = c(0, 0)): 设置 y 轴的范围扩展为 0,即 y 轴不留空白。

  6. theme_half_open(): 使用自定义的主题 theme_half_open,这可能是用户定义的主题。

  7. ggtitle('position_stack'): 设置图表标题为 'position_stack'。

上一篇 下一篇

猜你喜欢

热点阅读