R ggplot中分类变量的排序

R ggplot中分类变量的排序,r,ggplot2,R,Ggplot2,您好,我希望使用ggplot2生成一个图形,但不使用分类变量的默认排序(按字母顺序,在脚本:字母中),而是使用连续变量的关联值(在脚本:数字中) 下面是一个示例脚本: library(ggplot2) trial<-data.frame(letters=letters, numbers=runif(n=26,min=1,max=26)) trial<-trial[sample(1:26,26),] trial.plot<-qplot(x=numbers, y=letters,

您好,我希望使用ggplot2生成一个图形,但不使用分类变量的默认排序(按字母顺序,在脚本:字母中),而是使用连续变量的关联值(在脚本:数字中)

下面是一个示例脚本:

library(ggplot2)
trial<-data.frame(letters=letters, numbers=runif(n=26,min=1,max=26))
trial<-trial[sample(1:26,26),]
trial.plot<-qplot(x=numbers, y=letters, data=trial)
trial.plot
trial<-trial[order(trial$numbers),]
trial.plot<-qplot(x=numbers, y=letters, data=trial)
trial.plot
trial.plot+stat_sort(variable=numbers)
库(ggplot2)

试用我非常确定
统计排序
不存在,因此它不能像您认为的那样工作也就不足为奇了。幸运的是,有一个
reorder()
函数,它根据第二个变量的值对分类变量的级别进行重新排序。我认为这应该满足您的要求:

trial.plot <- qplot( x = numbers, y = reorder(letters, numbers), data = trial)
trial.plot

trial.plot如果你能更具体地说明你想要它的外观,我认为社区可以对我的答案进行改进,不管你想要的是什么:

qplot(numbers, reorder(letters, numbers), data=trial)

就我个人而言,我喜欢事先整理我的数据。记住,这不是一个有序因子,而是一个顺序正确的因子。这两种方法都令人惊讶。谢谢