如何用填充重新排列ggplot geom_col中的cols? [英] How to reorder cols in ggplot geom_col with fill?

查看:28
本文介绍了如何用填充重新排列ggplot geom_col中的cols?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试以最大比例的"muni_d2 = 2"排在后面的方式排列此ggplot,然后是"muni_d2 = 1".

I am trying to order this ggplot in a way that the highest proportion of "muni_d2 = 2" comes first, followed by "muni_d2 = 1".

我尝试了一些代码,例如ranging()和reorder(),但是它们没有用.

I tried some codes like arrange() and reorder(), but they didn'd work.

这是数据:

structure(list(muni_o = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 
34L, 35L, 36L, 37L, 38L, 39L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 
22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 
35L, 36L, 37L, 38L, 39L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 
36L, 37L, 38L, 39L), .Label = c("1", "2", "3", "4", "5", "6", 
"7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", 
"18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", 
"29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39"
), class = "factor"), muni_d2 = 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, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 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
), .Label = c("0", "1", "2"), class = "factor"), Freq = c(6122.14220014045, 
14014.8937908602, 3212.98371738453, 3762.86973933326, 2036.58101043117, 
49935.7095757787, 5660.24459378729, 27133.6814011703, 8436.05010435109, 
1244.59144510279, 8192.3151623091, 5125.775303556, 5520.96031514796, 
971.04877757262, 13042.4320509421, 11677.9344908548, 23179.1486978947, 
19342.6692995674, 11480.8298502747, 773.75470216998, 658.84762129725, 
47303.8942738045, 12189.6600233976, 34138.2766548561, 2280.85569280618, 
6648.00527768305, 13731.4673121252, 6822.03535863008, 1654.5719919344, 
3408.63547537664, 8289.59920490436, 68250.529943451, 42919.3498777027, 
11304.5876393861, 1371.59278771158, 281250.973114875, 11758.2956764985, 
12160.5828034274, 4762.03601855022, 3774.00754913303, 17629.1897496886, 
450.28749520739, 11504.661338085, 2732.14923854684, 40180.8293143395, 
16562.6710623701, 43865.6430358497, 36647.9484766594, 7035.57607097289, 
25651.323071728, 26432.811604918, 23743.6986932767, 597.5643668538, 
124644.985934809, 29773.2098362482, 13788.9040158001, 24718.4440592384, 
6526.85217011751, 474.70640264902, 5440.01365034166, 29930.306857298, 
11072.6889810259, 101099.685991706, 254.95577591974, 11637.2716353329, 
5367.72912672273, 2504.81681919538, 97.98256049428, 2258.20425774592, 
8475.80585487552, 40564.9077830157, 59907.8345066714, 12858.502494956, 
850.24914346085, 0, 14152.3410516667, 41777.9839790016, 2730.8225408641, 
13662.2793985781, 46579.248451629, 5035.33799662119, 9167.68431269098, 
15713.4248568278, 36391.6173102699, 36523.5128982161, 56158.4213063058, 
17666.1932118543, 10319.0225229469, 14744.0900996851, 14519.6971048675, 
14341.9635886829, 5193.52143006156, 267285.439969485, 10049.4551896091, 
17861.0606322283, 56589.3203766755, 14836.888817694, 6069.4455916734, 
18992.3441918275, 52074.0110108799, 88973.5164027747, 82777.2109430964, 
2270.54970959312, 12030.813277725, 15414.1038338142, 3284.84133984456, 
3101.73291583232, 10020.5318813645, 25286.7675500444, 114919.563601638, 
185758.597625183, 28154.2996127091, 2873.4152126078, 3503521.52693064, 
49555.0928217366, 35402.2559957372, 7917.49624385274)), class = "data.frame", row.names = c(NA, 
-117L))

这是当前情节:

ggplot(pop, aes(muni_o, Freq, fill = muni_d2)) +
  geom_col(position = "fill") +
  coord_flip()

推荐答案

图中的顺序由因子 muni_o 中的级别定义,因为您希望将其按两列排列认为重新排序不会奏效.您可以做的是按两列排列数据框,然后相应地重新排列因子水平,这是可能的:

The order in the plot is defined by the levels in the factor muni_o, since you want to arrange it by two columns I don't think reorder would work. What you can do is arrange your dataframe by the two columns and then rearrange the factor levels accordingly, here's a possibility:

pop %>% 
     group_by(muni_o) %>% 
     mutate(prop = Freq / sum(Freq)) %>% 
     ungroup() %>% 
     arrange(desc(muni_d2), prop) %>% 
     mutate(muni_o = factor(muni_o, levels = unique(muni_o))) %>% # here you rearrange the levels
     ggplot(aes(muni_o, prop, fill = muni_d2)) +
     geom_col() +
     coord_flip()

希望这会有所帮助.

这篇关于如何用填充重新排列ggplot geom_col中的cols?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