在 ggplot2 r 中的 x 轴上添加子组 labels/order 元素

Add subgroup labels/order elements on x-axis in ggplot2 r

我正在尝试添加 sub-group labels 并在我的 . There are multiple questions about this on here already but the responses all recommend using faceting (e.g. here 中的 x 轴上排列观察值。我的情节已经是多方面的,因此这些回应对我不起作用。我尝试使用 reorder(x, by_this_variable) 但这似乎只有在 by_this_variable 是 y 轴时才有效。为什么?如果我尝试用不同的变量重新排序,我会收到警告:

argument is not numeric or boolean

更具体地说,我正在为每个离散的 x 轴值(每个参与者 1 个)绘制两个点(参与者在两个不同任务中获得的百分比),并用箭头连接每个参与者的点。这是为了表明参与者的行为是否在任务中受到负面或正面影响。我的方面是参与者被随机分入的 2 种不同(治疗)条件。我现在想根据不同的参与者来源(对治疗的不同反应的可能预测因子)对这些点箭头图进行分组,并将此信息添加为 x 轴上的标签,但我现在所能实现的就是拥有按字母顺序排序的值(默认值)。

这个情节可能最终看起来太忙了。如果有更好的方法将所有这些信息(按任务、参与者、条件、来源的行为相对变化)绘制在一张图中,我愿意征求建议!

我的代码:

Data <- data.frame(c(28.5, 20, 55.4, 30.5, 66.6, 45.4, 43.2, 43.1, 28.5, 55.4, 30.5, 
                   66.6, 45.4, 20), c("Participant 1", "Participant 1", 
                   "Participant 2", "Participant 2", "Participant 3", 
                   "Participant 3","Participant 4", "Participant 4","Participant 5", 
                   "Participant 5", "Participant 6", "Participant 6", "Participant 7", 
                   "Participant 7"),c("India", "India", "India", "India", "Algeria", 
                   "Algeria", "Algeria", "Algeria", "India", "India", "India", 
                   "India", "Algeria", "Algeria"),c("Treatment A", "Treatment A", 
                   "Treatment B", "Treatment B","Treatment A", "Treatment A", 
                   "Treatment B", "Treatment B", "Treatment A", "Treatment A", 
                   "Treatment B", "Treatment B", "Treatment A", "Treatment A"),
                   c("Task 1", "Task 2", "Task 1", "Task 2", "Task 1", "Task 2", 
                   "Task 1", "Task 2", "Task 1", "Task 2", "Task 1", "Task 2", 
                   "Task 1", "Task 2"))
colnames(Data) <- c("Percentage", "Participant", "Origin", "Treatment", "Task")

ggplot(Data, aes(y=Percentage, x = Participant, group = Participant))+
   geom_point(aes(color = Task))+ 
   geom_line(arrow = arrow(length=unit(0.30,"cm"), type = "closed"), size = .3)+
   facet_grid(~Treatment, scales = "free_x", space = "free_x")+ 
   theme(axis.text.x = element_text(angle = 90, hjust = 1))

这会产生以下情节:

Plot

参与者 1 和 5 来自印度,参与者 3 和 7 来自阿尔及利亚,所以我想将他们在 x 轴上分组并添加原产地标签。

编辑:

上面的警告似乎源于 Origin 是一个多层次因素(并且重新排序似乎只适用于数值),因此设置 x = reorder(Participant, as.numeric(Origin) ) 将根据 Origin 对值进行排序,但如何在图下方添加适当的 Origin 标签?

一个建议是使用有序因子。对于因子的水平,连接 OriginParticipant。对于因子的标签,连接 ParticipantOrigin.

# The unique values from the column 'Origin_Participant' will act as the levels
# of the factor. The order is imposed by 'Origin', so that participants from
# same country group together.
Data$Origin_Participant <- paste(Data$Origin, Data$Participant, sep = "\n")
# The unique values from 'Participant_Origin' column will be used for the
# factor' labels (what will end up on the plot).
Data$Participant_Origin <- paste(Data$Participant, Data$Origin, sep = "\n")
# Order data.frame by 'Origin_Participant'. Is also important so that the levels
# correspond to the labels of the factor when creating it below.
Data <- Data[order(Data$Origin_Participant),]
# Or in decreasing order if you need
# Data <- Data[order(Data$Origin_Participant, decreasing = TRUE),]

# Finally, create the needed factor.
Data$Origin_Participant <- factor(x = Data$Origin_Participant,
                                  levels = unique(Data$Origin_Participant),
                                  labels = unique(Data$Participant_Origin),
                                  ordered = TRUE)

library(ggplot2)
# Reuse your code, but map the factor `Origin_Participant` into x. I think there
# is no need of a grouping factor. I also added vjust = 0.5 to align the labels
# on the vertical center.
ggplot(Data, aes(y=Percentage, x = Origin_Participant))+
  geom_point(aes(color = Task))+ 
  geom_line(arrow = arrow(length=unit(0.30,"cm"), type = "closed"), size = .3)+
  facet_grid(~Treatment, scales = "free_x", space = "free_x")+ 
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))

如果您不关心 Origin 出现在标签中的第一个位置,那么可以缩短几步:

Data$Origin_Participant <- factor(x = paste(Data$Origin, Data$Participant, sep = "\n"),
                                  ordered = TRUE)
ggplot(Data, aes(y=Percentage, x = Origin_Participant))+
  geom_point(aes(color = Task))+ 
  geom_line(arrow = arrow(length=unit(0.30,"cm"), type = "closed"), size = .3)+
  facet_grid(~Treatment, scales = "free_x", space = "free_x")+ 
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))