According to the following R code, the data frame Results
is grouped by the Industry
variable and for each one of them I calculate the number of cases/observations. After that I create a bar plot where the X axis displays the industries while the Y the number of cases/observations. Finally, the graph is flipped (first graph).
Results %>%
group_by(Industry) %>%
summarise(Count = length(Buyer.ID)) %>%
ggplot() +
geom_col(aes(x = Industry, y = Count),fill = "red") +
geom_text(aes(x = Industry, y = Count, label = Count), size = 5, hjust = 0) +
labs(y = "Number of Buyers",x = "Industry") +
coord_flip()
Nevertheless, when I try to order my bars from the one with the largest count of cases, to the lowest, the values of the X axis are not adjusted/ordered accordingly (second graph). They have exactly the same order as in the first graph.
Results %>%
group_by(Industry) %>%
summarise(Count = length(Buyer.ID)) %>%
ggplot() +
geom_col(aes(x = reorder(Industry,sort(Count)), y = sort(Count)),fill = "red") +
geom_text(aes(x = reorder(Industry,sort(Count)), y = sort(Count), label = sort(Count)), size = 5, hjust = 0) +
labs(y = "Number of Buyers",x = "Industry") +
coord_flip()
Is there a solution for this issue?
Here is a sample:
Buyer ID Industry
103992 Services
372423 Chemicals
2769385 Agriculture
2818071 Construction
2822202 Construction
2980052 Services
3175852 Textiles
3320461 Services
3328727 Construction
3347810 Services
3362754 Electronics
3362872 Construction
3363103 Construction
3364583 Food
3364678 Consumer Durables
3365146 Electronics
3365326 Metals
3365327 Chemicals
3365497 Machines
3366894 Construction
3367204 Metals
3368157 Food
3368385 Food
3368919 Chemicals
3369333 Food
3370385 Textiles
3370467 Construction Materials
3370701 Chemicals
3371202 Consumer Durables
3371243 Machines
3371757 Textiles
3372520 Food
3374124 Chemicals
3374648 Construction
3374794 Construction
3377600 Services
3378984 Electronics
3379162 Construction Materials
3379612 Food
3380628 Machines
3380943 Machines
3381275 Paper
3381859 Metals
3382106 Construction Materials
3382478 Food
3385367 Services
3385639 Machines
3385840 Machines
3386488 Food
3387205 Transport