Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort ggplot boxplots by median with facets

I'm trying to get ggplot to order my boxplots based on median value after splittin the data into several different facets.

This is part of a larger Shiny app I've written. Under default parameters, I can generate three faceted boxplots that order correctly:

boxData <- structure(list(Classification = structure(c(4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Pluripotent/ Undifferentiated", 
"Endoderm", "Mesoderm", "Ectoderm"), class = c("ordered", "factor"
)), value = c(0.000255214868214152, 0.000108050996652777, 0.00751505823956855, 
8.71801689770664, 5.71059263813113e-05, 4.90291746067526e-05, 
0.000129388767504551, 2.52712436532327e-07, 5345.09546573398, 
0.0020991194782334, 4.33360773005175e-06, 1.8200776481618, 3.44754305553851e-06, 
4.38932775031697, 0.00720892572385782, 7.53517216121544e-05, 
0.221288441144887, 0.00104230990042965, 0.00288742662358172, 
4.20947546944294e-05, 9.62973878475845e-07, 0.00710967831313203, 
26.9833955280036, 0.00265697432110539, 1.41814003567946, 0.261340025051291, 
0.00159083508412152, 9.55044905589291e-06, 0.0122931632086495, 
8.54789134364452e-06, 2.01899938950824e-05, 1.55354988683742e-06, 
0.000441285511108929, 0.000353500530366103, 0.125347054487635, 
109.440278770173, 2.03304264082645e-05, 2.01899938950824e-05, 
0.000148628664387571, 2.89902659683517e-06, 207.073625180606, 
3.52469070261441e-07, 3.15047327017105e-06, 0.639049681601525, 
2.11937734339159e-05, 0.484309094613314, 0.0126387710681522, 
0.000124981311087457, 0.010701820155981, 0.00520458916051572, 
0.002548740132205, 6.70653961877279e-06, 1.1372650836283e-06, 
0.0028674817110041, 6.38196191847228, 0.00104230990042965, 2.77791027153022, 
0.385285554179204, 3.23552539344696, 0.00129215960928528, 3313.17800288969, 
0.42454812322342, 0.427501088945987, 0.0252775421363044, 1.3790172222154e-05, 
0.000499925244349826, 0.575943821174679, 3.66456124110476e-05, 
0.000979273863184647, 1.71186456807568e-06, 0.000506903940694852, 
3.95489796579998e-05, 7.60789146241221e-07, 5.53083255055159e-07, 
0.000283178626588241, 5.68632541814152e-07, 89.5114292952616, 
2.15183665744117e-06, 9.48447928546097e-06, 1.10616651011032e-06, 
6.83831307491562e-05, 0.000231612381626088, 0.361984543094889, 
5.91197625260395e-05, 0.000979273863184647, 2.83936549218472e-06, 
0.000979273863184647, 5.11112358098405e-05, 1.714153924998e-07, 
5.19634300333657e-07, 0.000285939985649123, 0.000340041865397713, 
0.11809338012465, 60.884369685235, 2.29364239206782e-05, 1.59952159960469e-05, 
0.000213718586351138, 2.65657707341963e-06, 3635.65603745587, 
1.08786283557826e-07, 3.36257994807117e-06, 0.482299092292068, 
1.40214978558205e-05, 0.506277403675245, 0.00847835446782661, 
5.84677257215999e-05, 0.00674484030136259, 0.00483589957358377, 
0.0017456741452281, 6.45120458509457e-06, 6.32689066217975e-07, 
0.00245170310797391, 9.30496033238278, 0.000922604532223834, 
1.94261499108326, 0.348202870167258, 0.000995700862302919, 9.18683915124066e-06, 
0.00490340621594781, 9.51081233425213e-06, 1.64449027258861e-05, 
1.32828853670982e-06, 0.000283964853893518, 0.000480891817820092, 
0.103521332666818, 96.202334596196, 1.57750051307367e-05, 2.09600255345096e-05, 
0.000200793473806753, 1.29196641682183e-06, 179.519904082227, 
2.39744324779145e-07, 2.44454941589392e-06, 0.492433221447773, 
1.07746460295468e-05, 0.437695664847132, 0.00947275639891981, 
9.69768554804815e-05, 0.0056325346541415, 0.00470366164543522, 
0.00172164093341244, 6.91422987569681e-06, 8.82439067876674e-07, 
0.00253816223135828, 5.84822979360013, 0.000929021754230271, 
2.31017156910716, 0.278934830581241, 2.84415482117455, 0.00100262650949219, 
2661.45599990874, 0.357992185300285, 0.37579036951639, 0.0210213626331535, 
1.87597483406766e-05, 4.9165300967331e-05, 0.353063601096188, 
2.84344613435294e-05, 0.00277749494255326, 1.32828853670982e-06, 
0.00108958918195797, 9.25073867082013e-06, 1.4059026149049e-07, 
4.29154362580066e-07, 0.000537294242854559, 8.10925044524043e-06, 
0.020165038913309, 9.91469621624329e-06, 1.63313094852695e-05, 
8.58308725160133e-07, 2.34183669433728e-05, 0.000352033415883844, 
0.28087497575791, 4.58728478413563e-05, 0.0007598488052299, 1.48407969771465e-06, 
0.0223745115812679, 1.15479796826903e-05, 1.33006491938229e-07, 
4.03200286568411e-07, 83.9815202938853, 211.131788444181, 1.73147313103931, 
0.162893393670412, 6347.61978641754, 1.56049096034741, 0.532923368033971, 
0.651573574681646, 22.0392007421302, 0.05154584678813, 85997.0767809387, 
2.10234581817541, 1994.76074197656, 17462.8329237372, 1.76785506212734, 
49735.9012814537, 1.57134503333516, 340.615434516655, 3.73730938753272, 
2.07340220203944, 0.974004268543241, 53.8920290309386, 28.8800232787977, 
0.0604547706008708, 6.41744933081988, 1.9615580079771, 0.384751805040216, 
1.53900722016086, 1.68412590721683, 2.31658561238929, 1.62675839626425, 
2.23767420207142, 1.67249279982813, 1.53900722016086, 1.51781925297405, 
0.717972255311719, 1.08072540203935, 1.6958399292663, 1.74351647907412, 
1.6958399292663, 0.98077900398855, 0.000159075579756261, 1.32133840565826, 
1.57134503333516, 1.79253339913881, 2.00277451142267, 1.74351647907412, 
2.66105808216138, 0.90250072746243, 2.059080166868, 1.50733490955838, 
1.3966785324674, 1.61552155521922, 1.42602571736414, 1.90791910109511, 
1.38703096913138, 1.38703096913138, 1.49692298679269, 1.69583992926629, 
2.16145080407871, 2.67956720485568, 1.3966785324674, 1.53900722016086, 
1.70763542878249, 0.921464186198703, 3.32188009636358, 10.5707072452661, 
6.5522935828786, 1.68412590721683, 7.57896056479413, 1.43594451062343, 
0.312515575646302, 34.1070955541741, 2339.52511354582, 11.0962477530511, 
8.17942824487938, 1.68412590721683, 0.418123199957032, 804.528657067602, 
0.679243142274472, 1.47631440568283, 1.75564359521904, 2.81278639982623, 
4.14680440407889, 1.68412590721683, 2.33269873957693, 1.68412590721683, 
1.70763542878249, 1.37745004638314, 1.68412590721683), listElement = structure(c(3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Endoderm", 
"Mesoderm", "Ectoderm"), class = "factor")), .Names = c("Classification", 
"value", "listElement"), row.names = c(NA, -270L), class = "data.frame")

