ggplot geom_bar not";“堆叠”;或重新订购

ggplot geom_bar not";“堆叠”;或重新订购,r,ggplot2,R,Ggplot2,我正试图对我的情节做一些改变。我拥有的基本绘图代码如下: 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() 我试着订购的酒吧,最大的酒吧将在一边,最低的酒吧将在另一边

我正试图对我的情节做一些改变。我拥有的基本绘图代码如下:

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()
我试着订购的酒吧,最大的酒吧将在一边,最低的酒吧将在另一边。我还想根据“Status”变量来堆叠它们

我想当我尝试使用
reorder(Obs,Obs,function(x)-length(x))
我尝试
reorder()当我应该根据
模型名称和
主要扇区对数据进行
分组时
然后计算总观测值,然后对数据进行
重新排序()
这样就可以根据每个扇区和模型的总观测值对数据进行重新排序

其次,结果并非100%有意义。查看
3年的绘图
出版、印刷

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

您在错误的变量上传递了
reorder
函数,该变量将“Obs”值转换为因子格式

相反,请尝试以下方法:

ggplot(d,aes(x=重新排序(主扇区,Obs),y=Obs,填充=状态))+
geom_col()+
面包(~Model\u Name,ncol=1)+
coord_flip()

您在错误的变量上传递了
重新排序
函数,该变量将“Obs”值转换为因子格式

相反,请尝试以下方法:

ggplot(d,aes(x=重新排序(主扇区,Obs),y=Obs,填充=状态))+
geom_col()+
面包(~Model\u Name,ncol=1)+
coord_flip()

为什么要重新排列y轴?你能试试这样的吗<代码>ggplot(aes(x=重新排序(主扇区,Obs),y=Obs,填充=状态))
为什么要尝试重新排序y轴?你能试试这样的吗<代码>ggplot(aes(x=重新排序(主扇区,Obs),y=Obs,填充=状态))
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))