堆积柱状图也是我们科研绘图的常客,在堆积柱状图中同一分组可以展示多个变量信息,并通过颜色区分各个变量的大小。是在复杂分组信息的柱状图可视化展示的较优选择。

下面通过ggplot2简单展示其用R绘制的方法:


library(ggplot2)
library(ggthemes)
library(dplyr)

data <- data.frame(
    Class = c("A", "A", "B", "B", "C", "C"),
    Value = c(12, 32, 45, 65, 76, 45),
    Name = c("N1", "N2", "N1", "N2", "N1", "N2")
)


data  %>%
    ggplot(aes(x = Class, y = Value, fill = Name)) +
    geom_bar(stat = "identity", width = 0.5) +
    theme_few() +
    # 设置y轴起始点为0,参考:https://evvail.com/2019/09/27/230.html
    scale_y_continuous(expand = c(0, 0), limits = c(0, 125)) +
    ylab("Value")

参考资料:

1.https://www.data-to-viz.com/graph/barplot.html