2

这是我要修改的绘图的数据:

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),我希望大小等于 variable 的值,而不是 using ,rating即 5 用于以 B 结尾的名称,3 用于以 A 结尾的名称。

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

  3. 如果出现第二个(或第三个)图例,如何抑制所选图例的出现?- 因为使用opts(legend.position = "none")会使它们全部不可见。

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

提前致谢

编辑

我在第 4 号中发现了问题。我应该给调色板 =“RdYlGn”,而不是 pal="RdYlGn"。

4

2 回答 2

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

在此处输入图像描述

于 2012-09-06T19:24:30.323 回答
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"))

在此处输入图像描述

于 2012-09-06T19:17:45.203 回答