[英]ggplot geom_bar not "stacked" or re-ordered
我正在尝试对我的情节做一些事情。 我的基本图的代码如下:
d %>%
ggplot(aes(x = Major.sectors, y = reorder(Obs, Obs, function(x)-length(x)), fill = Status)) +
geom_bar(stat = "identity") +
facet_wrap(~Model_Name, ncol = 1) +
coord_flip()
我正在尝试对条形进行排序,以使最大的条形位于一侧,而最低的条形位于另一侧。 我也想根据“状态”变量堆叠它们。
我认为我的问题出现在我尝试使用reorder(Obs, Obs, function(x)-length(x))
我试图reorder()
所有观察(包括 Status = 1 和 Status = 0)时我可能应该group_by
Model_Name
和Major.sectors
然后计算总观测值,然后reorder()
数据,这样数据就根据每个扇区和模型的总观测值重新排序。
其次,结果并非 100% 有意义。 看情节3 Year
, Publishing, printing
。
d %>%
filter(Model_Name == "3 Year" & Major.sectors == "Publishing, printing")
我得到:
# A tibble: 2 x 4
Status Model_Name Major.sectors Obs
<fct> <chr> <chr> <int>
1 0 3 Year Publishing, printing 422
2 1 3 Year Publishing, printing 74
查看当年的绘图时,“蓝色”或Status = 1
颜色占据条形图空间的大约 75%,但74/422 = 0.175
。
我认为问题又出在reorder()
函数上。
数据:
d <- structure(list(Status = 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, 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, 2L, 2L, 2L), .Label = c("0", "1"), class = "factor"),
Model_Name = c("1 Year", "1 Year", "1 Year", "1 Year", "1 Year",
"1 Year", "1 Year", "1 Year", "1 Year", "1 Year", "1 Year",
"1 Year", "1 Year", "1 Year", "1 Year", "1 Year", "2 Year",
"2 Year", "2 Year", "2 Year", "2 Year", "2 Year", "2 Year",
"2 Year", "2 Year", "2 Year", "2 Year", "2 Year", "2 Year",
"2 Year", "2 Year", "2 Year", "3 Year", "3 Year", "3 Year",
"3 Year", "3 Year", "3 Year", "3 Year", "3 Year", "3 Year",
"3 Year", "3 Year", "3 Year", "3 Year", "3 Year", "3 Year",
"3 Year", "4 Year", "4 Year", "4 Year", "4 Year", "4 Year",
"4 Year", "4 Year", "4 Year", "4 Year", "4 Year", "4 Year",
"4 Year", "4 Year", "4 Year", "4 Year", "4 Year", "1 Year",
"1 Year", "1 Year", "1 Year", "1 Year", "1 Year", "1 Year",
"1 Year", "1 Year", "1 Year", "1 Year", "1 Year", "1 Year",
"1 Year", "1 Year", "1 Year", "2 Year", "2 Year", "2 Year",
"2 Year", "2 Year", "2 Year", "2 Year", "2 Year", "2 Year",
"2 Year", "2 Year", "2 Year", "2 Year", "2 Year", "2 Year",
"2 Year", "3 Year", "3 Year", "3 Year", "3 Year", "3 Year",
"3 Year", "3 Year", "3 Year", "3 Year", "3 Year", "3 Year",
"3 Year", "3 Year", "3 Year", "3 Year", "3 Year", "4 Year",
"4 Year", "4 Year", "4 Year", "4 Year", "4 Year", "4 Year",
"4 Year", "4 Year", "4 Year", "4 Year", "4 Year", "4 Year",
"4 Year", "4 Year"), Major.sectors = c("Chemicals, rubber, plastics, non-metallic products",
"Construction", "Education, Health", "Food, beverages, tobacco",
"Gas, Water, Electricity", "Hotels & restaurants", "Machinery, equipment, furniture, recycling",
"Metals & metal products", "Other services", "Post & telecommunications",
"Primary sector", "Publishing, printing", "Textiles, wearing apparel, leather",
"Transport", "Wholesale & retail trade", "Wood, cork, paper",
"Chemicals, rubber, plastics, non-metallic products", "Construction",
"Education, Health", "Food, beverages, tobacco", "Gas, Water, Electricity",
"Hotels & restaurants", "Machinery, equipment, furniture, recycling",
"Metals & metal products", "Other services", "Post & telecommunications",
"Primary sector", "Publishing, printing", "Textiles, wearing apparel, leather",
"Transport", "Wholesale & retail trade", "Wood, cork, paper",
"Chemicals, rubber, plastics, non-metallic products", "Construction",
"Education, Health", "Food, beverages, tobacco", "Gas, Water, Electricity",
"Hotels & restaurants", "Machinery, equipment, furniture, recycling",
"Metals & metal products", "Other services", "Post & telecommunications",
"Primary sector", "Publishing, printing", "Textiles, wearing apparel, leather",
"Transport", "Wholesale & retail trade", "Wood, cork, paper",
"Chemicals, rubber, plastics, non-metallic products", "Construction",
"Education, Health", "Food, beverages, tobacco", "Gas, Water, Electricity",
"Hotels & restaurants", "Machinery, equipment, furniture, recycling",
"Metals & metal products", "Other services", "Post & telecommunications",
"Primary sector", "Publishing, printing", "Textiles, wearing apparel, leather",
"Transport", "Wholesale & retail trade", "Wood, cork, paper",
"Chemicals, rubber, plastics, non-metallic products", "Construction",
"Education, Health", "Food, beverages, tobacco", "Gas, Water, Electricity",
"Hotels & restaurants", "Machinery, equipment, furniture, recycling",
"Metals & metal products", "Other services", "Post & telecommunications",
"Primary sector", "Publishing, printing", "Textiles, wearing apparel, leather",
"Transport", "Wholesale & retail trade", "Wood, cork, paper",
"Chemicals, rubber, plastics, non-metallic products", "Construction",
"Education, Health", "Food, beverages, tobacco", "Gas, Water, Electricity",
"Hotels & restaurants", "Machinery, equipment, furniture, recycling",
"Metals & metal products", "Other services", "Post & telecommunications",
"Primary sector", "Publishing, printing", "Textiles, wearing apparel, leather",
"Transport", "Wholesale & retail trade", "Wood, cork, paper",
"Chemicals, rubber, plastics, non-metallic products", "Construction",
"Education, Health", "Food, beverages, tobacco", "Gas, Water, Electricity",
"Hotels & restaurants", "Machinery, equipment, furniture, recycling",
"Metals & metal products", "Other services", "Post & telecommunications",
"Primary sector", "Publishing, printing", "Textiles, wearing apparel, leather",
"Transport", "Wholesale & retail trade", "Wood, cork, paper",
"Chemicals, rubber, plastics, non-metallic products", "Construction",
"Education, Health", "Food, beverages, tobacco", "Hotels & restaurants",
"Machinery, equipment, furniture, recycling", "Metals & metal products",
"Other services", "Post & telecommunications", "Primary sector",
"Publishing, printing", "Textiles, wearing apparel, leather",
"Transport", "Wholesale & retail trade", "Wood, cork, paper"
), Obs = c(471L, 3594L, 1029L, 657L, 620L, 1648L, 981L, 718L,
5270L, 124L, 959L, 422L, 288L, 1375L, 6221L, 217L, 449L,
3492L, 988L, 628L, 586L, 1554L, 961L, 706L, 5127L, 105L,
918L, 425L, 269L, 1262L, 5966L, 218L, 443L, 3336L, 922L,
599L, 592L, 1454L, 913L, 666L, 4830L, 89L, 870L, 422L, 260L,
1182L, 5673L, 213L, 438L, 3371L, 886L, 584L, 527L, 1377L,
887L, 684L, 4609L, 91L, 844L, 415L, 250L, 1114L, 5463L, 207L,
118L, 1080L, 49L, 98L, 16L, 139L, 210L, 161L, 521L, 19L,
68L, 83L, 69L, 145L, 762L, 44L, 119L, 1109L, 48L, 98L, 14L,
134L, 220L, 166L, 508L, 14L, 65L, 79L, 68L, 144L, 771L, 42L,
113L, 1094L, 41L, 91L, 12L, 127L, 201L, 147L, 476L, 12L,
66L, 74L, 71L, 131L, 733L, 43L, 111L, 1027L, 39L, 87L, 117L,
191L, 147L, 434L, 13L, 66L, 70L, 67L, 128L, 681L, 41L)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -127L))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.