R 如何指定使用dodge position打印的条形图组的顺序

R 如何指定使用dodge position打印的条形图组的顺序,r,ggplot2,R,Ggplot2,我有一个问题与我的问题有关。我不想指定每个条形图组中各个条形图的顺序,例如将Certified放置在下图的右侧(从上面的链接),而是想指定条形图组的顺序,例如将Soziale Madien放置在左侧。下面的绘图代码可以在上面的链接中找到,并且在这里与我任意创建的数据一起复制,以便您可以尝试生成类似的绘图 library(ggplot2) AllCoursesReg <- data.frame(name=c( 'a', 'b', 'c', 'd', 'e','a', 'b', 'c', '

我有一个问题与我的问题有关。我不想指定每个条形图组中各个条形图的顺序,例如将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))
库(ggplot2)

AllCoursSereg您可以指定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))

你试过什么?你能发布代码和样本数据吗?请使用您运行的代码和
dput(df)
的输出编辑问题。或者,如果dput(头部(df,20))的输出太大
。(
df
是数据集的名称。)您需要按所需顺序将x轴划分为因子。请看我在这里的回答:@Rui Barradas可以在我帖子的链接上找到该情节的代码。我把我的帖子改成这样。谢谢。@Dave2对不起,我还不清楚。我的帖子中显示的绘图代码可以在我帖子的链接上找到。能否显示将Soziale Medien置于左侧的修改代码(即指定三个条形组的顺序)?谢谢。仅供参考,我们无法复制您提供的帖子中的情节。没有数据可以说