本文介绍了Boxplot有两个级别和多个data.frames的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在每个data.frame中有4个data.frames和两个factor级别。下面转载df1。请重复df1生成df2 ... df4。 如何使用ggplot2生成boxplots,使得我的最终图与下图非常相似?图中的季节代表数据框的名称,而现在和未来代表的是水平名称,图例代表重型,重型,重量级数据。红线。 1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,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),标签= c(NN,SS ),class =factor), heavy = c(0.13623 0125,0.136281211,0.136038018,0.135392862, 0.137088902,0.136028293,0.13640057,0.135317058,0.13688615, 0.136448994,0.137089424,0.136810847,0.135865471,0.136130096, 0.136361327,0.137796714,0.136052839,0.135892646,0.13544437, 0.136452363,0.135367421,0.135617509,0.138202559,0.135396942, 0.135930092,0.135661805,0.135666,0.135860128,0.137648687, 0.136057353,0.136057731,0.135162399,0.136080113,0.135285036, 0.136204839,0.138058091, 0.137215664,0.135696637,0.1356.3902, 0.135733243,0.13874445,0.136632122,0.1137787919,0.1135033093, 0.136926798,0.1136766413,0.13690947,0.135203152,0.138370968, 0.136862356,0.1136083112,0.138212845,0.135964773,0.13583601, 0.134923731,0.135828965,0.136272539,0.138127602,0.1137028323, 0.136526836,0.136407397,0.1137025373,0.138358757,0.1137858521, 0 .135464076,0.136302506,0.135528362,0.137540677,0.136455865 0.138470144,0.17227895,0.136296955,0.16792631,0.1585875782 0.13815733 0.136383864 0.136696618 0.13857652 0.136700903 0.136743873 0.136033619 0.135970522 0.135816385 ,0.136003984, 0.136583925,0.136768202,0.136292002,0.16316737,0.136540075, 0.136051218,0.113524119,0.136736303,0.13646894,0.136266073, 0.136263692,0.1636330301,0.163611577,0.1355707095,0.136769488, 0.136072466 ,0.135564224,0.136496131,0.137659507,0.136704681, 0.136542173,0.136777403,0.135771538,0.13665463,0.136984748, 0.137717859,0.138195237,0.132232227,0.135956814),较重= c(0.227332679, 0.227200132,0.2227299118, 0.227289816,0.22224478,0.2227082442, 0.227861315,0.2227055561,0.2227112284,0.222655388,0.2221581212, 0.228789678,0.222188949,0.2228850198,0.22272 46991,0.227359368, 0.227359531,0.2227310607,0.2229490445,0.2227295226,0.222958185, 0.228104958,0.222254823,0.22215392,0.2228062515,0.222509559, 0.227143662,0.230048719,0.2227860836,0.22267792,0.222263728, 0.227222794,0.227165592,0.227140611,0.228424335,0.227356425, 0.227243374,0.228936267,0.227320467,0.22738371,0.227694891, 0.227270428,0.227751798,0.228803279,0.227330453,0.229679261, 0.228999206,0.227227604,0.227247085,0.227198567, 0.229234921, 0.227211613,0.223007234,0.2226793036,0.226474338,0.226654333, 0.229964991,0.22280328,0.22200099,0.2226640822,0.2227522393, 0.227463578,0.2227832692,0.222293936,0.230154101,0.229813709, 0.22761097, 0.227445308,0.228669159,0.22260339,0.229017398, 0.230421347,0.2227041103,0.2227583471,0.229547568,0.222676335, 0.226737661,0.2229922588, 0.226907188,0.227102239,0.226469073, 0.230680908,0.2227763879,0.2226882448,0.2226741993,0.2226693024, 0.22671415,0.222777762,0.2227795194,0.2228983096,0.226647946, 0.226799552,0.22275218,0.222692942,0.2226601519,0.2227098192,0.27989292, 0.226886889,0.226959012,0.2226552119,0.222809761,0.2226786285, 0.226709252,0.2226834015,0.2228033943,0.2226693494,0.22248613, 0.227608804,0.222685023,0.2226586619,0.2227718907,0.2228890098, 0.226701909,0.230919944),最重的= c(0.316870607,0.316772978, 0.316851707,0.317017543,0.316673994,0.317224709,0.319234458, 0.31861305,0.319804304,0.318605816,0.316930034,0.31688398, 0.316789552,0.320783976,0.317094325,0.31809319,0.317134565, b $ b 0.318173976,0.317213167,0.317084404,0.321712205,0.317128056, 0.316866913,0.3170489,0.31712423,0.31684494,0.319497635, 0.3169323 01,0.316864646,0.317279005,0.316887692,0.317134437, 0.316792589,0.320894499,0.319883014,0.316924639,0.316575642, 0.31686389,0.316985994,0.321566256,0.316683995,0.320299883, 0.317308965,0.318151948,0.316479828,0.319857732, 0.317171909, 0.322137849,0.316526917,0.316870364,0.32205784,0.317055758, 0.320329144,0.318015397,0.318719989,0.317910658,0.317292016, 0.321348723,0.319915048,0.317160762,0.318773245,0.319627925, 0.31869767, 0.322422407,0.32082693,0.318034899,0.318760783,0 0.318325502,0.320739086,0.317216142,0.32284544,0.319466593, 0.318740499,0.317489944,0.319064923,0.322014928,0.317353897, 0.318904583,0.317931141,0.3323295254,0.318924712,0.318965677,0.318965677,0.318965677, 0.317700019,0.31793468,0.317699508,0.317168657,0.318903983, 0.317493401,0.317511406,0.317483897,0.31748495,0.317776804, 0.318893431,0.317663608,0.316978585,0.317473467,0.317500429, 0.317144259,0.317330826,0.317610353,0.317881476,0.31707787, 0.317728374,0.317452137,0.31938939,0.317199373,0.31898747, 0.318878952,0.317987024,0.318951952 ,0.318419561,0.319568088, 0.321165413)),.Names = c(id,heavy,heavier,heaviest),class =data.frame,row.names = c(NA,-113L)) 解决方案 ##创建一些data.frames:这会生成一个包含四个dfs createDF< - quote(data.frame(id = sample(c(NN,SS )100,rep = T), heavy = runif(100), heavyier = runif(100), heaviest = runif(100))) dfs< - lapply(1:4,function(i)eval(createDF)) ##连接并塑造它们 library(reshape2) dat dat $ dfid< - paste(d f,rep(1:4,times = sapply(dfs,nrow))) dat ggplot(dat,aes(id,value,group = interaction(variable,id),fill = variable))+ geom_boxplot()+ facet_grid(〜dfid) I have 4 data.frames with two factor levels in each data.frame. df1 is reproduced below. Please duplicate df1 to produce df2...df4.How can I produce boxplots with ggplot2 such that my final figure looks very similar to the figure below? The seasons in the figure represent the dataframe names while present and future represent level names and the legend represents heavy, heavy, heaviest in the data reproduced here.Ignore the dotted horizontal red line.df1= structure(list(id = 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, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 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("NN", "SS"), class = "factor"), heavy = c(0.136230125, 0.136281211, 0.136038018, 0.135392862, 0.137088902, 0.136028293, 0.13640057, 0.135317058, 0.13688615, 0.136448994, 0.137089424, 0.136810847, 0.135865471, 0.136130096, 0.136361327, 0.137796714, 0.136052839, 0.135892646, 0.13544437, 0.136452363, 0.135367421, 0.135617509, 0.138202559, 0.135396942, 0.135930092, 0.135661805, 0.135666, 0.135860128, 0.137648687, 0.136057353, 0.136057731, 0.135162399, 0.136080113, 0.135285036, 0.136204839, 0.138058091, 0.137215664, 0.135696637, 0.135863902, 0.135733243, 0.138274445, 0.136632122, 0.137787919, 0.135033093, 0.136926798, 0.136766413, 0.13690947, 0.135203152, 0.138370968, 0.136862356, 0.136083112, 0.138212845, 0.135964773, 0.13583601, 0.134923731, 0.135828965, 0.136272539, 0.138127602, 0.137028323, 0.136526836, 0.136407397, 0.137025373, 0.138358757, 0.137858521, 0.135464076, 0.136302506, 0.135528362, 0.137540677, 0.136455865, 0.138470144, 0.137227895, 0.136296955, 0.136792631, 0.135875782, 0.13815733, 0.136383864, 0.136696618, 0.13857652, 0.136700903, 0.136743873, 0.136033619, 0.135970522, 0.135816385, 0.136003984, 0.136583925, 0.136768202, 0.136292002, 0.136316737, 0.136540075, 0.136051218, 0.135924119, 0.136736303, 0.136946894, 0.136266073, 0.136263692, 0.136399301, 0.13611577, 0.135857095, 0.136769488, 0.136072466, 0.135564224, 0.136496131, 0.137659507, 0.136704681, 0.136542173, 0.136777403, 0.135771538, 0.13665463, 0.136984748, 0.137717859, 0.138195237, 0.136232227, 0.135956814), heavier = c(0.227332679, 0.227200132, 0.227299118, 0.227289816, 0.22724478, 0.227082442, 0.227861315, 0.227055561, 0.227112284, 0.228651438, 0.228158412, 0.228789678, 0.227188949, 0.228850198, 0.227246991, 0.227359368, 0.227359531, 0.227310607, 0.229490445, 0.227295226, 0.227958185, 0.228104958, 0.227254823, 0.22715392, 0.228062515, 0.227509559, 0.227143662, 0.230048719, 0.227860836, 0.228467792, 0.227263728, 0.227222794, 0.227165592, 0.227140611, 0.228424335, 0.227356425, 0.227243374, 0.228936267, 0.227320467, 0.22738371, 0.227694891, 0.227270428, 0.227751798, 0.228803279, 0.227330453, 0.229679261, 0.228999206, 0.227227604, 0.227247085, 0.227198567, 0.229234921, 0.227211613, 0.23007234, 0.226793036, 0.226474338, 0.226654333, 0.229964991, 0.22880328, 0.22700099, 0.226640822, 0.227522393, 0.227463578, 0.227832692, 0.227293936, 0.230154101, 0.229813709, 0.22761097, 0.227445308, 0.228669159, 0.22660539, 0.229017398, 0.230421347, 0.227041103, 0.227583471, 0.229547568, 0.22676335, 0.226737661, 0.229922588, 0.226907188, 0.227102239, 0.226469073, 0.230680908, 0.227763879, 0.226882448, 0.226741993, 0.226693024, 0.22671415, 0.226773662, 0.227795194, 0.226983096, 0.226647946, 0.226799552, 0.226759218, 0.22692942, 0.226601519, 0.227098192, 0.226886889, 0.226959012, 0.226552119, 0.226809761, 0.226786285, 0.226709252, 0.226834015, 0.228033943, 0.226693494, 0.22748613, 0.227608804, 0.22685023, 0.226586619, 0.227718907, 0.228890098, 0.226701909, 0.230919944), heaviest = c(0.316870607, 0.316772978, 0.316851707, 0.317017543, 0.316673994, 0.317224709, 0.319234458, 0.31861305, 0.319804304, 0.318605816, 0.316930034, 0.31688398, 0.316789552, 0.320783976, 0.317094325, 0.31809319, 0.317134565, 0.318173976, 0.317213167, 0.317084404, 0.321712205, 0.317128056, 0.316866913, 0.3170489, 0.31712423, 0.31684494, 0.319497635, 0.316932301, 0.316864646, 0.317279005, 0.316887692, 0.317134437, 0.316792589, 0.320894499, 0.319883014, 0.316924639, 0.316575642, 0.31686389, 0.316985994, 0.321566256, 0.316683995, 0.320299883, 0.317308965, 0.318151948, 0.316479828, 0.319857732, 0.317171909, 0.322137849, 0.316526917, 0.316870364, 0.322205784, 0.317055758, 0.320329144, 0.318015397, 0.318719989, 0.317910658, 0.317292016, 0.321348723, 0.319915048, 0.317160762, 0.318773245, 0.319627925, 0.31869767, 0.322422407, 0.32082693, 0.318034899, 0.318760783, 0.318325502, 0.320739086, 0.317216142, 0.32284544, 0.319466593, 0.318740499, 0.317489944, 0.319064923, 0.322014928, 0.317353897, 0.318904583, 0.317931141, 0.323295254, 0.318924712, 0.318965677, 0.317700019, 0.31793468, 0.317699508, 0.317168657, 0.318903983, 0.317493401, 0.317511406, 0.317483897, 0.31748495, 0.317776804, 0.318893431, 0.317663608, 0.316978585, 0.317473467, 0.317500429, 0.317144259, 0.317330826, 0.317610353, 0.317881476, 0.31707787, 0.317728374, 0.317452137, 0.31938939, 0.317199373, 0.31898747, 0.318878952, 0.317987024, 0.318951952, 0.318419561, 0.319568088, 0.321165413)), .Names = c("id", "heavy", "heavier", "heaviest" ), class = "data.frame", row.names = c(NA, -113L)) 解决方案 ## create some data.frames: this results in a list of four dfscreateDF <- quote(data.frame(id=sample(c("NN", "SS"), 100, rep=T), heavy=runif(100), heavier=runif(100), heaviest=runif(100)))dfs <- lapply(1:4, function(i) eval(createDF))## join and shape themlibrary(reshape2)dat <- do.call(rbind, dfs)dat$dfid <- paste("df", rep(1:4, times=sapply(dfs, nrow)))dat <- melt(dat, id.vars=c("id", "dfid"))ggplot(dat, aes(id, value, group=interaction(variable, id), fill=variable)) + geom_boxplot() + facet_grid(~dfid) 这篇关于Boxplot有两个级别和多个data.frames的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-21 05:46