根据下面的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
答案 0 :(得分:0)
我的错误是同时对x
和y
变量进行排序。订购y
(sort(Count)
)是多余的。这就像取消在x
变量上设置的顺序一样。因此,正确的代码如下:
Results %>%
group_by(Industry) %>%
summarise(Count = length(Buyer_ID)) %>%
ggplot() +
geom_col(aes(x = reorder(Industry, Count), y = Count),fill = "red") +
geom_text(aes(x = reorder(Industry, Count), y = Count, label = Count), size = 5, hjust = 0) +
labs(y = "Number of Buyers",x = "Industry") +
coord_flip()
提供以下图形结果: