我希望將語意差異分數的用戶調查結果呈現為箱線圖,第一個語意沿默認 x 軸顯示,第二個語意沿第二個頂部 x 軸顯示。簡單來說,這可以表示為
df <- data.frame(sem1 = c("good", "big", "red"), sem2 = c("bad", "small", "blue"), a = c(4,6,1), b = c(6,2,6), c = c(8,3,7))`
df %>% pivot_longer(!c(sem1, sem2), names_to = "user", values_to = "score") %>%
ggplot()
geom_boxplot(aes(x = sem1, y = score))
ylim(1,10)
這產生了下面的圖表。我真正想做的是將 sem2 中的字符(即“壞”、“小”、“藍色”)添加到圖表頂部的輔助軸。我所看到的使用 sec_axis 的所有東西似乎都適合于數值軸的轉換,而不是添加一個不同的第二個軸。我相信我可以使用 annotation_custom 逐個添加每個語意,但在我的實際應用程式中,使用大約 15 個語意對來顯示這可能會變得非常麻煩。有沒有更優雅的方式?

uj5u.com熱心網友回復:
一個可能的選擇:
library(tidyverse)
df <- data.frame(sem1 = c("good", "big", "red"),
sem2 = c("bad", "small", "blue"),
a = c(4, 6, 1), b = c(6, 2, 6),
c = c(8, 3, 7)) %>%
pivot_longer(!c(sem1, sem2), names_to = "user", values_to = "score")
df %>%
ggplot(aes(sem1, score))
geom_boxplot()
geom_label(aes(y = max(score * 1.2), label = sem2))
ylim(1, 10)

由reprex 包(v2.0.1)于 2022-05-19 創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/477816.html
