R 如何在每个方面都有有序数据的情况下生成分面ggplot图?

R 如何在每个方面都有有序数据的情况下生成分面ggplot图?,r,plot,ggplot2,facet,R,Plot,Ggplot2,Facet,我想按平均权重对我的因子(条件、参数和主体)进行排序,并根据主体绘制平均权重图,这样当按条件和参数分面时,平均权重按降序显示。 这是我的解决方案,它没有给我想要的: dataSummary <- structure(list(SubjectID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L), .Label = c("s001", "s002", "s003",

我想按平均权重对我的因子(条件、参数和主体)进行排序,并根据主体绘制平均权重图,这样当按条件和参数分面时,平均权重按降序显示。 这是我的解决方案,它没有给我想要的:

dataSummary <- structure(list(SubjectID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L), .Label = c("s001", 
"s002", "s003", "s004"), class = "factor"), Condition = structure(c(1L, 
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L), .Label = c("1", "2", "3"), class = "factor"), Parameter = structure(c(1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
3L), .Label = c("(Intercept)", "PrevCorr1", "PrevFail1"), class = "factor"), 
    MeanWeight = c(-0.389685536725783, 0.200987679398502, -0.808114314421089, 
    -0.10196105040707, 0.0274188815763494, 0.359978984195839, 
    -0.554583879312783, 0.643791202050396, -0.145042221940287, 
    -0.0144598460145723, -0.225804028997856, -0.928152539784374, 
    0.134025102103562, -0.267448309989731, -1.19980109795115, 
    0.0587152632631923, 0.0050656268880826, -0.156537446664213
    )), .Names = c("SubjectID", "Condition", "Parameter", "MeanWeight"
), row.names = c(NA, 18L), class = "data.frame")
## Order by three variables
orderWeights <- order(dataSummary$Condition, dataSummary$Parameter, dataSummary$SubjectID, -dataSummary$MeanWeight)
## Set factors to the new order. I expect this to sort for each facet when plotting, but it doesn't seem to work. 
conditionOrder <- dataSummary$Condition[orderWeights]
dataSummary$Condition <- factor(dataSummary$Condition, levels=conditionOrder)
paramOrder <- dataSummary$Parameter[orderWeights]
dataSummary$Parameter <- factor(dataSummary$Parameter, levels=paramOrder)
sbjOrder <- dataSummary$SubjectID[orderWeights]
dataSummary$SubjectID <- factor(dataSummary$SubjectID, levels=sbjOrder)
## Plot
ggplot(dataSummary, aes(x=MeanWeight, y=SubjectID)) + 
scale_x_continuous(limits=c(-3, 3)) + 
geom_vline(yintercept = 0.0, size = 0.1, colour = "#a9a9a9", linetype = "solid") + 
geom_segment(aes(yend=SubjectID), xend=0, colour="grey50") + 
geom_point(size=2) + 
facet_grid(Parameter~Condition, scales="free_y")

dataSummary您可以对数据进行排序并进行绘图。但是,标签不再对应于主题ID,而是对应于重新排序的主题。如果这不是您想要的,您不能使用镶嵌面,但必须单独绘制零件,并使用例如
网格。排列
以组合不同的绘图

require(plyr)
## Ordered data
datOrder <- ddply(dataSummary, c("Condition", "Parameter"), function(x){
  if (nrow(x)<=1) return(x)
  x$MeanWeight <- x$MeanWeight[order(x$MeanWeight)]
  x
})
## Plot
ggplot(datOrder, aes(x=MeanWeight, y=SubjectID)) + 
  scale_x_continuous(limits=c(-3, 3)) + 
  geom_vline(yintercept = 0.0, size = 0.1, colour = "#a9a9a9", linetype = "solid") + 
  geom_segment(aes(yend=SubjectID), xend=0, colour="grey50") + 
  geom_point(size=2) + 
  facet_grid(Parameter~Condition) +
  scale_y_discrete(name="Ordered subjects")
require(plyr)
##有序数据

datOrder(1)您没有注意到代码引发的所有警告吗?(2) 您尝试的是不可能的(使用镶嵌面)。这将需要对每一行面的y轴进行不同的排序。@joran,(1)我看到了警告,在没有很快弄清楚他们在告诉我什么之后就忽略了它们。(2) 我希望facet_网格中的“scales='free'”可以让我为每个绘图拥有独立的比例。要尝试将y轴转换为连续变量。所有自由比例将允许范围变化。但是,根据定义,刻面是为了在比例基本相同的情况下使用。你提出的是完全不同的顺序,因此规模也完全不同。我知道的唯一选择是使用grid.arrange,如下所述。我现在就知道了。谢谢。谢谢@shadow。这接近我想要的。然而,我还想让主语与顺序匹配。我希望在facet_网格中设置“scales='free'”可以为每个绘图设置独立的轴,但对于因子来说可能不是这样。我现在正在考虑将主语从分类变量转换为连续变量,看看这是否适用于facet_网格
dataSummary <- transform(dataSummary, SubjectID=reorder(Condition, Parameter, SubjectID, MeanWeight))
require(plyr)
## Ordered data
datOrder <- ddply(dataSummary, c("Condition", "Parameter"), function(x){
  if (nrow(x)<=1) return(x)
  x$MeanWeight <- x$MeanWeight[order(x$MeanWeight)]
  x
})
## Plot
ggplot(datOrder, aes(x=MeanWeight, y=SubjectID)) + 
  scale_x_continuous(limits=c(-3, 3)) + 
  geom_vline(yintercept = 0.0, size = 0.1, colour = "#a9a9a9", linetype = "solid") + 
  geom_segment(aes(yend=SubjectID), xend=0, colour="grey50") + 
  geom_point(size=2) + 
  facet_grid(Parameter~Condition) +
  scale_y_discrete(name="Ordered subjects")