當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。