user113156
user113156

Reputation: 7147

ggplot geom_bar not "stacked" or re-ordered

I am trying to do a few things to my plot. The code for the basic plot I have is the following:

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()

I am trying to order the bars such that the largest bars will be on one side and the lowest bars will be on the other. I would like to stack them according to the "Status" variable also.

I think my problem comes about when I try to use reorder(Obs, Obs, function(x)-length(x)) I am trying to reorder() all of the observations (inclusive of Status = 1 and Status = 0) when I should maybe group_by the Model_Name and Major.sectors then compute the total observations and then reorder() the data, such that the data is reordered on the total observation for each sector and model.

Secondly, the results do not make 100% sense. Looking at the plot for 3 Year, Publishing, printing.

d %>% 
  filter(Model_Name == "3 Year" & Major.sectors == "Publishing, printing")

I get:

# 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

When looking at the plot for that year, the "blue" or the Status = 1 color takes up approx 75% of the bar plot space but 74/422 = 0.175.

I think the problem is again in regards to the reorder() function.

Data:

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))

Upvotes: 0

Views: 45

Answers (1)

dc37
dc37

Reputation: 16178

You pass the reorder function on the wrong variable which convert your "Obs" values in a factor format.

Instead try this:

ggplot(d, aes(x = reorder(Major.sectors, Obs), y = Obs, fill = Status))+
  geom_col()+
  facet_wrap(~Model_Name, ncol = 1)+
  coord_flip()

enter image description here

Upvotes: 2

Related Questions