当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R ggplot2 position_stack 将重叠的对象堆叠在一起


position_stack() 将条形堆叠在一起; position_fill() 堆叠条形并将每个堆叠标准化为具有恒定的高度。

用法

position_stack(vjust = 1, reverse = FALSE)

position_fill(vjust = 1, reverse = FALSE)

参数

vjust

对具有位置(如点或线)而不是尺寸(如条形或区域)的几何图形进行垂直调整。设置为 0 以与底部对齐,0.5 为中间对齐,1(默认值)为顶部对齐。

reverse

如果是 TRUE ,将反转默认的堆叠顺序。如果您要旋转绘图和图例,这非常有用。

细节

position_fill()position_stack() 自动以与组美学相反的顺序堆叠值,对于条形图来说,这通常由填充美学定义(默认组美学由除 x 和 y 之外的所有离散美学的组合形成)。此默认值可确保条形颜色与默认图例对齐。

根据您的需要,可以通过三种方式覆盖默认值:

  1. 更改基础因子中的级别顺序。这将更改堆叠顺序以及图例中键的顺序。

  2. 设置图例breaks以更改键的顺序而不影响堆叠。

  3. 手动设置组美学以更改堆叠顺序而不影响图例。

正值和负值的堆叠是单独执行的,因此正值从 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/position-stack.R

相关用法


注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Stack overlapping objects on top of each another。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。