簡體   English   中英

為什么 geom_rect 只為 facet_wrap 的第一行着色?

[英]Why geom_rect colours only first row of facet_wrap?

我試圖在我的facet_wrap圖的每個偶數面板上獲得陰影矩形。 但是,當我使用geom_rect時,它僅在第二個面板上生成矩形。 我嘗試使用annotategeom_tile但沒有成功。 我想我在這里遺漏了一些簡單的細節,可能與我的 X 變量是分類而不是數字的事實有關,但我已經為此奮斗了幾個小時......

這是我的代碼:

even_numbers <- seq(2,nrow(df.plt),2)
ggplot(df.plt) + 
  geom_rect(data = df.plt[even_numbers, ],
            xmin = even_numbers - 0.5, xmax = even_numbers + 0.5,
            ymin = -Inf, ymax = Inf, alpha = 0.3, fill = 'grey') +
  geom_boxplot(aes(x = Cnd, y = nst, fill = Srs), position = position_dodge(0.9), outlier.shape = 1) + 
  facet_wrap(vars(Grp), ncol=1)

結果圖: geom_rect 和 facet_wrap 的結果圖未按預期工作

編輯:

我創建了一個虛擬數據集示例來復制我的問題:

set.seed(002) # just to make it reproducible
df.tmp = data.frame(nst = rnorm(100*2), Srs = sample(rep(c("S3","S4"),100)), Cnd = sample(rep(c("DN","DA","DV","DAV"),50)), Grp = sample(rep(c("close","far"),100)))

even_numbers <- seq(2,nrow(df.tmp),2)
ggplot(df.tmp) + 
  geom_rect(data = df.tmp[even_numbers, ],
            xmin = even_numbers - 0.5, xmax = even_numbers + 0.5,
            ymin = -Inf, ymax = Inf, alpha = 0.3, fill = 'grey') +
  geom_boxplot(aes(x = Cnd, y = nst, fill = Srs), position = position_dodge(0.9), outlier.shape = 1) + 
  facet_wrap(vars(Grp), ncol=1)

雖然您的想法是正確的,恕我直言,您可以通過將xminxmax值放入數據框中並通過美學映射來更輕松地實現您想要的結果。 首先注意,我們只需要一個length(unique(df.tmp$Cnd)) ,即Cnd的類別數。 其次,當我們混合離散和連續 x 變量時,我在scale_x_discrete之前添加了一個geom_rect ,否則我們會得到一個錯誤。

library(ggplot2)

even_numbers <- seq(2, length(unique(df.tmp$Cnd)), 2)

rects <- data.frame(
  xmin = even_numbers - 0.5,
  xmax = even_numbers + 0.5
)

ggplot(df.tmp) +
  scale_x_discrete() +
  geom_rect(
    data = rects, aes(xmin = xmin, xmax = xmax),
    ymin = -Inf, ymax = Inf, alpha = 0.3, fill = "grey"
  ) +
  geom_boxplot(aes(x = Cnd, y = nst, fill = Srs), position = position_dodge(0.9), outlier.shape = 1) +
  facet_wrap(vars(Grp), ncol = 1)

編輯以防萬一。 您的方法不起作用的原因是用於 rects 的數據的相關部分僅包含far組。 問題是基本上只顯示對應於 1 到 4 范圍內的偶數( Cnd類別的數量)的矩形。 從下面的代碼片段可以看出,它復制了在你的方法中用於矩形的數據,只有far grp 存在(在過濾了 1 到 4 范圍內的偶數之后):

even_numbers <- seq(2,nrow(df.tmp),2)

dplyr::bind_cols(df.tmp[even_numbers, ], data.frame(even_number = even_numbers)) |> 
  dplyr::filter(even_number <= 4)
#>          nst Srs Cnd Grp even_number
#> 1  0.1848492  S3  DV far           2
#> 2 -1.1303757  S3  DA far           4

我一直試圖理解為什么只有第二段被遮蔽。 最后我准備了第二個例子,在其中我找到了另一種可能的解決方案。 但是,我並不完全滿意,因為我不了解geom_rect / facet_wrap的問題; 我只找到了一些解決方法。

這是示例:

# constructing the dataframe so all the combinations are present for both even and odd rows
df.tmp = data.frame(nst = rnorm(16*6), 
                    Srs = rep(c("S3", "S4"), each=8, 6), 
                    Cnd = rep(c("DN", "DA", "DV", "DAV"), each=2, 12), 
                    Grp = rep(c(rep(c("close","far"), 8), rev(rep(c("close","far"), 8))),3) )

even_numbers <- seq(2,nrow(df.tmp),2) # so the df.tmp[even_numbers, ] contains all the combinations
ggplot(df.tmp) + 
  geom_rect(data = df.tmp[even_numbers, ],
            xmin = even_numbers - 0.5, xmax = even_numbers + 0.5,
            ymin = -Inf, ymax = Inf, alpha = 0.3, fill = 'grey') +
  geom_boxplot(aes(x = Cnd, y = nst, fill = Srs), position = position_dodge(0.9), outlier.shape = 1) + 
  facet_wrap(vars(Grp), ncol=1)

正如您在此處看到的,盡管確保df.tmp[even_numbers, ]也包含close的數據點,但該圖僅在第二行中具有陰影矩形: 第一行中的非陰影段

在這里,我更改了ggplot ,使其分別包含close段和far段的geom_rect

even_numbers <- seq(2,length(unique(df.tmp$Cnd)),2) # here the df.tmp[even_numbers, ] doesn't need to have all the combinations
ggplot(df.tmp) + 
  geom_rect(data = df.tmp[df.tmp$Grp=="close", ][even_numbers, ],
            xmin = even_numbers - 0.5, xmax = even_numbers + 0.5,
            ymin = -Inf, ymax = Inf, alpha = 0.3, fill = 'grey') + 
  geom_rect(data = df.tmp[df.tmp$Grp=="far", ][even_numbers, ],
            xmin = even_numbers - 0.5, xmax = even_numbers + 0.5,
            ymin = -Inf, ymax = Inf, alpha = 0.3, fill = 'grey') +
  geom_boxplot(aes(x = Cnd, y = nst, fill = Srs), position = position_dodge(0.9), outlier.shape = 1) + 
  facet_wrap(vars(Grp), ncol=1)

正如您在下面看到的,它現在可以工作了: 在此處輸入圖像描述

正如我之前提到的,我仍然不確定為什么geom_rect開始就不起作用。 在我的解決方案中,需要為每個分段准備一個單獨的geom_rect ,因此對於包含許多分段的繪圖來說,這絕對不是一個解決方案。 我試圖找到一種更優雅的方式,這樣就不必擔心聲明了多少段或其他分組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM