我嘗試使用兩次呼叫 annotate() 來獲取 2 級嵌套刻度標簽(
# action used as facet
df %>%
ggplot(data=., mapping=aes(x=interaction(status,gend), y=cellMean,
color=status, shape=gend))
geom_point(size=3.5)
theme_light()
facet_wrap(~action)

# annotate hack on unfaceted
df %>%
ggplot(data=., mapping=aes(x=interaction(status,gend), y=cellMean,
color=status, shape=gend))
geom_point(size=3.5)
annotate(geom = "text", x = 1:4, y = .7, label = rep(c("A","B"), times=2) )
annotate(geom = "text", x = c(1.5,3.5), y = .6, label = c("Females","Males"))
coord_cartesian(ylim = c(.8, 4), xlim=c(.5,4.5), expand = FALSE, clip = "off")
theme_light()
theme(plot.margin = unit(c(1, 1, 4, 1), "lines"),
axis.title.x = element_blank(),
axis.text.x = element_blank() )

# annotate hack on FACETED fails saying it wants 8 labels
df %>%
ggplot(data=., mapping=aes(x=interaction(status,gend), y=cellMean,
color=status, shape=gend))
geom_point(size=3.5)
annotate(geom = "text", x = 1:4, y = .7, label = rep(c("A","B"), times=2) )
annotate(geom = "text", x = c(1.5,3.5), y = .6, label = c("Females","Males"))
coord_cartesian(ylim = c(.8, 4), xlim=c(.5,4.5), expand = FALSE, clip = "off")
theme_light()
theme(plot.margin = unit(c(1, 1, 4, 1), "lines"),
axis.title.x = element_blank(),
axis.text.x = element_blank() )
facet_wrap(~action)
產生:“錯誤:美學必須是長度 1 或與資料 (8):label 相同”,這似乎需要跨兩個方面的 8 個標簽,每個方面有 4 個標簽。
但是,試圖供應 8 個才需要 16 個。
# annotate hack on FACETED with length 8 vectors fails saying 16
df %>%
ggplot(data=., mapping=aes(x=interaction(status,gend), y=cellMean,
color=status, shape=gend))
geom_point(size=3.5)
annotate(geom = "text", x = rep(1:4, times=2), y = .7, label = rep(c("A","B"), times=4) )
annotate(geom = "text", x = c(1.5,3.5), y = .6, label = c("Females","Males"))
coord_cartesian(ylim = c(.8, 4), xlim=c(.5,4.5), expand = FALSE, clip = "off")
theme_light()
theme(plot.margin = unit(c(1, 1, 4, 1), "lines"),
axis.title.x = element_blank(),
axis.text.x = element_blank() )
facet_wrap(~action)
產生:“錯誤:美學必須是長度1或與資料相同(16):標簽”
有沒有辦法將 annotate() 技巧與 facet_wrap() 一起使用?
我想知道是否需要制作 2 個圖并將它們并排放置以模擬刻面。
uj5u.com熱心網友回復:
我也經常難以annotate()很好地處理方面。我無法讓它作業,但你可以使用它geom_text()。它需要對剪輯、x-label 格式和主題設定進行一些修改才能使其正常作業。我vjust = 3, y = -Inf選擇而不是硬編碼 y 位置,這樣人們就可以更輕松地將其推廣到他們的情節中。
df %>%
ggplot(data=., mapping=aes(x=interaction(status,gend), y=cellMean,
color=status, shape=gend))
geom_point(size=3.5)
geom_text(data = data.frame(z = logical(2)),
aes(x = rep(c(1.5, 3.5), 2), y = -Inf,
label = rep(c("Females", "Males"), 2)),
inherit.aes = FALSE, vjust = 3)
theme_light()
coord_cartesian(clip = "off")
facet_wrap(~action)
scale_x_discrete(labels = ~ substr(.x, 1, nchar(.x) - 2))
theme(axis.title.x.bottom = element_text(margin = margin(t = 20)))

另一種選擇是用于ggh4x::guide_axis_nested()顯示interaction()ed 因素。您需要重新編碼您的 M/F 級別以讀取男性/女性以獲得與上述類似的結果。
df %>%
ggplot(data=., mapping=aes(x=interaction(status,gend), y=cellMean,
color=status, shape=gend))
geom_point(size=3.5)
theme_light()
facet_wrap(~action)
guides(x = ggh4x::guide_axis_nested(delim = ".", extend = -1))

由reprex 包創建于 2022-03-30 (v2.0.1)
免責宣告:我寫了 ggh4x。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/452993.html
