如何分别对每个facet_wrap进行排序

时间:2019-10-01 15:49:02

标签: r ggplot2 facet-wrap

我正在尝试绘制某些农作物的产量,并按每种农作物的前十大生产国排序。我使用facet_wrap,第一个面板排序,但其余面板不排序。

我尝试按照tidytuesday屏幕截图中的示例进行操作,但是由于某种原因,我的图形无法正确排序。

以下是截屏视频的链接和时间 https://youtu.be/rxJZT0duwfU?t=1841

以下是crop_dataset的数据:

structure(list(country = structure(c(1L, 1L, 2L, 2L, 3L, 4L, 
5L, 5L, 5L, 5L, 6L, 7L, 7L, 7L, 8L, 9L, 9L, 9L, 10L, 10L, 11L, 
11L, 12L, 12L, 13L, 13L, 13L, 14L, 14L, 15L, 16L, 17L, 17L, 17L, 
18L, 18L, 18L, 18L, 19L, 19L, 19L, 20L, 20L, 20L, 21L, 21L, 22L, 
23L, 23L, 23L, 23L, 24L, 24L, 25L, 25L, 25L, 26L, 26L, 26L, 26L, 
27L, 27L, 27L, 27L, 28L, 28L, 29L, 29L, 29L, 29L, 30L, 30L, 30L, 
30L, 31L, 31L, 32L, 32L, 32L, 33L, 33L, 34L, 35L, 35L, 35L, 35L, 
36L, 36L, 36L, 37L, 37L, 37L, 37L, 38L, 39L, 39L, 39L, 40L, 40L, 
40L, 41L, 41L, 42L, 42L, 43L, 43L, 44L, 44L, 45L, 45L, 45L, 45L, 
46L, 47L, 47L, 48L, 48L, 49L, 49L, 49L, 50L, 50L, 50L, 50L, 51L, 
51L, 51L, 52L, 52L, 52L, 53L, 54L, 55L, 55L, 56L, 56L, 56L, 57L, 
57L, 58L, 58L, 59L, 59L, 60L, 61L, 61L, 61L, 61L, 62L, 62L, 63L, 
63L, 64L, 64L, 65L, 65L, 65L, 66L, 66L, 67L, 67L, 68L, 69L, 70L, 
70L, 70L, 70L, 71L, 71L, 71L, 72L, 72L, 72L, 73L, 73L, 74L, 74L, 
75L, 75L, 75L, 75L, 76L, 76L, 77L, 77L, 77L, 78L, 78L, 78L, 78L, 
79L, 79L, 79L, 80L, 80L, 80L, 81L, 82L, 82L, 83L, 83L, 84L, 84L, 
84L, 85L, 85L, 86L, 86L, 87L, 87L, 87L, 88L, 89L, 89L, 90L, 90L, 
90L, 91L, 91L, 92L, 93L, 93L, 93L, 94L, 95L, 96L, 97L, 97L, 97L, 
97L, 98L, 98L, 98L, 99L, 99L, 99L, 100L, 101L, 101L, 101L, 102L, 
103L, 104L, 104L, 105L, 105L, 105L, 105L, 106L, 107L, 108L, 109L, 
109L, 109L, 110L, 110L, 111L, 111L, 111L, 112L, 112L, 112L, 113L, 
114L, 115L, 116L, 116L, 116L, 116L, 117L, 117L, 118L, 118L, 118L, 
118L, 119L, 119L, 120L, 120L, 120L, 121L, 121L, 121L, 121L, 122L, 
122L, 122L, 123L, 123L, 123L, 123L, 124L, 124L, 124L, 124L, 125L, 
125L, 125L, 125L, 126L, 126L, 127L, 127L, 128L, 129L, 130L, 130L, 
131L, 131L, 132L, 132L, 133L, 133L, 134L, 134L, 135L, 135L, 135L, 
136L, 136L, 137L, 138L, 138L, 139L, 140L, 140L, 140L, 141L, 141L, 
142L, 142L, 142L, 143L, 143L, 144L, 144L, 145L, 146L, 146L, 147L, 
147L, 147L, 148L, 149L, 149L, 149L, 150L, 150L, 150L, 151L, 151L, 
152L, 152L, 152L, 152L, 153L, 153L, 154L, 154L, 154L, 155L, 155L, 
156L, 156L, 156L, 156L, 157L, 157L, 158L, 158L, 159L, 159L, 159L, 
160L, 161L, 161L, 162L, 163L, 163L, 163L, 164L, 164L, 165L, 166L, 
166L, 166L, 166L, 167L, 167L, 167L, 168L, 168L, 168L, 169L, 170L, 
171L, 171L, 171L, 171L, 172L, 172L, 172L, 173L, 174L, 174L, 175L, 
175L, 175L, 176L, 176L, 176L), .Label = c("Afghanistan", "Albania", 
"Algeria", "American Samoa", "Angola", "Antigua and Barbuda", 
"Argentina", "Armenia", "Australia", "Austria", "Azerbaijan", 
"Bahamas", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", 
"Benin", "Bhutan", "Bolivia (Plurinational State of)", "Bosnia and Herzegovina", 
"Botswana", "Brazil", "Bulgaria", "Burkina Faso", "Burundi", 
"C\xf4te d'Ivoire", "Cabo Verde", "Cambodia", "Cameroon", "Canada", 
"Central African Republic", "Chad", "Chile", "China, mainland", 
"China, Taiwan Province of", "Colombia", "Comoros", "Congo", 
"Costa Rica", "Croatia", "Cuba", "Czechia", "Democratic People's Republic of Korea", 
"Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", 
"Dominican Republic", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", 
"Eritrea", "Eswatini", "Ethiopia", "Fiji", "France", "French Guiana", 
"French Polynesia", "Gabon", "Gambia", "Georgia", "Germany", 
"Ghana", "Greece", "Grenada", "Guadeloupe", "Guam", "Guatemala", 
"Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Honduras", "Hungary", 
"India", "Indonesia", "Iran (Islamic Republic of)", "Iraq", "Israel", 
"Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", 
"Kuwait", "Kyrgyzstan", "Lao People's Democratic Republic", "Lebanon", 
"Lesotho", "Liberia", "Libya", "Lithuania", "Luxembourg", "Madagascar", 
"Malawi", "Malaysia", "Maldives", "Mali", "Martinique", "Mauritania", 
"Mauritius", "Mexico", "Micronesia (Federated States of)", "Montenegro", 
"Montserrat", "Morocco", "Mozambique", "Myanmar", "Nepal", "Netherlands", 
"New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", 
"Oman", "Pakistan", "Panama", "Papua New Guinea", "Paraguay", 
"Peru", "Philippines", "Poland", "Portugal", "Puerto Rico", "Qatar", 
"R\xe9union", "Republic of Korea", "Republic of Moldova", "Romania", 
"Russian Federation", "Rwanda", "Saint Vincent and the Grenadines", 
"Samoa", "Sao Tome and Principe", "Saudi Arabia", "Senegal", 
"Serbia", "Sierra Leone", "Slovakia", "Slovenia", "Solomon Islands", 
"Somalia", "South Africa", "South Sudan", "Spain", "Sri Lanka", 
"Sudan", "Suriname", "Switzerland", "Syrian Arab Republic", "Tajikistan", 
"Thailand", "The former Yugoslav Republic of Macedonia", "Timor-Leste", 
"Togo", "Trinidad and Tobago", "Turkey", "Turkmenistan", "Uganda", 
"Ukraine", "United Arab Emirates", "United Republic of Tanzania", 
"United States of America", "Uruguay", "Uzbekistan", "Vanuatu", 
"Venezuela (Bolivarian Republic of)", "Viet Nam", "Wallis and Futuna Islands", 
"Yemen", "Zambia", "Zimbabwe"), class = "factor"), item = structure(c(1L, 
4L, 1L, 3L, 1L, 4L, 1L, 2L, 3L, 4L, 1L, 1L, 3L, 4L, 1L, 1L, 3L, 
4L, 1L, 3L, 1L, 3L, 1L, 4L, 1L, 3L, 4L, 1L, 4L, 1L, 1L, 1L, 3L, 
4L, 1L, 2L, 3L, 4L, 1L, 3L, 4L, 1L, 3L, 4L, 1L, 3L, 1L, 1L, 2L, 
3L, 4L, 1L, 3L, 1L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 3L, 1L, 2L, 4L, 1L, 4L, 
1L, 1L, 2L, 3L, 4L, 1L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 1L, 2L, 4L, 
1L, 2L, 4L, 1L, 3L, 1L, 4L, 1L, 3L, 1L, 3L, 1L, 2L, 3L, 4L, 1L, 
1L, 4L, 1L, 4L, 1L, 2L, 4L, 1L, 2L, 3L, 4L, 1L, 3L, 4L, 1L, 3L, 
4L, 2L, 1L, 1L, 4L, 1L, 3L, 4L, 1L, 4L, 1L, 3L, 1L, 4L, 4L, 1L, 
2L, 3L, 4L, 1L, 2L, 1L, 3L, 1L, 3L, 1L, 2L, 4L, 1L, 3L, 1L, 4L, 
4L, 1L, 1L, 2L, 3L, 4L, 1L, 2L, 4L, 1L, 2L, 4L, 1L, 4L, 1L, 4L, 
1L, 2L, 3L, 4L, 1L, 3L, 1L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 3L, 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, 19.164718, 0.010413, 0.436194, 0.064546, 36.561497, 
    2.075983, 0.193416, 0.235669, 4.5e-05, 0.000798, 0.058342, 
    3.025392, 0.096921, 3.862775, 9.9e-05, 0.102181, 0.694238, 
    0.608671, 0.089536, 0.013037, 1.670432, 1.454, 0.690908, 
    0.16, 0.01253, 0.090263, 0.000204, 0.014856, 1.004181, 3.018872, 
    8.049439, 0.709584, 0.01174, 0.012265, 97.72186, 1.676421, 
    114.599168, 758.548292, 2.562569, 0.02, 1.533431, 0.0185, 
    0.487643, 0.24, 0.086511, 0.003185, 0.219435, 1.025, 2.227, 
    0.000503, 1.794697, 0.005314, 0.028375, 0.75, 0.16, 0.168, 
    0.686505, 2.246241, 3.110296, 0.027022, 1.288039, 14.0953, 
    7.7166, 0.09, 0.0058, 0.10334, 0.396506, 0.461004, 1.062589, 
    259.071, 0.671347, 13.149485, 104.404, 0.163478, 0.003203, 
    0.389033, 1.296199, 5.738006, 0.075685, 34.638019, 0.006231, 
    0.01173, 0.148057, 0.701698, 0.006865, 1.0958, 4.142143, 
    1.559638, 0.207765, 0.37387, 16.070729, 0.588105, 0.037012, 
    2.199755, 0.223325, 2.017574, 1.192601, 0.025, 2.225155, 
    0.0389, 1.3e-05, 5.3e-05, 0.000189, 0.004872, 0.044007, 0.429486, 
    5.460761, 1.436106, 3.275993, 0.035006, 9.030074, 7.1, 0.045, 
    15.260597, 0.904, 0.00571, 7.155699, 0.035936, 0.02, 0.090864, 
    5.737328, 8.116787, 0.084033, 1.384207, 0.000961, 1.458049, 
    14.12168, 0.412, 0.000658, 0.01119, 0.003489, 0.04522, 0.016752, 
    0.004037, 0.295172, 0.039051, 0.035063, 0.1425, 0.001812, 
    4.5476, 0.061, 1.965, 2.469763, 0.151762, 1.3624, 0.0033, 
    0.000349, 0.007286, 0.671953, 5.1e-05, 1.917395, 2.031743, 
    0.038, 33.758389, 0.768082, 0.842045, 0.30595, 0.00727, 0.081313, 
    0.006698, 0.004, 1.859037, 0.225, 1.501927, 0.675665, 2.4033, 
    0.00127, 5.379869, 6.811337, 0.162075, 28.72, 10.981, 306.069, 
    27.952, 158.343062, 0.542, 21.21295, 1.223086, 0.2, 7.561703, 
    0.185291, 2.6e-05, 0.007, 0.087583, 6.048499, 1.019781, 0.002539, 
    1.1312, 0.000167, 0.253, 1.497043, 0.022044, 0, 0.784695, 
    0.252319, 3.186, 0.002012, 4.751609, 0.003838, 0.653351, 
    0.003512, 1.192525, 0.00796, 1.76439, 0.003, 8.5e-05, 0.172666, 
    0.179364, 0.003356, 0.274072, 0.003418, 0.056974, 0.000695, 
    0.281, 0.021105, 4.3e-05, 3.041628, 3.464139, 0.208556, 2.964006, 
    0.072561, 101.7409, 0.02999, 0.000151, 2.811385, 0.007875, 
    0.368916, 0.197274, 0.014, 0.000442, 3.713331, 27.762481, 
    0.873518, 0.432927, 56.954993, 8.6e-05, 0.00274, 0.000126, 
    0.122399, 0.000679, 0.553115, 1.70392, 2.9, 1.909334, 0.20947, 
    10.370042, 2.300121, 0.029061, 3.234711, 0.116711, 0.009415, 
    0.175586, 0.408415, 0.0743, 0.005693, 7.096373, 0.038345, 
    0.252854, 10.42, 7.759426, 0.73, 1.497757, 0.007075, 0.001184, 
    5.7014, 0.000159, 73.401199, 0.13, 0.044486, 0.00011, 2.444456, 
    0.013237, 2.434698, 0.205183, 5.1559, 0.14043, 10.478, 6.608, 
    1.523204, 0.852022, 0.001581, 9.399617, 7.914909, 0.474792, 
    0.000666, 29.286893, 4.021592, 0.020297, 0.745123, 0.005479, 
    0.000164, 0.000693, 0.014219, 1.822545, 0.072587, 0.068992, 
    1.773, 0.047, 14.3261, 0.41637, 13.235748, 3.621344, 0.358417, 
    0.023934, 0.107877, 0.000633, 0.01778, 1.2e-05, 0.000964, 
    0.018412, 0.015079, 0.264342, 0.137775, 0.464979, 4.01837, 
    0.461272, 0.018, 0.211292, 0.077944, 1.066188, 0.102441, 
    0.272177, 0.007713, 0.285721, 0.065, 0.209189, 16.82, 1.316, 
    17.388177, 0.092, 3.775645, 0.004599, 0.000506, 0.195744, 
    0.014363, 0.654522, 0.048873, 5.828601, 8.4e-05, 0.001186, 
    6e-06, 0.125301, 0.162454, 0.005642, 0.18557, 3e-04, 1e-06, 
    0.218448, 2.3e-05, 4.96158, 14.591343, 0.054, 102.946001, 
    0.123014, 3.4e-05, 0.100224, 0.001033, 0.854689, 0.155058, 
    0.002154, 0.003221, 5.9, 0.14, 0.048072, 3.015316, 0.030689, 
    3.858456, 24.66875, 3.89937, 0.006432, 5.939737, 0.07548, 
    0.005741, 3.00108, 370.96039, 119.51849, 30.15301, 0.309, 
    1.316, 0.357097, 0.389427, 0.000904, 1.05, 0.452379, 0.008, 
    3.6, 5.109766, 0.101856, 18.356398, 2e-05, 0.042137, 0, 3.606549, 
    0.351416, 4.451253, 0.90099, 0.06, 3.583672)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -411L))

这是我的代码

crop_dataset %>%
  group_by(item, value) %>% 
  arrange(item, desc(value)) %>%
  group_by(item) %>% 
  top_n(10, value) %>%
  mutate(country = fct_reorder(country, value)) %>% 
  ggplot(aes(country, value)) +
  geom_point() +
  coord_flip() +
  facet_wrap(~item, scales = "free", ncol = 2) +
  labs(title = measure, y = "", x = measure)

这是输出: enter image description here

我希望所有面板都可以排序,但这只是第一个。

0 个答案:

没有答案