Group variables in a boxplot in R

Posted by tao.hong on Stack Overflow See other posts from Stack Overflow or by tao.hong
Published on 2012-11-23T20:00:43Z Indexed on 2012/11/23 23:04 UTC
Read the original article Hit count: 292

Filed under:
|
|

I am trying to generate a boxplot whose data come from two scenarios. In the plot, I would like to group boxes by their names (So there will be two boxes per variable). I know ggplot would be a good choice. But I got errors which I could not figure out. Can anyone give me some suggestions?

sensitivity_out1

structure(c(0.0522902104339716, 0.0521369824334004, 0.0520240345973737, 
0.0519818337359876, 0.051935071418996, 0.0519089404325544, 0.000392698277338341, 
0.000326135474295325, 0.000280863338343747, 0.000259631566041935, 
0.000246594043996332, 0.000237923540393391, 0.00046732650331544, 
0.000474448907808135, 0.000478287273678457, 0.000480194683464109, 
0.000480631753078668, 0.000481760272726273, 0.000947965771207979, 
0.000944821699830455, 0.000939631071343889, 0.000937186900570605, 
0.000936007346568281, 0.000934756220144141, 0.00132442589501872, 
0.00132658367774979, 0.00133334696220742, 0.00133622384928092, 
0.0013381577476241, 0.00134005741746304, 0.0991622968751298, 
0.100791399440082, 0.101946808417405, 0.102524244727408, 0.102920085260477, 
0.103232984259916, 0.0305219507186844, 0.0304635269233494, 0.0304161055015213, 
0.0303742106794513, 0.0303381888169022, 0.0302996157711171, 1.94268588634518e-05, 
2.23991225564447e-05, 2.5756135487907e-05, 2.79997917298194e-05, 
3.00753967077715e-05, 3.16270817369878e-05, 0.544701146678523, 
0.542887331601984, 0.541632986366816, 0.541005610554556, 0.540617004208336, 
0.540315690692195, 0.000453386694666078, 0.000448473414508756, 
0.00044692043197248, 0.000444826296854332, 0.000445747996014684, 
0.000444764303682453, 0.000127569551159321, 0.000128422491392669, 
0.00012933662856487, 0.000129941842982939, 0.000129578971489026, 
0.000131113075233758, 0.00684610571790029, 0.00686349387897349, 
0.00687468164010565, 0.00687880720347743, 0.00688275579317197, 
0.00687822247621936), .Dim = c(6L, 12L))

out2

structure(c(0.0189965816735366, 0.0189995096225103, 0.0190099362589894, 
0.0190033523148514, 0.01900896721937, 0.0190099427513381, 0.00192043989797585, 
0.00207303208721059, 0.00225931163225165, 0.0024049969048389, 
0.00252310364086785, 0.00262940166568126, 0.00195164921633517, 
0.00190079923515755, 0.00186139563778548, 0.00184188171395076, 
0.00183248544676564, 0.00182492970673969, 1.83038731485927e-05, 
1.98252671720347e-05, 2.14794764479231e-05, 2.30713122969332e-05, 
2.4484220713564e-05, 2.55958833705284e-05, 0.0428066864455102, 
0.0431686808647809, 0.0434411033615353, 0.0435883377765726, 0.0436690169266633, 
0.0437340464360965, 0.145288252474567, 0.141488776430307, 0.138204532539654, 
0.136281799717717, 0.134864952272761, 0.133738386148036, 0.0711728636959696, 
0.072031388688795, 0.0727536853228245, 0.0731581966147734, 0.0734424337399303, 
0.0736637270702609, 0.000605277151497094, 0.000617268349064968, 
0.000632975679951382, 0.000643904422677427, 0.000653775268094148, 
0.000662225067910141, 0.26735354610469, 0.267515415990146, 0.26753155165617, 
0.267553498616325, 0.267532284594615, 0.267510330320289, 0.000334158771646756, 
0.000319032383145857, 0.000306074699839994, 0.000299153278494114, 
0.000293956197852583, 0.000290171804454218, 0.000645975219899115, 
0.000637548672578787, 0.000632375486965757, 0.000629579821884212, 
0.000624956458229123, 0.000622456283217054, 0.0645188290106884, 
0.0651539609630352, 0.0656417364889907, 0.0658996698322889, 0.0660715073023965, 
0.0662034341510152), .Dim = c(6L, 12L))

Melt data:

  group variable value
1     1   PLDKRT     0
2     1   PLDKRT     0
3     1   PLDKRT     0
4     1   PLDKRT     0
5     1   PLDKRT     0
6     1   PLDKRT     0

Code:

#Data_source 1
sensitivity_1=rbind(sensitivity_out1,sensitivity_out2)
sensitivity_1=data.frame(sensitivity_1)
colnames(sensitivity_1)=main_l #variable names
sensitivity_1$group=1

#Data_source 2
sensitivity_2=rbind(sensitivity_out1[3:4,],sensitivity_out2[3:4,])
sensitivity_2=data.frame(sensitivity_2)
colnames(sensitivity_2)=main_l
sensitivity_2$group=2

sensitivity_pool=rbind(sensitivity_1,sensitivity_2)
sensitivity_pool_m=melt(sensitivity_pool,id.vars="group")

ggplot(data = sensitivity_pool_m, aes(x = variable, y = value)) + geom_boxplot(aes( fill= group), width = 0.8)

Error:

"Error in unit(tic_pos.c, "mm") : 'x' and 'units' must have length > 0" 

Update

Figure out the error. I should use geom_boxplot(aes( fill= factor(group)), width = 0.8) rather than fill= group

© Stack Overflow or respective owner

Related posts about r

    Related posts about ggplot2