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

时间:2018-04-18 18:15:19

标签: r ggplot2 ggplotly

If I have a head(df) like:

    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

and I'm trying to plot something with:

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

Is there something I'm missing that causes p not to plot the order of the data above? the first five on the plot are

work
train
time
skill
people

But according to the df, it should be work, employee, good, improve, career.

1 个答案:

答案 0 :(得分:1)

这些东西被称为"水平"哪个ggplot用来确定事物应该出现在图中的顺序。如果您在控制台中运行levels(x$feature),那么我打赌您看到的列表与图中显示的顺序相同。

要让它们按照您想要的顺序显示,您只需要覆盖"级别"对于要素栏。

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