ggplot - 按多列排序x轴标签

时间:2017-06-06 13:25:01

标签: r ggplot2

使用如下表格

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()

enter image description here

正如预期的那样,model的x轴标签按其级别按升序排列 - 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
> 

1 个答案:

答案 0 :(得分:2)

您可以更改模型变量的因子级别的顺序,然后绘制。像这样:

df <- df[with(df, order(tier, make, -sales)),]
df$model <- factor(df$model, levels = unique(df$model))
ggplot(df, aes(x=model, y=sales)) +
  geom_point()

第一行更改行的顺序。第二行是实际的重新排序。 unique(df$model)是变量的当前顺序,通过使用它作为因子的级别,您可以按此顺序绘制数据。