簡體   English   中英

如何在 ggplot 條形圖中重新排序 X 軸聚類

[英]How to Reorder X-Axis Clustering in ggplot Bar-Chart

R專家下午好,

我正在用 R 制作一個相當復雜的條形圖,我很難重新組織圖中的集群。 具體來說,所有控制值和病變值都分別聚類,如下所示:

簇狀條形圖

與其將控制和病變條件分別聚集在每個方面,我希望它們按每篇論文和任務並排排列。 這應該會導致 Control 和 Lesion 標簽重復多次,我不知道如何做到這一點。 我用來實現此條形圖的代碼如下:

p10 <- ggplot(cbmeta_new1, 
              aes(x = Condition, y = Proportion, fill = Task, alpha = Paper)) +   
  geom_bar(stat = 'identity', position = 'dodge', colour='black') + 
  facet_grid(~  Type) +
  scale_alpha_discrete()

有人可以建議我如何編輯上述腳本以實現所需的格式嗎? 提前感謝您的時間和考慮!

以下是一些示例數據:

示例數據

dput(cbmeta_new[1:10, ])
structure(list(Paper = structure(c(1L, 2L, 2L, 2L, 2L, 4L, 5L, 
3L, 3L, 6L), .Label = c("Cattaneo et al., 2012", "Clausi et al., 
2019", 
"Gerschcovich et al., 2011", "Hoche et al., 2016", "Roca et al., 
2013", 
"van Harskamp et al., 2005", "van Overwalle et al., 2019"), class = 
"factor"), 
Type = structure(c(2L, 1L, 3L, 4L, 4L, 1L, 4L, 1L, 4L, 4L
), .Label = c("Perceptual", "Sequencing", "Emotion Attribution", 
"Verbal Vignette", "Other"), class = "factor"), Task = structure(c(1L, 
9L, 3L, 5L, 12L, 9L, 5L, 9L, 5L, 12L), .Label = c("Bio Action Obs", 
"Causal Attribution", "Emot Attribution", "False Belief", 
"Faux Pas", "Gravity", "Mechanical", "Norm Behavior", "RMET", 
"Sequencing", "Social Script", "ToM", "Trait Attribution", 
"Violations"), class = "factor"), Condition = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Control", 
"Lesion"), class = "factor"), Proportion = c(0.8875, 0.73, 
0.71, 0.78, 0.95, 0.7632, 0.91, 0.829444444, 0.95, 0.986428571
)), row.names = c(NA, 10L), class = "data.frame")

我很抱歉,我是這個網站的新手,仍在探索如何最好地制作這些帖子。

親切的問候,琳達

您的示例數據僅包含“控制”條件,並且每種“類型”僅包含一項任務。 我通過從 Control 中減去一個常量,並通過用新名稱回收每個任務,為每種類型添加了一個第二個虛擬任務,從而添加了一個虛擬的“病變”-條件。

#add Lesion Condition
cbmeta_new1 <- rbind(cbmeta_new1, 
                     transform(cbmeta_new1, Condition = "Lesion",
                               Proportion = cbmeta_new1$Proportion - 0.2))

#add second task for each type
cbmeta_new1 <- rbind(cbmeta_new1,
                     transform(cbmeta_new1, Task = paste0(substr(cbmeta_new1$Task, 1, 3), "2")))

聽起來您想要組合條件、任務和紙張來確定條在 x 軸上的位置。 一種方法是使用包含這些變量的所有組合的interaction()創建一個新變量。

然后,您可以手動指定 x 軸上所需的標簽。 在這里,我進行了設置,以便僅顯示 Condition 的標簽(Control vs Lesion),而 Task 和 Paper 由fillalpha標識,就像在原始圖中一樣。

#variable for position on x-axis
cbmeta_new1$xpos1 <- with(cbmeta_new1, 
                         interaction(Condition, Paper, Task, drop = TRUE))

#Labels on x-axis (condition only)
xlabs1 <- rep(c("Control", "Lesion"), length(levels(cbmeta_new1$xpos)))

#plot
ggplot(cbmeta_new1, 
       aes(x = xpos1, y = Proportion, fill = Task, alpha = Paper)) +   
  geom_bar(stat = "identity", colour = "black", ) + 
  facet_grid(. ~  Type, scale = "free_x") +
  scale_alpha_discrete() +
  scale_x_discrete(name = "", labels = xlabs1)+
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

在此處輸入圖片說明

我發現 alpha 級別對於區分論文沒有用。 更好的解決方案可能是將紙質標簽放在 x 軸上並使用 alpha 作為條件。 當您還可以按條件對條形進行分組時(對於任務和紙張的每個組合)。 原理和之前一樣:(a) 創建一個變量來定義 x 位置 (b) 定義在 x 軸上顯示哪些標簽。 在這種情況下,我只展示了論文。 條件和任務由 alpha 和顏色定義。 為了創建 x 標簽,我編寫了一個自定義函數,該函數從xpos2變量的級別中提取紙張名稱。 可能有一種更直接的方法可以做到這一點,但這說明了總體思路。

#variable for position on x-axis
cbmeta_new1$xpos2 <- with(cbmeta_new1, interaction(Paper, Task, drop = TRUE))

##Labels on x-axis (paper only)
xlabs2 <- sapply(levels(cbmeta_new1$xpos2), FUN = function(x) {
  x = sub(pattern = "et al.", replacement = "et al", x)
  x = sub(pattern = "\n", replacement = "", x)
  x = strsplit(x, split = ".", fixed = TRUE)[[1]]
  return(x[1])
})

#plot
ggplot(cbmeta_new1, 
       aes(x = xpos2, y = Proportion, fill = Task, alpha = Condition)) +   
  geom_bar(aes(group = Condition), stat = "identity", colour = "black", 
           position = "dodge") + 
  facet_grid(. ~  Type, scale = "free_x") +
  scale_alpha_discrete() +
  scale_x_discrete(name = "", labels = xlabs2) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

在此處輸入圖片說明

另一種選擇可能是使用嵌套方面(例如,類型嵌套的任務)。 例如,請參見此處: https : //stackoverflow.com/a/58037287/6240490 或者您可以通過例如為每個“類型”創建單獨的圖然后自己安排它們來簡化事情,例如使用拼湊包( https://github.com/thomasp85/patchwork )。

暫無
暫無

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

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