position_stack() 将条形堆叠在一起; position_fill() 堆叠条形并将每个堆叠标准化为具有恒定的高度。
参数
- vjust
 - 
对具有位置(如点或线)而不是尺寸(如条形或区域)的几何图形进行垂直调整。设置为
0以与底部对齐,0.5为中间对齐,1(默认值)为顶部对齐。 - reverse
 - 
如果是
TRUE,将反转默认的堆叠顺序。如果您要旋转绘图和图例,这非常有用。 
细节
position_fill() 和 position_stack() 自动以与组美学相反的顺序堆叠值,对于条形图来说,这通常由填充美学定义(默认组美学由除 x 和 y 之外的所有离散美学的组合形成)。此默认值可确保条形颜色与默认图例对齐。
根据您的需要,可以通过三种方式覆盖默认值:
- 
更改基础因子中的级别顺序。这将更改堆叠顺序以及图例中键的顺序。
 - 
设置图例
breaks以更改键的顺序而不影响堆叠。 - 
手动设置组美学以更改堆叠顺序而不影响图例。
 
正值和负值的堆叠是单独执行的,因此正值从 x 轴向上堆叠,负值向下堆叠。
由于堆叠是在尺度变换之后执行的,因此使用非线性尺度进行堆叠会产生扭曲,很容易导致数据的误解。因此,不鼓励将这些位置调整与尺度变换(例如对数或平方根尺度)结合使用。
也可以看看
有关更多示例,请参阅geom_bar() 和 geom_area()。
其他位置调整:position_dodge()、position_identity()、position_jitterdodge()、position_jitter()、position_nudge()
例子
# Stacking and filling ------------------------------------------------------
# Stacking is the default behaviour for most area plots.
# Fill makes it easier to compare proportions
ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) +
  geom_bar()
ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) +
  geom_bar(position = "fill")
ggplot(diamonds, aes(price, fill = cut)) +
  geom_histogram(binwidth = 500)
ggplot(diamonds, aes(price, fill = cut)) +
  geom_histogram(binwidth = 500, position = "fill")
# Stacking is also useful for time series
set.seed(1)
series <- data.frame(
  time = c(rep(1, 4),rep(2, 4), rep(3, 4), rep(4, 4)),
  type = rep(c('a', 'b', 'c', 'd'), 4),
  value = rpois(16, 10)
)
ggplot(series, aes(time, value)) +
  geom_area(aes(fill = type))
# Stacking order ------------------------------------------------------------
# The stacking order is carefully designed so that the plot matches
# the legend.
# You control the stacking order by setting the levels of the underlying
# factor. See the forcats package for convenient helpers.
series$type2 <- factor(series$type, levels = c('c', 'b', 'd', 'a'))
ggplot(series, aes(time, value)) +
  geom_area(aes(fill = type2))
# You can change the order of the levels in the legend using the scale
ggplot(series, aes(time, value)) +
  geom_area(aes(fill = type)) +
  scale_fill_discrete(breaks = c('a', 'b', 'c', 'd'))
# If you've flipped the plot, use reverse = TRUE so the levels
# continue to match
ggplot(series, aes(time, value)) +
  geom_area(aes(fill = type2), position = position_stack(reverse = TRUE)) +
  coord_flip() +
  theme(legend.position = "top")
# Non-area plots ------------------------------------------------------------
# When stacking across multiple layers it's a good idea to always set
# the `group` aesthetic in the ggplot() call. This ensures that all layers
# are stacked in the same way.
ggplot(series, aes(time, value, group = type)) +
  geom_line(aes(colour = type), position = "stack") +
  geom_point(aes(colour = type), position = "stack")
ggplot(series, aes(time, value, group = type)) +
  geom_area(aes(fill = type)) +
  geom_line(aes(group = type), position = "stack")
# You can also stack labels, but the default position is suboptimal.
ggplot(series, aes(time, value, group = type)) +
  geom_area(aes(fill = type)) +
  geom_text(aes(label = type), position = "stack")
# You can override this with the vjust parameter. A vjust of 0.5
# will center the labels inside the corresponding area
ggplot(series, aes(time, value, group = type)) +
  geom_area(aes(fill = type)) +
  geom_text(aes(label = type), position = position_stack(vjust = 0.5))
# Negative values -----------------------------------------------------------
df <- tibble::tribble(
  ~x, ~y, ~grp,
  "a", 1,  "x",
  "a", 2,  "y",
  "b", 1,  "x",
  "b", 3,  "y",
  "b", -1, "y"
)
ggplot(data = df, aes(x, y, group = grp)) +
  geom_col(aes(fill = grp), position = position_stack(reverse = TRUE)) +
  geom_hline(yintercept = 0)
ggplot(data = df, aes(x, y, group = grp)) +
  geom_col(aes(fill = grp)) +
  geom_hline(yintercept = 0) +
  geom_text(aes(label = grp), position = position_stack(vjust = 0.5))
相关用法
- R ggplot2 position_dodge 躲避左右重叠的物体
 - R ggplot2 position_nudge 将点微移固定距离
 - R ggplot2 position_jitter 抖动点以避免过度绘制
 - R ggplot2 position_jitterdodge 同时闪避和抖动
 - R ggplot2 print.ggplot 明确绘制情节
 - R ggplot2 print.ggproto 格式化或打印 ggproto 对象
 - R ggplot2 annotation_logticks 注释:记录刻度线
 - R ggplot2 vars 引用分面变量
 - R ggplot2 geom_qq 分位数-分位数图
 - R ggplot2 geom_spoke 由位置、方向和距离参数化的线段
 - R ggplot2 geom_quantile 分位数回归
 - R ggplot2 geom_text 文本
 - R ggplot2 get_alt_text 从绘图中提取替代文本
 - R ggplot2 annotation_custom 注释:自定义grob
 - R ggplot2 geom_ribbon 函数区和面积图
 - R ggplot2 stat_ellipse 计算法行数据椭圆
 - R ggplot2 resolution 计算数值向量的“分辨率”
 - R ggplot2 geom_boxplot 盒须图(Tukey 风格)
 - R ggplot2 lims 设置规模限制
 - R ggplot2 geom_hex 二维箱计数的六边形热图
 - R ggplot2 scale_gradient 渐变色阶
 - R ggplot2 scale_shape 形状比例,又称字形
 - R ggplot2 geom_bar 条形图
 - R ggplot2 draw_key 图例的关键字形
 - R ggplot2 annotate 创建注释层
 
注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Stack overlapping objects on top of each another。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
