简体   繁体   English

如何在 ggplot 条形图中重新排序 X 轴聚类

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

Good afternoon R experts, R专家下午好,

I am in the process of making a fairly involved bar-chart in R, and I am having a difficult time reorganizing the clusters in my graph.我正在用 R 制作一个相当复杂的条形图,我很难重新组织图中的集群。 Specifically, all of the control and lesion values are clustered separately as shown below:具体来说,所有控制值和病变值都分别聚类,如下所示:

簇状条形图

Instead of the Control and Lesion conditions being clustered separately in each facet, I want them to be side-by-side per each paper and task.与其将控制和病变条件分别聚集在每个方面,我希望它们按每篇论文和任务并排排列。 This should cause the Control and Lesion labels to be repeated many times over, and I cannot figure out how to do this.这应该会导致 Control 和 Lesion 标签重复多次,我不知道如何做到这一点。 My code that I used to achieve this bar graph is below:我用来实现此条形图的代码如下:

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()

Can someone please advise as to how I can edit the above script to achieve the desired formatting?有人可以建议我如何编辑上述脚本以实现所需的格式吗? Thank you in advance for your time and consideration!提前感谢您的时间和考虑!

Here is some example data:以下是一些示例数据:

Example Data示例数据

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")

I apologize, I'm new to this site and still navigating how to best make these posts.我很抱歉,我是这个网站的新手,仍在探索如何最好地制作这些帖子。

Kind regards, Linda亲切的问候,琳达

Your example data contains only "Control"-conditions and only one task per "type".您的示例数据仅包含“控制”条件,并且每种“类型”仅包含一项任务。 I added a dummy "Lesion"-Condition by substracting a constant from Control and a second dummy-task for each type by recycling each task with a new name.我通过从 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")))

It sounds like you want a combination of Condition, Task, and Paper to determine the position of a bar on the x-axis.听起来您想要组合条件、任务和纸张来确定条在 x 轴上的位置。 One way to do this would be to create a new variable with interaction() which contains all combinations of these variables.一种方法是使用包含这些变量的所有组合的interaction()创建一个新变量。

You can then manually specify which labels you want on the x-axis.然后,您可以手动指定 x 轴上所需的标签。 Here I set it up so that only the labels for Condition are shown (Control vs Lesion) whereas Task and Paper are identified by fill and alpha as in your original plot.在这里,我进行了设置,以便仅显示 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))

在此处输入图片说明

I find alpha-level not useful to distinguish the papers.我发现 alpha 级别对于区分论文没有用。 A better solution may be to put the paper labels on the x-axis and use alpha for condition.更好的解决方案可能是将纸质标签放在 x 轴上并使用 alpha 作为条件。 When you could also group the bars by Condition (for each combination of task and paper).当您还可以按条件对条形进行分组时(对于任务和纸张的每个组合)。 The principle is the same as before: (a) create a variable to define x-position (b) define which labels to show on the x-axis.原理和之前一样:(a) 创建一个变量来定义 x 位置 (b) 定义在 x 轴上显示哪些标签。 In this case I am showing only thep papers.在这种情况下,我只展示了论文。 Condition and Task are defined by alpha and color.条件和任务由 alpha 和颜色定义。 To create the x-labels I wrote a custom function which extracts the paper name from the levels of the xpos2 -variable.为了创建 x 标签,我编写了一个自定义函数,该函数从xpos2变量的级别中提取纸张名称。 There is probably a more straightforward way to do this, but this illustrates the general idea.可能有一种更直接的方法可以做到这一点,但这说明了总体思路。

#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))

在此处输入图片说明

Another option may be to use nested facets (eg, task nested in type).另一种选择可能是使用嵌套方面(例如,类型嵌套的任务)。 See for instance here: https://stackoverflow.com/a/58037287/6240490 .例如,请参见此处: https : //stackoverflow.com/a/58037287/6240490 Or you could simplify things by, for instance, creating separate plots for each "Type" and then arranging them yourself, eg using the patchwork package ( https://github.com/thomasp85/patchwork ).或者您可以通过例如为每个“类型”创建单独的图然后自己安排它们来简化事情,例如使用拼凑包( https://github.com/thomasp85/patchwork )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM