在此示例中,在 tidyverse 和 ggplot 中订购箱线图的最简单方法是什么?

What is the easiest way to order boxplots in tidyverse and ggplot in this example?

考虑以下数据,使用 ggplot 在箱线图中显示它们。实际上需要的是订购 home 的食品。

MWE

structure(list(Site = structure(c(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, 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, 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, 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, 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, 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, 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, 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1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, 2L, 2L, 
2L, 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, 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, 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, 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, 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, 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), .Label = c("home", 
"office"), class = "factor"), Food = c("Banana", "Apple", "Egg", 
"Berry", "Tomato", "Potato", "Bean", "Pea", "Nuts", "Onion", 
"Carrot", "Cabbage", "Eggplant", "Banana", "Apple", "Egg", "Berry", 
"Tomato", "Potato", "Bean", "Pea", "Nuts", "Onion", "Carrot", 
"Cabbage", "Eggplant", "Banana", "Apple", "Egg", "Berry", "Tomato", 
"Potato", "Bean", "Pea", "Nuts", "Onion", "Carrot", "Cabbage", 
"Eggplant", "Banana", "Apple", "Egg", "Berry", "Tomato", "Potato", 
"Bean", "Pea", "Nuts", "Onion", "Carrot", "Cabbage", "Eggplant", 
"Banana", "Apple", "Egg", "Berry", "Tomato", "Potato", "Bean", 
"Pea", "Nuts", "Onion", "Carrot", "Cabbage", "Eggplant", "Banana", 
"Apple", "Egg", "Berry", "Tomato", "Potato", "Bean", "Pea", "Nuts", 
"Onion", "Carrot", "Cabbage", "Eggplant", "Banana", "Apple", 
"Egg", "Berry", "Tomato", "Potato", "Bean", "Pea", "Nuts", "Onion", 
"Carrot", "Cabbage", "Eggplant", "Banana", "Apple", "Egg", "Berry", 
"Tomato", "Potato", "Bean", "Pea", "Nuts", "Onion", "Carrot", 
"Cabbage", "Eggplant", "Banana", "Apple", "Egg", "Berry", "Tomato", 
"Potato", "Bean", "Pea", "Nuts", "Onion", "Carrot", "Cabbage", 
"Eggplant", "Banana", "Apple", "Egg", "Berry", "Tomato", "Potato", 
"Bean", "Pea", "Nuts", "Onion", "Carrot", "Cabbage", "Eggplant", 
"Banana", "Apple", "Egg", "Berry", "Tomato", "Potato", "Bean", 
"Pea", "Nuts", "Onion", "Carrot", "Cabbage", "Eggplant", "Banana", 
"Apple", "Egg", "Berry", "Tomato", "Potato", "Bean", "Pea", "Nuts", 
"Onion", "Carrot", "Cabbage", "Eggplant", "Banana", "Apple", 
"Egg", "Berry", "Tomato", "Potato", "Bean", "Pea", "Nuts", "Onion", 
"Carrot", "Cabbage", "Eggplant", "Banana", "Apple", "Egg", "Berry", 
"Tomato", "Potato", "Bean", "Pea", "Nuts", "Onion", "Carrot", 
"Cabbage", "Eggplant", "Banana", "Apple", "Egg", "Berry", "Tomato", 
"Potato", "Bean", "Pea", "Nuts", "Onion", "Carrot", "Cabbage", 
"Eggplant", "Banana", "Apple", "Egg", "Berry", "Tomato", "Potato", 
"Bean", "Pea", "Nuts", "Onion", "Carrot", "Cabbage", "Eggplant", 
"Banana", "Apple", "Egg", "Berry", "Tomato", "Potato", "Bean", 
"Pea", "Nuts", "Onion", "Carrot", "Cabbage", "Eggplant", "Banana", 
"Apple", "Egg", "Berry", "Tomato", "Potato", "Bean", "Pea", "Nuts", 
"Onion", "Carrot", "Cabbage", "Eggplant", "Banana", "Apple", 
"Egg", "Berry", "Tomato", "Potato", "Bean", "Pea", "Nuts", "Onion", 
"Carrot", "Cabbage", "Eggplant", "Banana", "Apple", "Egg", "Berry", 
"Tomato", "Potato", "Bean", "Pea", "Nuts", "Onion", "Carrot", 
"Cabbage", "Eggplant", "Banana", "Apple", "Egg", "Berry", "Tomato", 
"Potato", "Bean", "Pea", "Nuts", "Onion", "Carrot", "Cabbage", 
"Eggplant", "Banana", "Apple", "Egg", "Berry", "Tomato", "Potato", 
"Bean", "Pea", "Nuts", "Onion", "Carrot", "Cabbage", "Eggplant", 
"Banana", "Apple", "Egg", "Berry", "Tomato", "Potato", "Bean", 
"Pea", "Nuts", "Onion", "Carrot", "Cabbage", "Eggplant", "Banana", 
"Apple", "Egg", "Berry", "Tomato", "Potato", "Bean", "Pea", "Nuts", 
"Onion", "Carrot", "Cabbage", "Eggplant", "Banana", "Apple", 
"Egg", "Berry", "Tomato", "Potato", "Bean", "Pea", "Nuts", "Onion", 
"Carrot", "Cabbage", "Eggplant", "Banana", "Apple", "Egg", "Berry", 
"Tomato", "Potato", "Bean", "Pea", "Nuts", "Onion", "Carrot", 
"Cabbage", "Eggplant", "Banana", "Apple", "Egg", "Berry", "Tomato", 
"Potato", "Bean", "Pea", "Nuts", "Onion", "Carrot", "Cabbage", 
"Eggplant", "Banana", "Apple", "Egg", "Berry", "Tomato", "Potato", 
"Bean", "Pea", "Nuts", "Onion", "Carrot", "Cabbage", "Eggplant", 
"Banana", "Apple", "Egg", "Berry", "Tomato", "Potato", "Bean", 
"Pea", "Nuts", "Onion", "Carrot", "Cabbage", "Eggplant", "Banana", 
"Apple", "Egg", "Berry", "Tomato", "Potato", "Bean", "Pea", "Nuts", 
"Onion", "Carrot", "Cabbage", "Eggplant", "Banana", "Apple", 
"Egg", "Berry", "Tomato", "Potato", "Bean", "Pea", "Nuts", "Onion", 
"Carrot", "Cabbage", "Eggplant", "Banana", "Apple", "Egg", "Berry", 
"Tomato", "Potato", "Bean", "Pea", "Nuts", "Onion", "Carrot", 
"Cabbage", "Eggplant", "Banana", "Apple", "Egg", "Berry", "Tomato", 
"Potato", "Bean", "Pea", "Nuts", "Onion", "Carrot", "Cabbage", 
"Eggplant", "Banana", "Apple", "Egg", "Berry", "Tomato", "Potato", 
"Bean", "Pea", "Nuts", "Onion", "Carrot", "Cabbage", "Eggplant", 
"Banana", "Apple", "Egg", "Berry", "Tomato", "Potato", "Bean", 
"Pea", "Nuts", "Onion", "Carrot", "Cabbage", "Eggplant", "Banana", 
"Apple", "Egg", "Berry", "Tomato", "Potato", "Bean", "Pea", "Nuts", 
"Onion", "Carrot", "Cabbage", "Eggplant", "Banana", "Apple", 
"Egg", "Berry", "Tomato", "Potato", "Bean", "Pea", "Nuts", "Onion", 
"Carrot", "Cabbage", "Eggplant", "Banana", "Apple", "Egg", "Berry", 
"Tomato", "Potato", "Bean", "Pea", "Nuts", "Onion", "Carrot", 
"Cabbage", "Eggplant", "Banana", "Apple", "Egg", "Berry", "Tomato", 
"Potato", "Bean", "Pea", "Nuts", "Onion", "Carrot", "Cabbage", 
"Eggplant", "Banana", "Apple", "Egg", "Berry", "Tomato", "Potato", 
"Bean", "Pea", "Nuts", "Onion", "Carrot", "Cabbage", "Eggplant", 
"Banana", "Apple", "Egg", "Berry", "Tomato", "Potato", "Bean", 
"Pea", "Nuts", "Onion", "Carrot", "Cabbage", "Eggplant", "Banana", 
"Apple", "Egg", "Berry", "Tomato", "Potato", "Bean", "Pea", "Nuts", 
"Onion", "Carrot", "Cabbage", "Eggplant", "Banana", "Apple", 
"Egg", "Berry", "Tomato", "Potato", "Bean", "Pea", "Nuts", "Onion", 
"Carrot", "Cabbage", "Eggplant", "Banana", "Apple", "Egg", "Berry", 
"Tomato", "Potato", "Bean", "Pea", "Nuts", "Onion", "Carrot", 
"Cabbage", "Eggplant", "Banana", "Apple", "Egg", "Berry", "Tomato", 
"Potato", "Bean", "Pea", "Nuts", "Onion", "Carrot", "Cabbage", 
"Eggplant", "Banana", "Apple", "Egg", "Berry", "Tomato", "Potato", 
"Bean", "Pea", "Nuts", "Onion", "Carrot", "Cabbage", "Eggplant", 
"Banana", "Apple", "Egg", "Berry", "Tomato", "Potato", "Bean", 
"Pea", "Nuts", "Onion", "Carrot", "Cabbage", "Eggplant", "Banana", 
"Apple", "Egg", "Berry", "Tomato", "Potato", "Bean", "Pea", "Nuts", 
"Onion", "Carrot", "Cabbage", "Eggplant", "Banana", "Apple", 
"Egg", "Berry", "Tomato", "Potato", "Bean", "Pea", "Nuts", "Onion", 
"Carrot", "Cabbage", "Eggplant", "Banana", "Apple", "Egg", "Berry", 
"Tomato", "Potato", "Bean", "Pea", "Nuts", "Onion", "Carrot", 
"Cabbage", "Eggplant", "Banana", "Apple", "Egg", "Berry", "Tomato", 
"Potato", "Bean", "Pea", "Nuts", "Onion", "Carrot", "Cabbage", 
"Eggplant", "Banana", "Apple", "Egg", "Berry", "Tomato", "Potato", 
"Bean", "Pea", "Nuts", "Onion", "Carrot", "Cabbage", "Eggplant", 
"Banana", "Apple", "Egg", "Berry", "Tomato", "Potato", "Bean", 
"Pea", "Nuts", "Onion", "Carrot", "Cabbage", "Eggplant", "Banana", 
"Apple", "Egg", "Berry", "Tomato", "Potato", "Bean", "Pea", "Nuts", 
"Onion", "Carrot", "Cabbage", "Eggplant", "Banana", "Apple", 
"Egg", "Berry", "Tomato", "Potato", "Bean", "Pea", "Nuts", "Onion", 
"Carrot", "Cabbage", "Eggplant", "Banana", "Apple", "Egg", "Berry", 
"Tomato", "Potato", "Bean", "Pea", "Nuts", "Onion", "Carrot", 
"Cabbage", "Eggplant", "Banana", "Apple", "Egg", "Berry", "Tomato", 
"Potato", "Bean", "Pea", "Nuts", "Onion", "Carrot", "Cabbage", 
"Eggplant", "Banana", "Apple", "Egg", "Berry", "Tomato", "Potato", 
"Bean", "Pea", "Nuts", "Onion", "Carrot", "Cabbage", "Eggplant", 
"Banana", "Apple", "Egg", "Berry", "Tomato", "Potato", "Bean", 
"Pea", "Nuts", "Onion", "Carrot", "Cabbage", "Eggplant", "Banana", 
"Apple", "Egg", "Berry", "Tomato", "Potato", "Bean", "Pea", "Nuts", 
"Onion", "Carrot", "Cabbage", "Eggplant", "Banana", "Apple", 
"Egg", "Berry", "Tomato", "Potato", "Bean", "Pea", "Nuts", "Onion", 
"Carrot", "Cabbage", "Eggplant", "Banana", "Apple", "Egg", "Berry", 
"Tomato", "Potato", "Bean", "Pea", "Nuts", "Onion", "Carrot", 
"Cabbage", "Eggplant", "Banana", "Apple", "Egg", "Berry", "Tomato", 
"Potato", "Bean", "Pea", "Nuts", "Onion", "Carrot", "Cabbage", 
"Eggplant", "Banana", "Apple", "Egg", "Berry", "Tomato", "Potato", 
"Bean", "Pea", "Nuts", "Onion", "Carrot", "Cabbage", "Eggplant", 
"Banana", "Apple", "Egg", "Berry", "Tomato", "Potato", "Bean", 
"Pea", "Nuts", "Onion", "Carrot", "Cabbage", "Eggplant", "Banana", 
"Apple", "Egg", "Berry", "Tomato", "Potato", "Bean", "Pea", "Nuts", 
"Onion", "Carrot", "Cabbage", "Eggplant", "Banana", "Apple", 
"Egg", "Berry", "Tomato", "Potato", "Bean", "Pea", "Nuts", "Onion", 
"Carrot", "Cabbage", "Eggplant", "Banana", "Apple", "Egg", "Berry", 
"Tomato", "Potato", "Bean", "Pea", "Nuts", "Onion", "Carrot", 
"Cabbage", "Eggplant"), Amt = c(33.7320747087792, 36.0342981304717, 
NA, 35.2586085913145, NA, NA, NA, NA, 38.3145037556614, NA, NA, 
NA, NA, 34.5708791842277, 36.8083954420236, NA, 34.1103048796563, 
34.7916383519758, NA, NA, NA, 36.6601370680446, NA, 36.5265182398163, 
NA, NA, 25.005188762133, 22.6579865605078, 33.5223954506919, 
33.0003190550185, 33.7416090994921, 34.2899000244291, 36.330594872916, 
28.5307647511704, 34.8189822783005, NA, NA, 37.1003277282357, 
33.0113330939602, 26.6411931455001, 24.400305478985, NA, 33.1431675715187, 
NA, 35.7113224020986, NA, 29.287371719326, NA, 36.1534235779212, 
NA, NA, 35.0993333300875, 25.6350068029415, 22.8315582582792, 
35.6798417040879, 33.6551798874077, NA, 34.5149215921523, 35.2195542738363, 
27.454718149262, NA, 35.4491177241447, NA, 37.2340571766981, 
33.1136063404172, 26.2903409885922, 23.6416652669618, NA, 33.1263041491744, 
33.2114453080786, 34.1787283290142, 35.6627720040947, 28.2557166118573, 
36.4488643461852, 36.9465204323288, NA, 37.9450756808773, NA, 
28.24689336153, 25.2506989033878, 36.0273756925343, 35.0448688504907, 
39.6284568507634, 35.7268482344757, NA, 31.1914481880882, 34.2155313735937, 
37.308640751433, 37.3924774753636, NA, 37.4567306540345, 25.4730770175317, 
23.7860930258368, 35.4083653476054, 33.0730363731854, 37.1962667442638, 
34.2517588760593, NA, 27.1855960301278, 33.6324120786977, 34.6985008701019, 
NA, 36.5179668135246, 32.8981238924209, 27.2003716723138, 25.5899171812445, 
35.8637221808481, 35.3399576307628, NA, 32.4385415789459, 34.4323681936618, 
30.1565413047041, 35.947062876565, 35.6257086509707, NA, 36.7181363098719, 
31.8666781697971, 29.9869956793582, 28.5740680124215, 33.9812782521306, 
35.4760690593192, 34.9694127863526, 34.6380779165386, NA, 33.1278069397518, 
NA, 36.6162343127439, 39.1099774524497, NA, 34.2065721441469, 
25.7934532916232, 22.1527190880824, 33.5141366529546, 33.1848137577955, 
33.3056976665704, 33.0629384529152, NA, 28.6057843382806, 35.4855776329702, 
37.746145254053, NA, 37.8478246638457, 35.3145994570635, 26.816339202557, 
23.6427572112323, NA, 33.2130192061734, 34.1230406402024, 34.4137433944344, 
NA, 28.0027819936089, 33.1722169116251, 34.1502254502088, NA, 
NA, 35.8263035631523, 33.5689698353873, 34.3286397428481, NA, 
38.4971513007561, 35.0308129865917, NA, NA, 36.2380673398583, 
36.5585490125424, NA, NA, NA, 35.4199137511219, 33.8397511995298, 
34.9018963955308, NA, 35.4085223224039, 34.3428339260254, NA, 
NA, NA, 37.2751668736045, NA, NA, NA, NA, 27.1048369269962, 24.1622687506616, 
NA, 34.5046875912367, 36.9573864641579, 33.0500418532525, 37.7897852066917, 
29.3893064023818, 35.2787976820883, 35.8048016227347, NA, 36.570495487479, 
NA, 28.0373731157367, 24.1038125762098, 37.9928283932686, 34.0873109426604, 
34.3824151523536, 34.6853709858578, NA, 28.665403831058, 35.030033754826, 
36.8479703059791, NA, 37.2276618565035, 35.0321935817882, 24.9639725563492, 
21.5413405492274, 33.2776938744279, 34.1395813009498, 34.0980145374272, 
31.5679398989844, 36.3606852742019, 26.9810978199802, 33.2528878537102, 
35.0622928535478, 39.2695325520249, 35.29864322851, 33.2383716863936, 
24.7627143595055, 21.48218100484, NA, 33.0044643997302, 35.6077439907745, 
33.3509487946103, NA, 26.58261437784, 37.130013379686, 34.6168460164319, 
NA, NA, 32.8298245553818, 22.5978219563779, 21.1007109350344, 
33.1919730052388, 34.5159425302493, 35.0700162593142, 31.0966511502824, 
32.696949391938, 26.6369867985203, 35.6124436207985, 34.8931879812995, 
NA, 34.5123481827091, 28.1905255739212, 24.9024651175395, 22.7963723337745, 
33.1948681877228, 35.5691645278559, 36.6276774872535, 34.1093436259715, 
NA, 27.093792426083, 34.5966600075617, 35.9303570818422, NA, 
36.5053800030392, 29.0056467226404, 23.3291827726981, 21.8584094655647, 
34.3508987546419, 32.4744790865912, 34.9810202276665, 32.8483880508106, 
35.3162001787704, 26.6463564031159, 33.1478307722562, 35.4194209871219, 
NA, 35.6853674213489, 32.3403650828424, 25.9640446191113, 23.5306151690382, 
NA, 32.9002685637808, 38.7894756417518, 36.2193406969451, NA, 
27.8558991273316, 34.3818763306639, 35.6251634173161, NA, 36.5138719186128, 
34.3235962994842, 23.5617788238768, 20.325266118906, NA, 35.0366336389832, 
33.0410855077816, 32.8954165015973, 33.5658479292662, 26.1779387462112, 
32.1798160491199, 34.0059345518959, 33.9134446104187, 34.239321957294, 
32.0944601081991, 26.7391494600618, 22.390429731544, 37.9423749063706, 
34.9219499335594, 35.6643664156284, NA, 37.0881994412012, 27.5252113063503, 
36.6294169448529, 35.1032960995104, 36.4596185748331, 36.3983887802026, 
33.290180185996, 23.100380918891, 19.4810440361006, 35.0559092879642, 
34.7662464716677, 34.8023580337545, 31.4413392020832, 33.9906667882109, 
25.6003456896802, 34.0870501294717, 34.1138266762934, NA, 32.8303943905283, 
32.0100216764951, 25.1041654846232, 21.5840396744735, 32.2775128091517, 
34.9974580262585, 34.4067666482089, 33.9825315691151, NA, 26.086549732338, 
33.1826488852713, 36.135849808293, NA, 34.2390553893243, 34.7054997326759, 
22.2089037124919, 20.6971505439965, 31.365102857192, 34.5421971759165, 
36.1546494331904, 31.349261588011, 34.3554460170443, 25.682894575341, 
33.3924188518789, 35.8135620668499, 38.9866979128628, 37.5435086617283, 
30.2657971121487, 29.4754537560905, 29.2479046039039, NA, 35.1198780685103, 
33.0168055948649, NA, NA, 29.7760499166726, NA, NA, NA, NA, 29.5705573452566, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 34.422002798602, NA, NA, 34.15325816954, 34.6326302242432, 
NA, NA, NA, NA, NA, NA, NA, NA, 36.4880685822164, 37.3992276330308, 
NA, 36.0076786929174, 35.0347231820833, NA, NA, NA, NA, NA, NA, 
NA, NA, 26.9530323495884, 22.8026614891699, 34.3626646156445, 
34.631121647439, 34.1422563517602, 34.4732260808729, 35.671716254303, 
28.1554630614674, 36.8297132858095, 37.1140951794741, NA, 36.2925343926032, 
33.1450803721419, 27.0331011305547, 22.1211402922938, 36.5666666231928, 
34.6398991062198, 35.5052950094564, 33.9795510325964, NA, 28.0871501160913, 
35.4025652272473, 36.1664228373045, 35.3633238637043, 34.735041496722, 
NA, 27.7665834599958, 22.547960294331, NA, 34.4421693390938, 
NA, 35.0987504651101, NA, 28.4555427905722, 36.4268801644134, 
35.0417726444557, NA, 36.7458597500821, 36.1567561747919, 26.1645926461683, 
21.7382157021495, 35.6970935125265, 34.0199637628979, 34.2861024247436, 
33.2286710664692, NA, 26.638687857229, 35.0126722226325, 34.5174764798047, 
39.2677527690891, 33.8415805304284, 34.3341666333663, 21.9482845537368, 
20.4282711509926, 32.0572286175677, 32.8346421763693, 33.4434502787096, 
31.2963788447854, 34.0119613933917, 25.378811825975, 34.0767379367954, 
33.2988925958786, 37.6563721889651, 34.6150316310407, 31.5248159340017, 
23.8782822905105, 21.8617644082455, 33.6511562980718, 33.345281734173, 
36.9263745732831, 33.4657199965789, NA, 25.8469466133249, 33.8897499087351, 
34.4506999118678, NA, 35.5656930586111, 34.2061133146853, 26.3821554274702, 
21.0451200936985, NA, 33.4382040066249, 35.5457254835173, NA, 
35.4373275284757, 27.0853897776116, 36.1352783370551, 33.3477505189482, 
NA, 34.7823798772881, 34.2670600876537, 24.9174252304485, 21.1949145050295, 
35.9055751433032, 35.935980325927, 34.9945785315253, 35.0829571309848, 
35.4381519141592, 26.2032119580977, 31.1038565988792, 30.2760501898583, 
30.9566263569906, 30.1630584797066, 33.4790379007452, 23.3073401712644, 
21.3068562846048, 36.097466961967, 34.8403663728004, 35.7200485242893, 
33.8321858338282, NA, 28.2287319246857, NA, 35.4831868112887, 
NA, NA, 35.6950088074274, 24.6348127614424, 23.0322283272576, 
NA, 34.0520306051562, 38.7621696867191, 35.1504378875245, 35.5614134985523, 
27.1353023379795, NA, 35.0920857166253, 36.869670133947, 35.0684202034418, 
33.7415291710005, 25.2813045125938, 22.5386290613238, 35.5243498041775, 
36.2804876484455, 34.9396933611706, 34.0821048601979, 33.1652220005186, 
26.9069658930641, 35.0540147698775, NA, NA, 36.5461103454881, 
32.9472901274291, 24.9717743645712, 23.5721878308272, NA, 35.8766176157609, 
36.1995589062251, 32.8756871776144, NA, 27.1415970561554, 34.4682337707681, 
34.6883646786339, NA, 36.3118606807474, 33.4195197594415, 24.8631162813699, 
21.8226574999764, 36.4552654814768, 35.5444821199677, 33.7803976388234, 
32.2011593929073, 33.0432634166696, 26.2379004303746, 33.6696035367146, 
37.3981274402701, 36.9663505298282, 36.5610451260351, 27.9719615772428, 
26.1071157525824, 23.0719976068586, 35.8624919654559, 35.4665099854785, 
36.3039268989853, 34.3726321365982, 34.5672178622488, 26.8654148063767, 
36.0546644166852, 35.0165759525494, NA, 38.0133908213762, 28.7625201475681, 
23.3349235680768, 20.2882206946192, 34.4990228310731, 33.9118308738382, 
34.9721376642123, 32.077109820847, 35.6691961165434, 25.9898546238917, 
34.4044249248007, 33.6278983627004, 37.9858712146857, 33.067148028882, 
31.8765615276688, 27.1495891638328, 21.0857459653893, 37.0089249381228, 
34.6873324497559, 35.2770957612893, 34.9141204642957, 37.4223475342166, 
27.3259294548943, NA, 34.1119321732019, NA, 38.0480178394386, 
35.5537145433695, 25.3923521439289, 23.0922294493919, NA, 33.538056237357, 
34.1759523535249, 32.5763136298554, NA, 27.5486957778583, 36.0963003661885, 
36.1543365502897, 36.6193066673856, 38.7588491672215, 33.7582406400096, 
27.6125447865956, 26.2439497521459, NA, 33.6597010256619, 32.5667963161781, 
34.6767208781435, NA, 30.0967431966753, 36.8355503878017, NA, 
NA, 37.6208100575912, 29.9203535267449, 23.0579663023686, 20.5160823532227, 
35.0265642403216, 34.5464330438418, 35.8592997980752, 30.5573454464747, 
33.3042665497066, 26.0850226476924, 35.0126165610184, 35.3955483463556, 
36.1269224098847, 34.8681459471581, 33.1241705576519, 25.026492914099, 
21.9935225100838, NA, 34.8051329153033, 33.1385915196435, 33.1207772548047, 
34.4520178856501, 26.9815064790579, 34.8439650964387, 34.0892591269461, 
39.4698504114642, 35.3901176173061, 33.5256760778475, 23.2332294216024, 
20.0471275231065, 33.5245878350963, 33.4312317357962, 37.857281801283, 
30.9866630575631, 33.9573759252853, 26.2111082863746, 34.5690660011084, 
34.3679303988311, NA, 34.5865677643884, 34.0575697046708, 26.1262658258816, 
23.7999687521526, NA, 32.5448121745439, 38.5461985187852, 35.4714901830973, 
NA, 28.0068678945461, 33.7886755623611, 35.1857514484385, 39.5403795607686, 
36.6649207897475, NA, 24.1039501555994, 18.588348431427, 35.6095256903849, 
34.7195778435221, 35.2173649383351, 31.9638520894498, 37.6594147808233, 
26.3034472379872, 33.4999242605808, 34.5662784291335, NA, 32.9703432776085, 
31.3528413126484, 24.7174758757831, 21.0544389940199, 35.7645538289509, 
34.3222923143281, 34.0430655456755, 34.5006780597754, 35.6099072532631, 
26.7906537930688, 34.7314605160379, 35.3636242367639, 39.5205719549468, 
34.2897302499308, 33.8646655176917, 24.4144218975257, 21.3698994557312, 
32.4708908794049, 36.2119944797095, 35.3588982106935, 32.9674414729295, 
33.9299420467793, 27.6700064895605, 34.8698385627463, 34.2696663596163, 
NA, 34.0162964121286, 32.4325272625065, 25.3290753373847, 22.3330239199226, 
34.4567062332069, 36.6339486725376, 35.3133887633062, NA, NA, 
27.1309748887202, 34.5752926972933, 38.710520173709, NA, 36.4300557322918, 
33.5285541282708, 24.9867248015247, 22.2525409948961, 36.5769963340753, 
37.6206264990933, 35.082988413419, 33.631556531742, 36.7332987836322, 
27.5689459366795, 33.7087380528888, 35.108004068514, NA, 35.2138549646156, 
34.0138678223746, 25.9573289143666, 22.4003346265267, NA, 35.1171828566527, 
35.0910886427985, 36.0210013500091, NA, 27.4833924287783, NA, 
35.1242296932743, 38.5193179840847, 36.6192291301525, 35.8982756276023, 
25.9751920946864, 22.9857098095578, NA, 37.4818573930852, 34.3296303964954, 
33.9568118126176, NA, 27.0280463517924, 36.3395946294491, 35.2554724334548, 
NA, 36.8427218072644, 32.5069167109462, 25.8655879241207, 23.4222476450893, 
39.5185665174564, 36.204131292074, 34.3366667158137, 32.6682422615747, 
34.7443780506353, 27.476822133078, 35.9485032237767, 34.8178873487401, 
36.9381193175262, 34.7724643032025, 31.1313583955301, 26.2069611211106, 
23.1415088547721, NA, 34.136240471114, 33.9982842418703, 34.5044437351778, 
NA, 27.4029851614578, 34.0483417669017, 36.1550696236958, NA, 
36.599035159723, 35.1387766342835, 26.0457951721524, 23.376054904234, 
37.3381268698525, 35.0646946113874, 35.6219919860307, 35.0033577028143, 
37.0909440879456, 27.4838311603095, 35.3746310368092, 35.8593468244749, 
37.443612980754, 36.4916030175629, 33.810158956159, 24.5049083977186, 
22.157942757431, NA, 33.9986740024123, 34.9495720173099, 32.6824517852446, 
NA, 26.8209813992691, 38.5286673384362, 35.5293133436335, 36.0015464751256, 
36.0745948110326, 33.2299189189788, 24.4906579809774, 22.5985422813658, 
36.0520583373645, 34.0163859086937, 32.9691195198199, 33.272278716963, 
34.509756172223, 26.5060338314882, 34.2653311340806, 37.4052782722685, 
NA, 36.0591988601763, 33.7111072467809)), row.names = c(NA, -884L
), class = c("tbl_df", "tbl", "data.frame"))

bopxlots代码

ggplot(df, aes(Food, Amt)) +
  geom_boxplot() +
  facet_grid(facets = . ~ Site) + 
  theme_bw(base_size = 16) + 
  coord_flip() + 
  theme(axis.title.y = element_blank(), axis.text.y = element_text(face = "italic"))

图表

问题

如何使用 forcats::fct_reeorder()home 订购基于 Amt 的食品?订购时,豌豆会放在香蕉旁边。

我们可以将 'Food' 转换为 factor class 和 levels arranged 以 'Age' [=24 的递增顺序=]

library(dplyr)
library(ggplot2)    
df %>%
      arrange(Site, Amt, Food) %>%
      mutate(Food = factor(Food, levels = unique(Food))) %>% 
      ggplot(aes(Food, Amt)) +
              geom_boxplot() +
              facet_grid(facets = . ~ Site) + 
              theme_bw(base_size = 16) + coord_flip() + 
              theme(axis.title.y = element_blank(),
                   axis.text.y = element_text(face = "italic"))


如果是基于median值'Amt'基于'Site''home',filter行'Site'是"home",按'Food'分组,得到'Amt'的medianarrange数据集的行,提取distinct的值36=],并将其用作 levels,同时将 'Food' 转换为 factor 并绘制

lvls <- df %>%
           filter(Site == 'home') %>% 
           group_by(Food) %>%
           summarise(Amt = median(Amt, na.rm = TRUE)) %>% 
           ungroup %>% 
           arrange(Amt) %>%
           pull(Food) %>%
           unique
df %>% 
    mutate(Food = factor(Food, levels = lvls))  %>%
    ggplot(aes(Food, Amt)) +
              geom_boxplot() +
              facet_grid(facets = . ~ Site) + 
              theme_bw(base_size = 16) + coord_flip() + 
              theme(axis.title.y = element_blank(),
                   axis.text.y = element_text(face = "italic"))

注意:顺序根据排序中使用的标准而变化

一般来说,最简单的重新排序解决方案是使用 forcats 包中的各种 fct_*() 函数,特别是这里的 fct_reorder() 函数。

天真的尝试会产生这样的结果:

library(forcats)

ggplot(df, aes(fct_reorder(Food, Amt, na.rm = TRUE), Amt)) +
  geom_boxplot() +
  facet_grid(facets = . ~ Site) + 
  theme_bw(base_size = 16) + 
  coord_flip() + 
  theme(axis.title.y = element_blank(), axis.text.y = element_text(face = "italic"))
#> Warning: Removed 214 rows containing non-finite values (stat_boxplot).

reprex package (v0.3.0)

于 2020-01-01 创建

这不是我们想要的,因为它会在两个方面重新排序。要在 home 内排序,我们可以简单地将所有其他 Amt 值设置为 NA,使用战略性放置的 ifelse().

ggplot(df, aes(
  fct_reorder(Food, ifelse(Site == "home", Amt, NA), na.rm = TRUE), Amt)) +
  geom_boxplot() +
  facet_grid(facets = . ~ Site) + 
  theme_bw(base_size = 16) + 
  coord_flip() + 
  theme(axis.title.y = element_blank(), axis.text.y = element_text(face = "italic"))
#> Warning: Removed 214 rows containing non-finite values (stat_boxplot).

reprex package (v0.3.0)

于 2020-01-01 创建