ggplot 不绘制 data.frame 的顺序

ggplot doesn't plot the order of the data.frame

如果我head(df)喜欢:

    feature Comparison   Primary       diff key
1     work  15.441176 20.588235  5.1470588   1
2 employee  22.794118 19.117647 -3.6764706   2
3     good  11.029412 11.764706  0.7352941   3
4  improve   8.088235 10.294118  2.2058824   4
5   career   2.941176  8.823529  5.8823529   5
6  manager   2.941176  8.823529  5.8823529   6

我正在尝试绘制一些东西:

 p = ggplot(x, aes(x = feature,size=8)) + geom_point(aes(y = Primary)) +
        geom_point(aes(y=Comparison)) + coord_flip()
      ggplotly(p)

是否有我遗漏的东西导致 p 无法绘制上面数据的顺序?地块上的前五个是

work
train
time
skill
people

但是根据df,应该是work,employee,good,improve,career。

这些东西叫做 "levels",ggplot 使用它来确定事物在图中出现的顺序。如果您在控制台中 运行 levels(x$feature),那么我敢打赌您看到的列表与图中显示的顺序相同。

要让它们按您想要的顺序显示,您只需覆盖功能列的 "levels"。

x$feature = factor(x$feature, levels = c("work",
                                         "employee",
                                         "good",
                                         "improve",
                                         "manager"))