To generate the boxplot:

boxData$temp <- paste(substr(boxData$Classification,1,6), 
                            as.character(boxData$listElement))
ggplot(boxData, aes(reorder(boxData$temp, value, median),value, fill=Classification))+
        geom_boxplot()+
        scale_y_log10()+
        ylab("Fold Expression Change")+
        xlab("Gene Classification")+
        theme(axis.text.x=element_text(angle=90, hjust=1, size=6))+
facet_wrap(~listElement, scales='free', ncol=1)+
        scale_x_discrete(labels=setNames(as.character(boxData$Classification), boxData$temp))

Correct looking boxplot

But if a parameter is changed and we only have two samples rather than three (In this case, the same data, but with twice as many 'endoderm' samples and no 'mesoderm' samples), the boxplots look really weird:

boxData <- structure(list(Classification = structure(c(4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Pluripotent/ Undifferentiated", 
"Endoderm", "Mesoderm", "Ectoderm"), class = c("ordered", "factor"
)), value = c(0.000255214868214152, 0.000108050996652777, 0.00751505823956855, 
8.71801689770664, 5.71059263813113e-05, 4.90291746067526e-05, 
0.000129388767504551, 2.52712436532327e-07, 5345.09546573398, 
0.0020991194782334, 4.33360773005175e-06, 1.8200776481618, 3.44754305553851e-06, 
4.38932775031697, 0.00720892572385782, 7.53517216121544e-05, 
0.221288441144887, 0.00104230990042965, 0.00288742662358172, 
4.20947546944294e-05, 9.62973878475845e-07, 0.00710967831313203, 
26.9833955280036, 0.00265697432110539, 1.41814003567946, 0.261340025051291, 
0.00159083508412152, 9.55044905589291e-06, 0.0122931632086495, 
8.54789134364452e-06, 2.01899938950824e-05, 1.55354988683742e-06, 
0.000441285511108929, 0.000353500530366103, 0.125347054487635, 
109.440278770173, 2.03304264082645e-05, 2.01899938950824e-05, 
0.000148628664387571, 2.89902659683517e-06, 207.073625180606, 
3.52469070261441e-07, 3.15047327017105e-06, 0.639049681601525, 
2.11937734339159e-05, 0.484309094613314, 0.0126387710681522, 
0.000124981311087457, 0.010701820155981, 0.00520458916051572, 
0.002548740132205, 6.70653961877279e-06, 1.1372650836283e-06, 
0.0028674817110041, 6.38196191847228, 0.00104230990042965, 2.77791027153022, 
0.385285554179204, 3.23552539344696, 0.00129215960928528, 3313.17800288969, 
0.42454812322342, 0.427501088945987, 0.0252775421363044, 1.3790172222154e-05, 
0.000499925244349826, 0.575943821174679, 3.66456124110476e-05, 
0.000979273863184647, 1.71186456807568e-06, 0.000506903940694852, 
3.95489796579998e-05, 7.60789146241221e-07, 5.53083255055159e-07, 
0.000283178626588241, 5.68632541814152e-07, 89.5114292952616, 
2.15183665744117e-06, 9.48447928546097e-06, 1.10616651011032e-06, 
6.83831307491562e-05, 0.000231612381626088, 0.361984543094889, 
5.91197625260395e-05, 0.000979273863184647, 2.83936549218472e-06, 
0.000979273863184647, 5.11112358098405e-05, 1.714153924998e-07, 
5.19634300333657e-07, 3.36257994807117e-06, 0.482299092292068, 
1.40214978558205e-05, 0.00847835446782661, 5.84677257215999e-05, 
0.00674484030136259, 0.00483589957358377, 0.0017456741452281, 
6.45120458509457e-06, 6.32689066217975e-07, 0.00245170310797391, 
9.30496033238278, 0.000922604532223834, 1.94261499108326, 0.348202870167258, 
0.506277403675245, 0.000285939985649123, 0.000340041865397713, 
0.11809338012465, 60.884369685235, 2.29364239206782e-05, 1.59952159960469e-05, 
0.000213718586351138, 2.65657707341963e-06, 3635.65603745587, 
1.08786283557826e-07, 83.9815202938853, 211.131788444181, 1.73147313103931, 
0.162893393670412, 6347.61978641754, 1.56049096034741, 0.532923368033971, 
0.651573574681646, 22.0392007421302, 0.05154584678813, 85997.0767809387, 
2.10234581817541, 1994.76074197656, 17462.8329237372, 1.76785506212734, 
49735.9012814537, 1.57134503333516, 340.615434516655, 3.73730938753272, 
2.07340220203944, 0.974004268543241, 53.8920290309386, 28.8800232787977, 
0.0604547706008708, 6.41744933081988, 1.9615580079771, 1.57750051307367e-05, 
2.09600255345096e-05, 0.000200793473806753, 1.29196641682183e-06, 
179.519904082227, 2.39744324779145e-07, 2.44454941589392e-06, 
0.492433221447773, 1.07746460295468e-05, 0.437695664847132, 0.00947275639891981, 
9.69768554804815e-05, 0.0056325346541415, 0.00470366164543522, 
0.00172164093341244, 6.91422987569681e-06, 8.82439067876674e-07, 
1.57134503333516, 1.79253339913881, 2.00277451142267, 1.74351647907412, 
2.66105808216138, 0.90250072746243, 2.059080166868, 1.50733490955838, 
1.3966785324674, 1.61552155521922, 0.384751805040216, 1.53900722016086, 
1.68412590721683, 0.000995700862302919, 9.18683915124066e-06, 
0.00490340621594781, 9.51081233425213e-06, 1.64449027258861e-05, 
1.32828853670982e-06, 0.000283964853893518, 0.000480891817820092, 
0.103521332666818, 96.202334596196, 1.6958399292663, 0.98077900398855, 
0.000159075579756261, 2.31658561238929, 1.62675839626425, 2.23767420207142, 
1.67249279982813, 1.53900722016086, 1.51781925297405, 0.717972255311719, 
1.08072540203935, 1.6958399292663, 1.74351647907412, 1.32133840565826, 
0.0210213626331535, 1.87597483406766e-05, 4.9165300967331e-05, 
0.00253816223135828, 5.84822979360013, 0.000929021754230271, 
2.31017156910716, 0.278934830581241, 2.84415482117455, 0.00100262650949219, 
2661.45599990874, 0.357992185300285, 0.37579036951639, 1.42602571736414, 
1.90791910109511, 1.38703096913138, 0.353063601096188, 2.84344613435294e-05, 
0.00277749494255326, 1.32828853670982e-06, 0.00108958918195797, 
9.25073867082013e-06, 1.4059026149049e-07, 4.29154362580066e-07, 
0.000537294242854559, 8.10925044524043e-06, 0.020165038913309, 
9.91469621624329e-06, 1.63313094852695e-05, 8.58308725160133e-07, 
1.43594451062343, 0.312515575646302, 34.1070955541741, 2339.52511354582, 
11.0962477530511, 8.17942824487938, 1.68412590721683, 0.418123199957032, 
804.528657067602, 0.679243142274472, 10.5707072452661, 6.5522935828786, 
1.68412590721683, 1.38703096913138, 1.49692298679269, 1.69583992926629, 
2.16145080407871, 2.67956720485568, 1.3966785324674, 1.53900722016086, 
1.70763542878249, 0.921464186198703, 3.32188009636358, 7.57896056479413, 
2.34183669433728e-05, 0.000352033415883844, 0.28087497575791, 
4.58728478413563e-05, 0.0007598488052299, 1.48407969771465e-06, 
0.0223745115812679, 1.15479796826903e-05, 1.33006491938229e-07, 
4.03200286568411e-07, 1.47631440568283, 1.75564359521904, 2.81278639982623, 
4.14680440407889, 1.68412590721683, 2.33269873957693, 1.68412590721683, 
1.70763542878249, 1.37745004638314, 1.68412590721683), listElement = structure(c(2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Endoderm", 
"Ectoderm"), class = "factor")), .Names = c("Classification", 
"value", "listElement"), row.names = c(NA, -270L), class = "data.frame")

