我希望為 geom_tile() 的 x 軸上的變數添加一個總體分組標簽。
這類似于我正在尋找的結果:

但是,我能想到的最好的方法(不使用 geom_text() 并在 x 軸變數上方手動添加第 1 組、第 2 組和第 3 組)就是這樣,它只是創建了一個包含兩個資訊的新變數:
set.seed(1)
df <- expand.grid(group = c("Group 1", "Group 2", "Group 3"),
xVar = c("A", "B"),
yVar = c("x","y","z")) %>%
mutate(fillValue = factor(sample(c(0,1), 18, TRUE))) %>%
mutate(group_and_xVar = paste(group, xVar, sep = "\n"))
ggplot(df, aes(x = group_and_xVar, y = yVar, fill = fillValue))
geom_tile(colour = "black")
scale_x_discrete(position = "top", expand = c(0,0))
scale_y_discrete(expand = c(0,0))
scale_fill_manual(values=c("#FF4933", "#72AF4A"))
labs(x = "", y = "", fill = "")
theme_bw()
theme(axis.text.x = element_text(angle = 0, vjust = .05),
legend.position = "none")

這不是我想要的。我希望每個組只在繪圖上出現一次,并且對代碼進行軟編碼(適應未來可能添加的新資料或組)。我也嘗試過使用 facet_grid/facet_wrap 但我沒有運氣。
此外,調整單元格的高度以使其看起來更像第一張影像將是一個獎勵:)
uj5u.com熱心網友回復:
受這篇
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/444203.html
