如何生成facetted ggplot图,其中每个facet都有订购数据?

时间:2013-07-31 14:04:34

标签: r plot ggplot2 facet

我想通过MeanWeight对我的因子(条件,参数和主题ID)进行排序,并根据条件和参数绘制MeanWeight,以便在按条件和参数分面时,MeanWeight按降序显示。 这是我的解决方案,它没有给我我想要的东西:

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 <- dataSummary[order(dataSummary$Condition, dataSummary$Parameter, dataSummary$SubjectID, -dataSummary$MeanWeight),]

或者这个

dataSummary <- transform(dataSummary, SubjectID=reorder(Condition, Parameter, SubjectID, MeanWeight))

1 个答案:

答案 0 :(得分:0)

您可以订购数据并进行绘图。但是,标签不再与主题ID相对应,而是与重新排序的主题相对应。如果这不是你想要的,你不能使用刻面,但必须单独绘制部分,并使用例如grid.arrange来组合不同的绘图。

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