gpt4 book ai didi

r - 如何指定使用闪避位置绘制的条形组的顺序

转载 作者:行者123 更新时间:2023-12-02 02:39:20 24 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Order discrete x scale by frequency/value

(6 个回答)


去年关闭。




我有一个与问题相关的问题 Ordering position "dodge" in ggplot2 .而不是指定每个条形组中各个条形的顺序,例如将 Certified 在下面的图中(来自上面的链接)放在右侧,我喜欢指定条形组的顺序,例如将 Soziale Madien 置于左侧。下面的图的代码可在上面的链接中找到,并在此处使用我任意创建的数据复制,以便您可以尝试生成类似的图。

library(ggplot2)

AllCoursesReg <- data.frame(name=c( 'a', 'b', 'c', 'd', 'e','a', 'b', 'c', 'd', 'e', 'a', 'b', 'c', 'd', 'e','a', 'b', 'c', 'd', 'e'), Course=c('Gratis Online Lernen 2014', 'Soziale Medien', 'Soziale Medien', 'Gratis Online Lernen 2014', 'Lernen im Netz', 'Soziale Medien', 'Lernen im Netz', 'Gratis Online Lernen 2014', 'Lernen im Netz', 'Soziale Medien', 'Gratis Online Lernen 2014', 'Lernen im Netz', 'Soziale Medien', 'Gratis Online Lernen 2014', 'Lernen im Netz', 'Soziale Medien', 'Lernen im Netz', 'Gratis Online Lernen 2014', 'Lernen im Netz', 'Soziale Medien'), Status=c('Registrants','Certified', 'Registrants','Certified', 'Certified', 'Registrants','Certified', 'Registrants', 'Registrants','Registrants', 'Certified', 'Registrants','Certified', 'Registrants','Certified', 'Certified','Certified', 'Registrants', 'Certified','Certified'))

ggplot(AllCoursesReg, aes(Course, fill = Status)) +
geom_bar(aes(order = Status), position = "dodge", colour = "black") + theme_bw()+
guides(fill = guide_legend(reverse = TRUE))
enter image description here
谢谢。

最佳答案

您可以指定 xlimits:

desired = c('Soziale Medien', 'Lernen im Netz', 'Gratis Online Lernen 2014')

ggplot(AllCoursesReg, aes(Course, fill = Status)) +
geom_bar(position = "dodge", colour = "black") +
theme_bw()+
scale_x_discrete(limits=desired)+
guides(fill = guide_legend(reverse = TRUE))

或者改变因子的水平:
AllCoursesReg$Course = factor(AllCoursesReg$Course,levels=desired)

ggplot(AllCoursesReg, aes(Course, fill = Status)) +
geom_bar(position = "dodge", colour = "black") +
theme_bw()+
guides(fill = guide_legend(reverse = TRUE))

enter image description here

关于r - 如何指定使用闪避位置绘制的条形组的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60763182/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com