0
與表像下面ggplot - 訂貨x軸標籤,通過多列
df <- read.table(textConnection("
tier make model sales
entry Toyota Yeti 10000
entry Honda Jazz 8000
entry Nissan Sunny 5000
entry Honda Amaze 4000
entry Toyota Model10 3500
entry Nissan Beat 2000
Mid Honda Civic 4000
Mid Toyota Corolla 3000
Mid Honda Accord 2500
Mid Nissan Xtrail 2200
Mid Toyota Camry 1800
Mid Nissan Moon 800
"), header = TRUE)
> df
tier make model sales
1 entry Toyota Yeti 10000
2 entry Honda Jazz 8000
3 entry Nissan Sunny 5000
4 entry Honda Amaze 4000
5 entry Toyota Model10 3500
6 entry Nissan Beat 2000
7 Mid Honda Civic 4000
8 Mid Toyota Corolla 3000
9 Mid Honda Accord 2500
10 Mid Nissan Xtrail 2200
11 Mid Toyota Camry 1800
12 Mid Nissan Moon 800
當我繪製使用ggplot
通過模型的銷售爲以下我得到的情節在畫面
ggplot(df, aes(x=model, y=sales)) +
geom_point()
正如所預期的,x軸標籤model
在升序按照它們的電平 - Accord
至上和Yeti
最後。
> str(df)
'data.frame': 12 obs. of 4 variables:
$ tier : Factor w/ 2 levels "entry","Mid": 1 1 1 1 1 1 2 2 2 2 ...
$ make : Factor w/ 3 levels "Honda","Nissan",..: 3 1 2 1 3 2 1 3 1 2 ...
$ model: Factor w/ 12 levels "Accord","Amaze",..: 12 7 10 2 8 3 5 6 1 11 ...
$ sales: int 10000 8000 5000 4000 3500 2000 4000 3000 2500 2200 ...
>
不過,我需要順序不同的model
情節 - 當表由一線訂購了獲得,製造和銷售(下降)。我可以在下面的代碼中獲得該表的訂購順序 - 如何在圖中獲得model
的x軸標籤的相同順序?
> df[with(df, order(tier, make, -sales)),]
tier make model sales
2 entry Honda Jazz 8000
4 entry Honda Amaze 4000
3 entry Nissan Sunny 5000
6 entry Nissan Beat 2000
1 entry Toyota Yeti 10000
5 entry Toyota Model10 3500
7 Mid Honda Civic 4000
9 Mid Honda Accord 2500
10 Mid Nissan Xtrail 2200
12 Mid Nissan Moon 800
8 Mid Toyota Corolla 3000
11 Mid Toyota Camry 1800
>