How can I reorder the x axis in a plot in R?

前端 未结 2 829
星月不相逢
星月不相逢 2021-01-01 23:51

Using plot in R causes the factors on the x-axis to be alphabetically ordered.

How can I specify the order of the factors on the x-axis?

Examp

2条回答
  •  长发绾君心
    2021-01-02 00:40

    You just need to specify the levels of your factor in the order you want. So here I create a new variable x1

    x1  = factor(x, levels=c("B", "C", "A"))
    

    where

    R> x1
    [1] B B B A A A C C C
    Levels: B C A
    

    The plot function now works as expected.

    plot(y ~ x1)
    

提交回复
热议问题