0

I am trying to change the order of each portion in the stack bar chart from the least to the most proportion in each y variable. This is my data

A tibble: 76 x 5
Groups:   Hours.Type [17]
   Hours.Type                           Type.of.Control total  mean  freq
   <chr>                                <chr>           <int> <dbl> <dbl>
 1 Administrative Services Cost Centers City/County        46   9.2  36.8
 2 Administrative Services Cost Centers Investor            9   1.8   7.2
 3 Administrative Services Cost Centers Non-Profit         23   4.6  18.4
 4 Administrative Services Cost Centers State              47   9.4  37.6
 5 Aides & Orderlies                    City/County        46   9.2  20.3
 6 Aides & Orderlies                    District            3   1     1.3
 7 Aides & Orderlies                    Investor           67  13.4  29.5
 8 Aides & Orderlies                    Non-Profit         64  12.8  28.2
 9 Aides & Orderlies                    State              47   9.4  20.7
10 Ambulatory Cost Centers              City/County        52  10.4  14.3
# ... with 66 more rows

And this my code to plot the graph.

ggplot(null_mean_Type.of.Control_Hours.Type, aes(x = Hours.Type, y = freq, fill = Type.of.Control)) +
  geom_bar(position= position_stack(), stat= "identity", width = 0.7) +
  theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank()) +
  geom_text(aes(x = Hours.Type ,y = freq, label=paste(freq, sep = " "), group = Type.of.Control), position = position_stack(), hjust = 1.5) +
  scale_fill_discrete(guide = guide_legend(reverse=TRUE)) +
  coord_flip() + #show the proportion of each TOC in every hours type 
  ggtitle("Proportion of T.O.C in every hours type (0 productivity)")

the output is here.

I have try to use reorder but it end up like this and it pops out the error of "There were 50 or more warnings (use warnings() to see the first 50)". The warning messages are all like this:

1: In mean.default(X[[i]], ...) :
  argument is not numeric or logical: returning NA

The code that I have try reorder is like this:

ggplot(null_mean_Type.of.Control_Hours.Type, aes(x = Hours.Type, y = reorder(freq, Type.of.Control), fill = Type.of.Control)) + ...
Denusklo
  • 35
  • 5

0 Answers0