Running the same code as above:

boxData$temp <- paste(substr(boxData$Classification,1,6), 
                            as.character(boxData$listElement))
ggplot(boxData, aes(reorder(boxData$temp, value, median),value, fill=Classification))+
        geom_boxplot()+
        scale_y_log10()+
        ylab("Fold Expression Change")+
        xlab("Gene Classification")+
        theme(axis.text.x=element_text(angle=90, hjust=1, size=6))+
facet_wrap(~listElement, scales='free', ncol=1)+
scale_x_discrete(labels=setNames(as.character(boxData$Classification), boxData$temp))

gives a strange-looking graph:

Weird looking graph

This graph should look the same as the first graph, just with two facets rather than three. If I don't try to reorder the values by median, this graph plots fine. I've fiddled with a number of things, but can't seem to fix this issue. I'm sure I've made a stupid mistake somewhere, but can't seem to find it.

Any help would be greatly appreciated!

like image 580
RareAquaticBadger Avatar asked Aug 14 '17 20:08

RareAquaticBadger


1 Answers

It looks to me that you are reordering the factor "temp" without releveling the dataset. What about bringing the ordering operation outside the ggplot call?

boxData$temp <- paste(substr(boxData$Classification,1,6), 
                      as.character(boxData$listElement))

fac <- with(boxData, reorder(temp, value, median, order = TRUE))
boxData$temp <- factor(boxData$temp, levels = levels(fac))
ggplot(boxData, aes(temp,value, fill=Classification))+
  geom_boxplot()+
  scale_y_log10()+
  ylab("Fold Expression Change")+
  xlab("Gene Classification")+
  theme(axis.text.x=element_text(angle=90, hjust=1, size=6))+
  facet_wrap(~listElement, scales='free', ncol=1)+
  scale_x_discrete(labels=setNames(as.character(boxData$Classification), boxData$temp))

ggplot output

This is what you would expect, right?

like image 105
Damiano Fantini Avatar answered Nov 19 '22 20:11

Damiano Fantini