根据下面的 R 代码,数据框Results
按Industry
变量分组,对于其中的每一个,我计算案例/观察的数量。之后,我创建了一个条形图,其中 X 轴显示行业,而 Y 轴显示案例/观察的数量。最后,翻转图形(第一个图形)。
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()
然而,当我尝试从案例数量最多的条形到最低条形排序时,X 轴的值不会相应地调整/排序(第二张图)。它们的顺序与第一张图中的顺序完全相同。
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()
这个问题有解决方案吗?
这是一个示例:
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