Related
This was the question where it was shown how to use the concept.
I have naive question about this function which was give here which is this where it assigns predicted levels the cluster.
pred2labels = function(pred,actual){
pred = as.character(pred)
actual = as.character(actual)
tab = as.matrix(table(pred,actual))
assignment = colnames(tab)[max.col(tab)]
names(assignment) = rownames(tab)
assignment[pred]
}
I tried to do the same my question is do i need to generate a predicted labels for my data of clusters?
here is my data frame
dput(bb)
structure(list(FAB = structure(c(4L, 2L, 5L, 3L, 4L, 5L, 4L,
4L, 5L, 3L, 4L, 2L, 4L, 3L, 2L, 3L, 5L, 5L, 4L, 3L, 2L, 5L, 3L,
5L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 2L, 3L, 5L, 3L, 5L, 3L,
2L, 1L, 6L, 6L, 5L, 5L, 5L, 6L, 6L, 3L, 7L, 3L, 5L, 6L, 2L, 5L,
2L, 3L, 3L, 2L, 6L, 2L, 2L, 2L, 2L, 1L, 6L, 2L, 5L, 2L, 2L, 9L,
5L, 1L, 5L, 2L, 5L, 5L, 6L, 2L, 3L, 6L, 5L, 2L, 1L, 8L, 3L, 5L,
3L, 6L, 1L, 2L, 2L, 5L, 3L, 5L, 6L, 5L, 5L, 3L, 5L, 3L, 2L, 3L,
3L, 2L, 6L, 1L, 2L, 3L, 6L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 2L,
2L, 5L, 2L, 3L, 2L, 3L, 5L, 1L, 3L, 1L, 6L, 5L, 5L, 3L, 5L, 3L,
2L, 1L, 2L, 5L, 7L, 8L, 6L, 2L, 8L, 3L, 3L, 1L, 2L, 2L, 2L, 1L,
3L, 6L, 5L, 3L, 1L, 2L, 3L, 2L, 1L, 3L, 5L, 2L, 9L, 2L, 1L, 1L,
2L, 6L, 6L), .Label = c("M0", "M1", "M2", "M3", "M4", "M5", "M6",
"M7", "nc"), class = "factor"), RISK_CYTO = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L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, 2L, 2L, 4L, 2L, 3L, 2L,
4L, 3L, 5L, 4L, 6L, 2L, 5L, 4L, 3L, 4L, 5L, 4L, 2L, 4L, 6L, 4L,
1L, 4L, 5L, 6L, 1L, 4L, 4L, 5L, 4L, 2L, 3L, 5L, 3L, 5L, 2L, 2L,
4L, 2L, 1L, 4L, 3L, 5L, 5L, 6L, 2L, 2L, 3L, 6L, 1L, 5L, 5L, 5L,
5L, 3L, 3L, 6L, 5L, 4L, 6L, 3L, 5L, 5L, 5L, 5L, 5L, 2L, 1L, 5L,
6L, 5L, 5L, 6L, 2L, 2L)), row.names = c(NA, -170L), class = "data.frame")
My steps were this
library(irr)
clus_arrange = bb %>% dplyr::select(Class,FAB)
names(clus_arrange)[1] = "clus"
clus_arrange$predicted_label = pred2labels(clus_arrange$clus,clus_arrange$FAB)
kappam.light(cluster_r)
My output is this
Light's Kappa for m Raters
Subjects = 170
Raters = 2
Kappa = 0.266
z = 6.62
p-value = 3.58e-11
My question is the approach right way of doing it what i had followed from that answer?
UPDATED ANSWER BASED ON THIS TUTORIAL
table <- table(clus_arrange$FAB, clus_arrange$clus)
table
kappam.fleiss(table, detail=TRUE)
My question is which one is methodically and logically correct
I am working on a a model with lmer where I would like to get estimated marginal means with the emmeanslibrary. This is my dataframe:
df <- structure(list(treatment = structure(c(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), .Label = c("CCF", "UN"), class = "factor"), level = structure(c(2L,
3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L,
4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L,
2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L,
3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L,
4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L,
2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L,
3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L), .Label = c("A", "F", "H", "L"
), class = "factor"), random = structure(c(3L, 3L, 3L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 4L,
4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L), .Label = c("1.6", "2", "3.2", "5", NA), class = "factor"),
continuous = c(72.7951770264767, 149.373765810534, 1.64153094886205,
54.6697408615215, 25.5801835808851, 1.45794117443253, 25.3660934894788,
91.2321704897132, 2.75353217433675, 44.1995276851725, 33.1854545470435,
5.36536076058866, 29.6807620242672, 80.6077496067764, 0.833434180091457,
13.6789475327185, 77.4930412025109, 3.65998714174906, 25.2848344605563,
136.632099849828, 2.56715261161435, 28.6733878840584, 66.800616194317,
1.37475468782539, 23.007491380183, 84.980285774607, 1.13569710795522,
33.8610875632139, 56.1234827517798, 1.32327007970416, 60.0843812879313,
43.4487832450889, 1.14942423621912, 53.6673704529947, 146.746167255051,
3.91593723271292, 27.0321687961004, 89.5925729244878, 1.47707078226047,
44.0523211310831, 115.087908243373, 1.94039630728038, 86.4074806697431,
43.3266206881612, 2.81456503996437, 66.868588961071, 229.797526052566,
1.07971524769264, 30.3390107111747, 116.680801084036, 1.67711446647817,
69.0961010697534, 78.5454363192614, 1.92137892126384, 53.5708546850303,
37.7175476710608, 1.96087397451467, 25.5166981770257, 37.3755071788757,
2.21602000526086, 10.3266195584378, 38.1458490762217, 2.7508022340832,
44.5864920143771, 8.45382647692274, 2.63204944520792, 87.5376946978593,
27.2354119098268, 3.38134648323956, 26.8815471706502, 14.5539972194568,
2.0556994322415, 27.4619977737491, 32.8546665896602, 2.66809379088059,
42.3815445857533, 21.3359802201685, 2.19167325121191, 53.3189825439001,
13.5708790223439, 2.22274607227071, 88.297423835906, 8.50554349658773,
3.5764241495006, 29.284865737912, 21.1213079519954, 2.3070166819956,
10.7659615128225, 33.4813413290485, 2.49896565066211, 59.0935696616465,
13.2863515051715, 4.36424795471221, 72.1627847396763, 9.09326343200557,
2.13701784901259, 27.5824079679471, 8.84486812842272, 1.98293342019671,
17.5321126287485, 19.1806349705231, 5.03952187899644, 58.3473975730234,
9.17287686145614, 2.99575072457674)), class = "data.frame", row.names = c(NA,
105L))
This is my model:
library(lme4)
model <- lmer((continuous) ~ treatment + level + (1|random), data= df, REML = TRUE)
The data as it is does not meet the model assumptions, but still I am wondering why I get a negative estimated marginal mean (response) on treatment "UN" level "L" (see lettering table) when I don't have any negative numbers in df$continuous?
library(multcompView)
library(emmeans)
lsm.mixed_C <- emmeans::emmeans(my_model,pairwise ~ treatment * level, type="response")
lettering <- CLD(lsm.mixed_C,alpha=0.05,Letters=letters,
adjust= "tukey")
The short answer is because you badly need to include the interaction in your model. Compare:
model2 <- lmer((continuous) ~ treatment * level + (1|random),
data= df, REML = TRUE)
emmip(model2, treatment ~ level)
with:
emmip(model, treatment ~ level)
In model2, both EMMs at level L are close to zero. If you remove the interaction from the model, you force those two profiles to be parallel, while maintaining a sizeable positive difference between treatments CCF and UN, forcing the estimate for UN to go negative. In actual fact, though, all six estimates for treatment x level combinations are seriously distorted.
I can't repeat it enough. emmeans() summarizes a model. If you give it a bad model, you get dumb results. Thanks for the great illustration of this point.
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))
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:
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!
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))
This is what you would expect, right?
When I make a barplot with significance letters from anova above the bars, I use following code:
anova_NDW_geel<-aov(nodule_dry_weight~treatment,inoculatieproef_geel_variety2)
HSD_NDW_geel <- HSD.test(anova_NDW_geel,"treatment",alpha=0.05,group=TRUE)$groups
HSD_NDW_means_geel <- HSD.test(anova_NDW_geel,"treatment",alpha=0.05,group=TRUE)$means
HSD_NDW_means_geel <- HSD_NDW_means_geel[order(-HSD_NDW_means_geel$nodule_dry_weight),]
p_HSD_NDW_geel <- ggplot(aes(x=treatment, y=NDW_mean_geel, width=0.6), data=inoculatieproef_mean_geel)+
geom_bar(stat="identity", data=HSD_NDW_geel, aes(x=trt, y=means), fill="gray40")+
geom_text(data=HSD_NDW_geel, aes(x=trt, y=means, label=M), size=5, vjust=-1, hjust=1)+
ggtitle("Zand")+
ylab("Droog gewicht wortelknolletjes (g)")+
xlab("Behandeling")+
geom_errorbar(aes(ymin=NDW_mean_geel-NDW_sd_geel,ymax=NDW_mean_geel+NDW_sd_geel),
position=position_dodge(width=0.5),width=0.1,size=0.3)+
theme_bw() +
theme(axis.line = element_line(colour="black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())+
scale_y_continuous(expand = c(0, 0))+
theme(axis.text.x = element_text(angle = 0, hjust = 1, vjust = 0.5))+
theme(text = element_text(size=12))
which results in following graph: http://i.stack.imgur.com/bZidZ.png
This is probably not the best way to do this and when I want to add the letters to the barplots with facet wrap.
Here is a sample of the data I want to make a facet wrap with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 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("2014",
"2015"), class = "factor"), nodule_dry_weight = c(0, 0.0467,
0.0328, 0.0885, 0.0081, 0.1086, 0.0788, 0.0267, 0, 0.0128, 0.0143,
0.0333, 0.006, 0.098, 0.0286, 0.011, 0, 0.0627, 0.0769, 0.0784,
0.023, 0.1504, 0.1026, 0.0254, 0, 0.0597, 0.0158, 0.0354, 0.0226,
0.3261, 0.0436, 0, 0, 0.0203, 0.0469, 0.0904, 0.1593, 0.0836,
0.056, 0.0037, 0, 0.0534, 0.0901, 0.0435, 0.0248, 0.0435, 0.0279,
0.0029, 0, 0.0545, 0.038, 0.0991, 0.0099, 0.1453, 0.1096, 0.0272,
0, 0.0319, 0.0624, 0.0508, 0.0415, 0.11, 0.0079, 0, 0, 0.1257,
0.1242, 0.2899, 0.024, 0.2175, 0.2979, 0.0396, 0, 0.1583, 0.2935,
0.2541, 0.1027, 0.4196, 0.2059, 0.0396, 0, 0.0891, 0.167, 0.0907,
0.2153, 0.3063, 0.2921, 0.0528, 0, 0.0928, 0.2109, 0.1514, 0.0821,
0.3607, 0.0996, 0.0069, 0, 0.0685, 0.3109, 0.1862, 0.0393, 0.286,
0.3418, 0.0459, 0, 0.0765, 0.3486, 0.3988, 0.1155, 0.6341, 0.3653,
0.039, 0, 0.0766, 0.3112, 0.1988, 0.05, 0.2856, 0.34, 0.0862,
0, 0.2621, 0.1146, 0.393, 0.1644, 0.3415, 0.1343, 0.019, 0, 0.0976,
0.1853, 0.0691, 0.0248, 0.1764, 0.1244, 0.1525, 0, 0.1529, 0.1069,
0.2833, 0.0204, 0.2966, 0.2371, 0.1464, 0, 0.0691, 0.2094, 0.1633,
0.0264, 0.1344, 0.0694, 0.1175, 0, 0.1783, 0.1434, 0.2136, 0.0873,
0.19, 0.1683, 0.1927, 0, 0.0571, 0.0599, 0.1061, 0.0244, 0.1256,
0.0894, 0.0123, 0, 0.1696, 0.1046, 0.2164, 0.0939, 0.1552, 0.2942,
0.1652, 0, 0.0844, 0.102, 0.0227, 0.025, 0.0654, 0.1234, 0.0702,
0, 0.0979, 0.1246, 0.0958, 0.0867, 0.1104, 0.1969, 0.227, 0,
0.3704, 0.4727, 0.2527, 0.2078, 0.3377, 0.308, 0.1293, 0, 0.2417,
0.3744, 0.2916, 0.1773, 0.433, 0.2446, 0.1382, 0, 0.4718, 0.4271,
0.4882, 0.1799, 0.4178, 0.518, 0.3915, 0, 0.3421, 0.3804, 0.2112,
0.4292, 0.3829, 0.1315, 0.2719, 0, 0.3197, 0.6867, 0.414, 0.3112,
0.2914, 0.4994, 0.369, 0.0256, 0.1494, 0.5577, 0.2538, 0.3854,
0.4151, 0.544, 0.4009, 0, 0.5208, 0.2962, 0.4175, 0.2689, 0.3374,
0.5075, 0.3601, 0, 0.704, 0.4631, 0.4573, 0.154, 0.5087, 0.4319,
0.4155)), .Names = c("treatment", "block", "position", "variety",
"location", "year", "nodule_dry_weight"), row.names = c(NA, -256L
), class = "data.frame")
I use following code for my graph with facet wrap:
inoculatieproef <- inoculatieproef %>%
group_by(treatment, location, variety, year) %>%
mutate(NDW_mean = mean(nodule_dry_weight),
NDW_sd = sd(nodule_dry_weight))
ggplot(data=inoculatieproef,aes(x=treatment, y=NDW_mean))+
facet_wrap(~location*variety*year,ncol=2)+
geom_bar(position="dodge", stat="identity")+
geom_errorbar(aes(ymin = NDW_mean - NDW_sd,
ymax = NDW_mean + NDW_sd),
width=0.1,size=0.3,
color = "darkgrey")+
theme_bw() +
theme(axis.line = element_line(colour="black"),
panel.grid.minor = element_blank(),
panel.background = element_blank())
How do I add on each barplot the significance letters (anova) in de the facet wrap graph?
No idea if the test fits your data distribution, but you can start with that:
library(tidyverse)
stat_pvalue <- dd %>%
group_by(location, variety, year) %>%
rstatix::t_test(nodule_dry_weight~treatment) %>%
filter(p < 0.05) %>%
group_by(location, variety, year) %>%
rstatix::add_significance("p") %>%
rstatix::add_y_position() %>%
mutate(y.position = seq(min(y.position), max(y.position),length.out = n())*1.1) %>%
ungroup()
ggplot(data=dd,aes(x=treatment, y=nodule_dry_weight))+
geom_boxplot() +
facet_wrap(~location + variety + year,ncol=2, scales = "free_y") +
ggpubr::stat_pvalue_manual(stat_pvalue, label = "p")
The following code is a minimal (for some value of minimal....) example that uses lattice to produce boxplots. But the median line on those boxplot is a) coloured and b) very thin. How to get them to be black and tick?
a71<-structure(list(n = structure(c(1L, 2L, 2L, 4L, 4L, 1L, 1L, 4L,
2L, 1L, 1L, 2L, 2L, 4L, 2L, 2L, 3L, 4L, 1L, 2L, 2L, 3L, 2L, 2L,
2L, 4L, 3L, 3L, 4L, 2L, 4L, 2L, 1L, 3L, 2L, 3L, 4L, 1L, 4L, 1L,
3L, 3L, 2L, 1L, 1L, 3L, 3L, 1L, 2L, 4L, 3L, 2L, 3L, 1L, 4L, 1L,
4L, 2L, 3L, 4L, 4L, 4L, 1L, 3L, 3L, 3L, 4L, 2L, 2L, 2L, 4L, 4L,
4L, 1L, 4L, 3L, 2L, 2L, 4L, 4L, 3L, 2L, 2L, 2L, 1L, 2L, 3L, 3L,
3L, 1L, 3L, 3L, 4L, 1L, 3L, 2L, 1L, 3L, 1L, 2L), .Label = c("100",
"200", "400", "800"), class = "factor"), g = structure(c(3L,
3L, 1L, 3L, 1L, 3L, 2L, 1L, 1L, 3L, 1L, 2L, 3L, 1L, 2L, 2L, 1L,
3L, 1L, 2L, 3L, 2L, 2L, 2L, 3L, 1L, 1L, 3L, 3L, 2L, 1L, 1L, 3L,
1L, 3L, 3L, 1L, 2L, 2L, 2L, 2L, 3L, 2L, 3L, 3L, 1L, 3L, 3L, 1L,
1L, 1L, 2L, 3L, 1L, 3L, 1L, 3L, 3L, 1L, 2L, 1L, 2L, 2L, 1L, 3L,
3L, 1L, 3L, 2L, 3L, 1L, 3L, 1L, 1L, 3L, 3L, 2L, 3L, 3L, 3L, 1L,
3L, 3L, 2L, 3L, 3L, 2L, 2L, 2L, 3L, 2L, 2L, 3L, 1L, 2L, 3L, 3L,
3L, 1L, 3L), .Label = c("0", "0.5", "1"), class = "factor"),
cr = structure(c(1L, 2L, 3L, 1L, 3L, 3L, 2L, 1L, 2L, 3L,
3L, 2L, 2L, 3L, 2L, 2L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 1L, 1L,
3L, 3L, 3L, 1L, 3L, 1L, 1L, 1L, 2L, 1L, 1L, 3L, 2L, 3L, 1L,
3L, 2L, 2L, 2L, 3L, 2L, 3L, 1L, 1L, 2L, 1L, 2L, 2L, 3L, 1L,
1L, 1L, 1L, 3L, 3L, 2L, 3L, 1L, 3L, 3L, 3L, 3L, 1L, 1L, 1L,
1L, 1L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 3L, 3L, 2L, 1L, 2L,
2L, 2L, 2L, 3L, 2L, 2L, 1L, 3L, 2L, 1L, 3L, 1L, 1L, 2L, 2L
), .Label = c("-0.4", "0", "0.4"), class = "factor"), bias = c(0.0162558992812201,
0.138354243932496, 0.0205686041691062, 0.269714433604472,
0.381044037439145, 0.0869422119950729, 0.331379037601084,
0.686894150152472, 0.0140922903231885, 0.225078933454863,
0.554444988164574, 0.076032683077827, 0.335284040888653,
0.0630810396519646, 0.358402154233125, 0.260940142571834,
0.141353291599136, 0.0220267076189838, 0.242149484071382,
0.278319984858078, 0.193105829691662, 0.0259815643559331,
0.318504899459259, 0.00277002060524357, 0.212681621053374,
0.418358846098857, 0.358916156777489, 0.438248724241505,
0.194398889511096, 0.2266870834128, 0.144338808446284, 0.149227951210927,
0.268111328952192, 0.123265441389974, 0.0376832357983068,
0.0353605481767078, 0.021227873083535, 0.0385614926552725,
0.130640111978654, 0.161865326447675, 0.174151298764213,
0.292085797406362, 0.198391364913347, 0.0779507859721407,
0.0045571464157577, 0.114734038438965, 0.0469613758623325,
0.64238405800387, 0.74508519247034, 0.0251182457091362, 0.217835062247358,
0.131159910126724, 0.130034859007596, 0.222418419987533,
0.0861715693619894, 0.185660520258661, 0.0940670543815277,
0.105680179626893, 0.215966730684923, 0.109008340760604,
0.0474735195202623, 0.192326789813641, 0.022147195644035,
0.277372858009381, 0.237574293593955, 0.123383946121193,
0.46406480500022, 0.123698482002945, 0.671442441453945, 0.0406004813894845,
0.260472754754191, 0.0151116521560003, 0.0422855023583402,
0.0405517218780402, 0.0441583998205882, 0.0958995639409343,
0.37588506579263, 0.098494760958735, 0.0928763466294421,
0.111205748449328, 0.413083543393392, 0.0138839674143682,
0.22407421093074, 0.72309883706409, 0.423231501875638, 0.141932050342199,
0.133808548118004, 0.331500621801688, 0.127652280721512,
0.132083126730013, 0.261864564503826, 0.208243130464985,
0.18657049493156, 0.333701537602998, 0.404884075502013, 0.470789398932934,
0.115008599462104, 0.177984001517338, 0.331717679106776,
0.0862418839846533), group = structure(c(3L, 2L, 2L, 3L,
1L, 3L, 3L, 2L, 2L, 1L, 3L, 2L, 2L, 2L, 1L, 3L, 2L, 3L, 1L,
3L, 1L, 2L, 1L, 3L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 3L, 1L, 1L,
3L, 2L, 3L, 1L, 3L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 3L, 1L, 2L,
3L, 3L, 3L, 1L, 1L, 3L, 2L, 3L, 1L, 1L, 2L, 2L, 2L, 2L, 1L,
2L, 1L, 1L, 1L, 3L, 2L, 2L, 1L, 1L, 3L, 3L, 3L, 2L, 1L, 3L,
3L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 3L, 3L, 1L, 1L, 2L, 3L,
1L, 1L, 3L, 2L, 1L, 3L), .Label = c("1", "2", "3"), class = "factor")), .Names = c("n",
"g", "cr", "bias", "group"), row.names = c(8721L, 6970L, 6686L,
9624L, 352L, 10545L, 7505L, 4216L, 6170L, 3309L, 10429L, 4302L,
5602L, 5680L, 1530L, 9234L, 5007L, 8004L, 721L, 10038L, 502L,
4891L, 2946L, 8502L, 622L, 1972L, 2403L, 3383L, 5880L, 1038L,
4756L, 9506L, 2169L, 1023L, 8506L, 6239L, 7768L, 3221L, 9536L,
5981L, 1507L, 4883L, 414L, 3117L, 3993L, 1923L, 9143L, 2673L,
4430L, 9520L, 9363L, 10602L, 95L, 1141L, 9660L, 4285L, 10704L,
154L, 531L, 6440L, 4876L, 7052L, 4397L, 3375L, 5075L, 1295L,
2620L, 334L, 9510L, 4690L, 4288L, 3576L, 2248L, 7693L, 8820L,
8135L, 4026L, 1906L, 10164L, 8616L, 423L, 5290L, 418L, 6486L,
4485L, 7042L, 955L, 2215L, 9031L, 8049L, 2323L, 1627L, 4212L,
8689L, 439L, 2590L, 8649L, 5447L, 1957L, 10570L), class = "data.frame")
library(lattice)
cl<-c('red','green','blue')
mypanel<-function(...){
panel.bwplot(...,pch="|",col="black",cex=4,fill=cl)
}
o1<-bwplot(a71$bias~a71$group|a71$cr*a71$g,type=c("l","g"),ylim=c(0,1),panel=mypanel)
plot(o1)
By changing some of the parameters of box.rectangle (a lattice-specific graphical parameter), you can manipulate the lines (including the median line) surrounding each of the box plots. This will change all the lines around the boxes, however, not just the median line.
myPars <- list(box.rectangle = list(lwd = 2, col = "black"))
lwd changes the line width (thickness). colchanges the color of the lines. Then pass this list to the par.settings argument in bwplot.
o1 <- bwplot(a71$bias ~ a71$group | a71$cr * a71$g,
type = c("l", "g"), ylim = c(0, 1), panel = mypanel,
par.settings = myPars)
plot(o1)
To see all of the parameters associated with box.rectangle, use
trellis.par.get("box.rectangle")
OP is happy with all lines thicker by #BenBarnes, but for completeness, it is possible to just thicken the median line. Using the fact that box.width defaults to 1/2:
bwplot(a71$bias~a71$group|a71$cr*a71$g,type=c("l","g"),ylim=c(0,1),
panel=function(x,y,...){
panel.bwplot(x,y,...,pch="|",col="black",cex=4,fill=cl)
medy <- by(y,list(x),median)
xx <- sort(unique(as.numeric(x)))
panel.segments(xx-.25,medy,xx+.25,medy,lwd=2)
}
)