ggplot2:geom_line的大小和位置

时间:2012-09-06 18:44:59

标签: r ggplot2

以下是我要修改的图表的数据:

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

我使用以下命令

创建了一个图
ggplot(mdfr, aes(as.Date(value, "%d/%m/%Y"), name, colour = factor(stadio))) +
    geom_line(size = 5) +
    xlab("") + ylab("") + labs(colour="Title") +
    scale_colour_brewer(pal="RdYlGn",breaks = c("1", "2", "3","4"), labels = c("Label 1", "Label 2", "Label 3","Label 4"))

我需要你的帮助才能做到以下几点:

  1. 更改名称以A结尾的行的小节大小。也就是说,我不希望使用geom_line(size = 5),而是希望大小等于变量rating的值,即对于以B结尾的名称为5,对于以A结尾的名称为3。

  2. 是否可以使名称中具有相同编号的条形看起来像成对,以便看起来像11对而不是22个单独的?例如737A比734B更接近737B。 (看起来像that chart)。

  3. 如果出现第二个(或第三个)图例,如何抑制所选图例的外观? - 因为使用opts(legend.position = "none")会使所有这些都不可见。

  4. 为什么这个ggplot命令会在ggplot 0.9.1中生成错误,如果我使用ggplot 0.8.9就可以了? (scale_map.discrete中的错误(scale,df [[j]]):尝试应用非函数)。我应该如何修改它?

  5. 提前致谢

    修改

    我发现No 4中的问题而不是pal =“RdYlGn”我应该给出palette =“RdYlGn”。

2 个答案:

答案 0 :(得分:3)

library(ggplot2)
library(stringr)

# This solves issue #2
mdfr <- mdfr[order(mdfr$name), ]

# This grabs the last letter (A or B)
mdfr$letter_size <- ifelse(str_sub(mdfr$name, start=-1)=="B", 5, 3)

# This solves #1 for you by scaling the size of the bar by letter.
# the scale_size() option removes it from the legend and solves #3.
ggplot(mdfr, aes(as.Date(value, "%d/%m/%Y"), name, colour = factor(stadio))) +
    geom_line(aes(size = mdfr$letter_size)) + 
    scale_size(guide="none") + 
    xlab("") + ylab("") + 
    labs(colour="Title")  + 
    scale_colour_brewer(palette = "RdYlGn", breaks = c("1", "2", "3","4"), 
        labels = c("Label 1", "Label 2", "Label 3","Label 4"))

enter image description here

答案 1 :(得分:1)

编辑,我误读了第一个问题

1)使用尺寸评级作为美学。

2)使用您提供的数据,数据以这种方式绘制,但您可以在ggplot中订购。

3)在要删除的比例中使用guide = none

ggplot(mdfr[order(mdfr$name),], aes(as.Date(value, "%d/%m/%Y"), name, colour = factor(stadio))) +
  geom_line(aes(size = as.factor(rating))) +
 labs(colour="Title", x = "", y = "") +
 scale_size_manual(breaks = c("3","5"), values = c(3,5), guide = "none")+
  scale_colour_brewer(palette="RdYlGn",breaks = c("1", "2", "3","4"), 
                      labels = c("Label 1", "Label 2", "Label 3","Label 4"))

enter image description here