在ggplot2中订购问题

时间:2012-09-10 10:18:02

标签: r ggplot2

用于绘图的数据如下:

mdfr3 <- read.table(textConnection("
name  stadio    variable    value   rating
541B    2   start_date  22/02/2011  5
541B    4   start_date  24/05/2011  5
541B    5   start_date  01/06/2011  5
541B    7   start_date  20/09/2011  5
579B    2   start_date  07/06/2011  5
579B    4   start_date  08/02/2012  5
579B    5   start_date  09/02/2012  5
579B    7   start_date  24/04/2012  5
700B    2   start_date  29/02/2012  5
700B    4   start_date  29/02/2012  5
700B    5   start_date  29/02/2012  5
700B    7   start_date  08/05/2012  5
719B    2   start_date  29/06/2012  5
719B    4   start_date  10/07/2012  5
719B    5   start_date  19/07/2012  5
719B    7   start_date  16/10/2012  5
737B    2   start_date  22/06/2012  5
737B    4   start_date  25/07/2012  5
737B    5   start_date  01/08/2012  5
737B    7   start_date  23/10/2012  5
635B    2   start_date  03/05/2012  5
635B    4   start_date  29/06/2012  5
635B    5   start_date  05/07/2012  5
635B    7   start_date  11/09/2012  5
558B    2   start_date  30/05/2011  5
558B    4   start_date  17/10/2011  5
558B    5   start_date  27/01/2012  5
558B    7   start_date  06/04/2012  5
734B    2   start_date  16/03/2012  5
734B    4   start_date  16/03/2012  5
734B    5   start_date  16/03/2012  5
734B    7   start_date  25/04/2012  5
727B    2   start_date  23/12/2011  5
727B    4   start_date  03/02/2012  5
727B    5   start_date  16/02/2012  5
727B    7   start_date  18/05/2012  5
725B    2   start_date  28/12/2011  5
725B    4   start_date  23/02/2012  5
725B    5   start_date  02/03/2012  5
725B    7   start_date  31/05/2012  5
733B    2   start_date  30/12/2011  5
733B    4   start_date  23/02/2012  5
733B    5   start_date  02/03/2012  5
733B    7   start_date  07/06/2012  5
541B    2   end_date    24/05/2011  5
541B    4   end_date    01/06/2011  5
541B    5   end_date    20/09/2011  5
541B    7   end_date    28/09/2012  5
579B    2   end_date    08/02/2012  5
579B    4   end_date    09/02/2012  5
579B    5   end_date    24/04/2012  5
579B    7   end_date    29/10/2012  5
700B    2   end_date    29/02/2012  5
700B    4   end_date    29/02/2012  5
700B    5   end_date    08/05/2012  5
700B    7   end_date    12/11/2012  5
719B    2   end_date    10/07/2012  5
719B    4   end_date    19/07/2012  5
719B    5   end_date    16/10/2012  5
719B    7   end_date    28/02/2013  5
737B    2   end_date    25/07/2012  5
737B    4   end_date    01/08/2012  5
737B    5   end_date    23/10/2012  5
737B    7   end_date    28/02/2013  5
635B    2   end_date    29/06/2012  5
635B    4   end_date    05/07/2012  5
635B    5   end_date    11/09/2012  5
635B    7   end_date    04/02/2013  5
558B    2   end_date    17/10/2011  5
558B    4   end_date    27/01/2012  5
558B    5   end_date    06/04/2012  5
558B    7   end_date    09/09/2012  5
734B    2   end_date    16/03/2012  5
734B    4   end_date    16/03/2012  5
734B    5   end_date    25/04/2012  5
734B    7   end_date    04/07/2012  5
727B    2   end_date    03/02/2012  5
727B    4   end_date    16/02/2012  5
727B    5   end_date    18/05/2012  5
727B    7   end_date    19/11/2012  5
725B    2   end_date    23/02/2012  5
725B    4   end_date    02/03/2012  5
725B    5   end_date    31/05/2012  5
725B    7   end_date    30/11/2012  5
733B    2   end_date    23/02/2012  5
733B    4   end_date    02/03/2012  5
733B    5   end_date    07/06/2012  5
733B    7   end_date    30/11/2012  5
")->con,header=T);close(con)


mdfr4 <- read.table(textConnection("
name    stadio  variable    value   rating
541A    1   start_date  22/02/2011  3
541A    3   start_date  24/05/2011  3
541A    6   start_date  01/06/2011  3
541A    8   start_date  20/09/2011  3
579A    1   start_date  07/06/2011  3
579A    3   start_date  08/02/2012  3
579A    6   start_date  09/02/2012  3
579A    8   start_date  08/05/2012  3
700A    1   start_date  22/03/2012  3
700A    3   start_date  22/03/2012  3
700A    6   start_date  22/03/2012  3
700A    8   start_date  12/06/2012  3
719A    1   start_date  10/07/2012  3
719A    3   start_date  17/07/2012  3
719A    6   start_date  26/07/2012  3
719A    8   start_date  09/10/2012  3
737A    1   start_date  01/07/2012  3
737A    3   start_date  06/07/2012  3
737A    6   start_date  26/07/2012  3
737A    8   start_date  16/10/2012  3
635A    1   start_date  03/05/2012  3
635A    3   start_date  31/07/2012  3
635A    6   start_date  31/07/2012  3
635A    8   start_date  23/10/2012  3
558A    1   start_date  30/05/2011  3
558A    3   start_date  17/10/2011  3
558A    6   start_date  27/01/2012  3
558A    8   start_date  06/04/2012  3
734A    1   start_date  16/03/2012  3
734A    3   start_date  16/03/2012  3
734A    6   start_date  16/03/2012  3
734A    8   start_date  25/04/2012  3
727A    1   start_date  23/12/2011  3
727A    3   start_date  03/02/2012  3
727A    6   start_date  16/02/2012  3
727A    8   start_date  18/05/2012  3
725A    1   start_date  28/12/2011  3
725A    3   start_date  23/02/2012  3
725A    6   start_date  02/03/2012  3
725A    8   start_date  31/05/2012  3
733A    1   start_date  30/12/2011  3
733A    3   start_date  23/02/2012  3
733A    6   start_date  02/03/2012  3
733A    8   start_date  07/06/2012  3
541A    1   end_date    24/05/2011  3
541A    3   end_date    01/06/2011  3
541A    6   end_date    20/09/2011  3
541A    8   end_date    17/01/2013  3
579A    1   end_date    08/02/2012  3
579A    3   end_date    09/02/2012  3
579A    6   end_date    08/05/2012  3
579A    8   end_date    19/12/2012  3
700A    1   end_date    22/03/2012  3
700A    3   end_date    22/03/2012  3
700A    6   end_date    12/06/2012  3
700A    8   end_date    18/12/2012  3
719A    1   end_date    17/07/2012  3
719A    3   end_date    26/07/2012  3
719A    6   end_date    09/10/2012  3
719A    8   end_date    21/12/2012  3
737A    1   end_date    06/07/2012  3
737A    3   end_date    26/07/2012  3
737A    6   end_date    16/10/2012  3
737A    8   end_date    28/12/2012  3
635A    1   end_date    31/07/2012  3
635A    3   end_date    31/07/2012  3
635A    6   end_date    23/10/2012  3
635A    8   end_date    22/03/2013  3
558A    1   end_date    17/10/2011  3
558A    3   end_date    27/01/2012  3
558A    6   end_date    06/04/2012  3
558A    8   end_date    10/01/2013  3
734A    1   end_date    16/03/2012  3
734A    3   end_date    16/03/2012  3
734A    6   end_date    25/04/2012  3
734A    8   end_date    20/09/2012  3
727A    1   end_date    03/02/2012  3
727A    3   end_date    16/02/2012  3
727A    6   end_date    18/05/2012  3
727A    8   end_date    19/11/2012  3
725A    1   end_date    23/02/2012  3
725A    3   end_date    02/03/2012  3
725A    6   end_date    31/05/2012  3
725A    8   end_date    30/11/2012  3
733A    1   end_date    23/02/2012  3
733A    3   end_date    02/03/2012  3
733A    6   end_date    07/06/2012  3
733A    8   end_date    30/11/2012  3
")->con,header=T);close(con)

mdfr3$name <- factor(mdfr3$name,levels(factor(mdfr3$name))[c(1,3,5,6,11,4,2,10,8,7,9)])
mdfr3$rating <- factor(mdfr3$rating)
mdfr4$name <- factor(mdfr4$name,levels(factor(mdfr4$name))[c(1,3,5,6,11,4,2,10,8,7,9)])
mdfr4$rating <- factor(mdfr4$rating)

p图显示y轴上的值完全按照我希望的方式排序。

p <- ggplot(mdfr3, aes(as.Date(value, "%d/%m/%Y"), name, colour = factor(stadio))) +
  geom_line(size = 5) +
  scale_size_manual(breaks = c("3","5"),  values = c(3,5), guide = "none") +
  labs(colour="Legend:", x = "", y = "") +
  scale_colour_brewer(palette="BrBG",breaks = c("2", "4", "5","7"),
                      labels = c("Level 1", "Level 2", "Level 3","Level 4"))

但是当我发出以下命令时,顺序会发生变化。

  p + geom_line(data=mdfr4,aes(size = rating, colour = factor(stadio)))

为什么最后一行会改变y轴上标签的顺序?我怎样才能防止这种情况发生?

1 个答案:

答案 0 :(得分:0)

我怀疑ggplot会在geom_line次来电之间放弃你未使用的因素 - 这会影响你的订购。

我认为一个简单的解决方法是:

mdfr = rbind(mdfr3, mdfr4)
p <- ggplot(mdfr, aes(as.Date(value, "%d/%m/%Y"), name, colour = factor(stadio))) + ...

这意味着您不需要最终geom_line,并且您的订购将被保留。