r - How to generate facetted ggplot graph where each facet has ordered data? -


i want sort factors (condition, parameter , subjectid) meanweight , plot meanweight against subjectid such when faceted condition , parameter, meanweight appears in descending order. here solution, isn't giving me want:

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 3 variables orderweights <- order(datasummary$condition, datasummary$parameter, datasummary$subjectid, -datasummary$meanweight) ## set factors new order. expect sort each facet when plotting, doesn't seem 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") 

i tried few other approaches, didn't work either:

datasummary <- datasummary[order(datasummary$condition, datasummary$parameter, datasummary$subjectid, -datasummary$meanweight),] 

or one

datasummary <- transform(datasummary, subjectid=reorder(condition, parameter, subjectid, meanweight)) 

you can order data , plot it. however, labels no longer correspond subject id's, reordered subjects. if not want, cannot use faceting have plot parts separately , use e.g.grid.arrangeto combind different plots.

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

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -