使用facet_grid或facet_wrap根据组的最大值和最小值独立地修复y_scale

时间:2014-02-25 06:57:43

标签: r ggplot2 facet-wrap

我正在尝试使用facet_grid()或facet_wrap()来绘制几个时间序列。

首先:当我使用变量tac时,我收到此错误:

Error in seq.default(from = best$lmin, to = best$lmax, by = best$lstep) :'from' must be of length 1 

...当我使用变量cpue这个警告:

geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?

我无法找到问题所在,所以:

我知道如何修复它吗?

第二次

根据'渔业'中的群体,我的变量'tac'的范围不同。例如,'tasAba'的范围在2100和2660之间,'sawzbGL'在27和41之间。因此,'tasAba'的行将显示一个模式,'sawzbGL'的行将是平的。所以:

我想独立修复y_scale,根据渔业中每个群体的最大值和最小值,是否可以在ggplot中使用?

非常感谢任何帮助。

P.S。

这是我的代码:

  ggplot(data=lobaba_aba,aes(x=season, y=cpue, colour=fishery)) +
  geom_line()+facet_grid(fishery ~., scales = "free", as.table = TRUE)

如果你想尝试一下,这只是我数据的一小部分:

 season fishery tac cpue
1990    nswAba  NA  20
1991    nswAba  NA  19
1992    nswAba  NA  18
1993    nswAba  NA  20
1994    nswAba  NA  22
1995    nswAba  NA  25
1996    nswAba  333 21
1997    nswAba  333 20
1998    nswAba  333 19
1999    nswAba  333 18
2000    nswAba  305 19
2001    nswAba  305 25
2002    nswAba  300 21
2003    nswAba  291 16
2004    nswAba  283 14
2005    nswAba  130 12
2006    nswAba  125 15
2007    nswAba  110 18
2008    nswAba  105 18
2009    nswAba  73  18
2010    nswAba  94  35
2011    nswAba  109 39
1990    tasAba  2100    61
1991    tasAba  2100    62
1992    tasAba  2100    71
1993    tasAba  2100    73
1994    tasAba  2100    79
1995    tasAba  2100    80
1996    tasAba  2100    85
1997    tasAba  2520    90
1998    tasAba  2520    98
1999    tasAba  2520    91
2000    tasAba  2690    102
2001    tasAba  2660    90
2002    tasAba  2397    84
2003    tasAba  2468    82
2004    tasAba  2380    85
2005    tasAba  2380    84
2006    tasAba  2380    86
2007    tasAba  2380    75.5
2008    tasAba  2471    72.8
2009    tasAba  2482    76.3
2010    tasAba  2527    65.2
2011    tasAba  2422    57.8
1990    sawzaBL 293.25  68.65
1991    sawzaBL 293.25  69.97
1992    sawzaBL 293.25  64.77
1993    sawzaBL 293.25  64.94
1994    sawzaBL 293.25  66.09
1995    sawzaBL 293.25  68.38
1996    sawzaBL 258 63.27
1997    sawzaBL 293.25  66.35
1998    sawzaBL 293.25  65.47
1999    sawzaBL 293.25  66.44
2000    sawzaBL 293.25  68.12
2001    sawzaBL 293.25  74.74
2002    sawzaBL 293.25  75.09
2003    sawzaBL 293.25  79.59
2004    sawzaBL 293.25  78.35
2005    sawzaBL 293.25  77.3
2006    sawzaBL 293.25  79.85
2007    sawzaBL 293.25  76.06
2008    sawzaBL 293.25  74.47
2009    sawzaBL 293.25  69.97
2010    sawzaBL 276 69.8
2011    sawzaBL 276 68.21
1990    sawzaGL 207 65.7
1991    sawzaGL 207 67.4
1992    sawzaGL 207 62.6
1993    sawzaGL 207 63.3
1994    sawzaGL 207 66.3
1995    sawzaGL 207 63.8
1996    sawzaGL 207 63.5
1997    sawzaGL 207 64.4
1998    sawzaGL 207 68.2
1999    sawzaGL 207 67.8
2000    sawzaGL 207 71.3
2001    sawzaGL 207 75
2002    sawzaGL 207 80.2
2003    sawzaGL 207 85.8
2004    sawzaGL 207 83.2
2005    sawzaGL 207 83.5
2006    sawzaGL 227.7   81.3
2007    sawzaGL 227.7   80.3
2008    sawzaGL 227.7   79.8
2009    sawzaGL 227.7   72.7
2010    sawzaGL 207 72.5
2011    sawzaGL 207 72.3
1990    sawzbBL NA  49.61
1991    sawzbBL 27.6    42.4
1992    sawzbBL 27.6    53.7
1993    sawzbBL 34.5    63.17
1994    sawzbBL 41.4    48.5
1995    sawzbBL 41.4    51.16
1996    sawzbBL 41.4    53.13
1997    sawzbBL 41.4    52.19
1998    sawzbBL 41.4    57.08
1999    sawzbBL 41.4    50.13
2000    sawzbBL 41.4    44.89
2001    sawzbBL 41.4    51.07
2002    sawzbBL 41.4    53.65
2003    sawzbBL 41.4    48.33
2004    sawzbBL 41.4    49.01
2005    sawzbBL 41.4    51.07
2006    sawzbBL 41.4    55.19
2007    sawzbBL 41.4    51.33
2008    sawzbBL 41.4    49.01
2009    sawzbBL 41.4    52.01
2010    sawzbBL 41.4    40.94
2011    sawzbBL 27.6    31.16
1990    sawzbGL NA  44.43
1991    sawzbGL 27.6    50.96
1992    sawzbGL 27.6    57.39
1993    sawzbGL 34.5    53.98
1994    sawzbGL 41.4    51.24
1995    sawzbGL 41.4    43.77
1996    sawzbGL 41.4    43.3
1997    sawzbGL 41.4    39.99
1998    sawzbGL 41.4    40.46
1999    sawzbGL 41.4    51.05
2000    sawzbGL 41.4    52.66
2001    sawzbGL 41.4    51.05
2002    sawzbGL 41.4    49.54
2003    sawzbGL 41.4    52.85
2004    sawzbGL 41.4    48.41
2005    sawzbGL 41.4    45.57
2006    sawzbGL 41.4    44.06
2007    sawzbGL 41.4    46.33
2008    sawzbGL 41.4    48.5
2009    sawzbGL 41.4    41.22
2010    sawzbGL 41.4    42.07
2011    sawzbGL 27.6    36.68
1990    saczBL  41.1    59.43
1991    saczBL  41.1    75.69
1992    saczBL  41.1    58.88
1993    saczBL  41.1    63.71
1994    saczBL  42.3    67.03
1995    saczBL  42.3    67.59
1996    saczBL  42.3    64.27
1997    saczBL  42.3    61.92
1998    saczBL  42.3    63.16
1999    saczBL  42.3    60.4
2000    saczBL  42.3    71.18
2001    saczBL  42.3    71.3
2002    saczBL  42.3    66.34
2003    saczBL  42.3    75.46
2004    saczBL  42.3    72.42
2005    saczBL  29.7    66.06
2006    saczBL  24.3    63.58
2007    saczBL  24.3    70.07
2008    saczBL  24.3    61.92
2009    saczBL  24.3    69.11
2010    saczBL  24.3    66.06
2011    saczBL  24.3    62.47
1990    saczGL  142.2   61.94
1991    saczGL  142.2   60.94
1992    saczGL  142.2   60.27
1993    saczGL  142.2   61.27
1994    saczGL  143.1   57.76
1995    saczGL  143.1   55.59
1996    saczGL  143.1   64.27
1997    saczGL  143.1   59.1
1998    saczGL  143.1   61.6
1999    saczGL  143.1   65.44
2000    saczGL  143.1   83.97
2001    saczGL  143.1   86.48
2002    saczGL  143.1   79.13
2003    saczGL  143.1   83.64
2004    saczGL  143.1   82.8
2005    saczGL  143.1   75.13
2006    saczGL  143.1   76.79
2007    saczGL  143.1   70.95
2008    saczGL  143.1   75.63
2009    saczGL  143.1   77.13
2010    saczGL  143.1   72.29
2011    saczGL  143.1   67.11
2003    saszfdaBL   51  72.09
2004    saszfdaBL   51  66.59
2005    saszfdaBL   45  75.82
2006    saszfdaBL   45  84.18
2007    saszfdaBL   45  88.13
2008    saszfdaBL   45  74.07
2009    saszfdaBL   45  67.91
2003    sasznfdaBL  96  93.81
2004    sasznfdaBL  96  98.68
2005    sasznfdaBL  99  102.78
2006    sasznfdaBL  99  97.27
2007    sasznfdaBL  99  102.1
2008    sasznfdaBL  99  104.75
2009    sasznfdaBL  99  106
1990    vicwzAba    NA  77
1991    vicwz   NA  75
1992    vicwz   NA  69
1993    vicwz   NA  68
1994    vicwz   NA  64
1995    vicwz   NA  64
1996    vicwz   NA  73
1997    vicwz   NA  76
1998    vicwz   NA  79
1999    vicwz   NA  78
2000    vicwz   NA  87
2001    vicwz   NA  85
2002    vicwz   266 79
2003    vicwz   275 114
2004    vicwz   254 96
2005    vicwz   221 72
2006    vicwz   168 74
2007    vicwz   114 83
2008    vicwz   20  51
2009    vicwz   26  52
2010    vicwz   29  49
2011    vicwz   53.2    NA
1990    viccz   NA  65
1991    viccz   NA  60
1992    viccz   NA  61
1993    viccz   NA  63
1994    viccz   NA  65
1995    viccz   NA  70
1996    viccz   NA  80
1997    viccz   NA  83
1998    viccz   NA  90
1999    viccz   NA  90
2000    viccz   NA  95
2001    viccz   NA  96
2002    viccz   665 95
2003    viccz   673 123
2004    viccz   617 95
2005    viccz   620 86
2006    viccz   620 75
2007    viccz   620 74
2008    viccz   465 80
2009    viccz   432 79
2010    viccz   285 72
2011    viccz   331.9   NA
1990    vicez   NA  69
1991    vicez   NA  67
1992    vicez   NA  70
1993    vicez   NA  73
1994    vicez   NA  81
1995    vicez   NA  82
1996    vicez   NA  83
1997    vicez   NA  85
1998    vicez   NA  85
1999    vicez   NA  85
2000    vicez   NA  91
2001    vicez   NA  92
2002    vicez   465 87
2003    vicez   488 95
2004    vicez   488 158
2005    vicez   489 105
2006    vicez   489 109
2007    vicez   490 109
2008    vicez   490 109
2009    vicez   460 105
2010    vicez   428 106
2011    vicez   432 NA
1990    nzP2    121.19  111
1991    nzP2    121.19  91
1992    nzP2    121.19  86
1993    nzP2    121.19  96
1994    nzP2    121.19  92
1995    nzP2    121.19  107
1996    nzP2    121.19  108
1997    nzP2    121.19  111
1998    nzP2    121.19  139
1999    nzP2    121.19  155
2000    nzP2    121.19  162
2001    nzP2    121.19  138
2002    nzP2    121.19  149
2003    nzP2    121.19  157
2004    nzP2    121.19  166
2005    nzP2    121.19  147
2006    nzP2    121.19  156
2007    nzP2    121.19  147
2008    nzP2    121.19  NA
2009    nzP2    121.19  NA
2010    nzP2    121.19  NA
2011    nzP2    121.19  NA
1990    nzP3    77.24   153
1991    nzP3    91.5    111
1992    nzP3    91.5    97
1993    nzP3    91.5    120
1994    nzP3    91.5    105
1995    nzP3    91.62   100
1996    nzP3    91.62   104
1997    nzP3    91.62   113
1998    nzP3    91.62   112
1999    nzP3    91.62   127
2000    nzP3    91.62   116
2001    nzP3    91.62   107
2002    nzP3    91.62   113
2003    nzP3    91.62   102
2004    nzP3    91.62   115
2005    nzP3    91.62   105
2006    nzP3    91.62   127
2007    nzP3    91.62   108
2008    nzP3    91.62   NA
2009    nzP3    91.62   NA
2010    nzP3    91.62   NA
2011    nzP3    91.62   NA
1990    nzP4    287.25  336.36
1991    nzP4    287.25  389.42
1992    nzP4    287.25  374.93
1993    nzP4    287.25  300.46
1994    nzP4    287.25  292.91
1995    nzP4    326.54  313.24
1996    nzP4    326.54  333.81
1997    nzP4    326.54  334.7
1998    nzP4    326.54  294.05
1999    nzP4    326.54  397.36
2000    nzP4    326.54  361.98
2001    nzP4    326.54  425.98
2002    nzP4    326.54  465.96
2003    nzP4    326.54  472.01
2004    nzP4    326.54  449.08
2005    nzP4    326.54  420.47
2006    nzP4    326.54  433.44
2007    nzP4    326.54  448.6
2008    nzP4    326.54  396.61
2009    nzP4    326.54  NA
2010    nzP4    326.54  NA
2011    nzP4    326.54  NA
1990    nzP5A   NA  1.385
1991    nzP5A   NA  1.21
1992    nzP5A   NA  1.18
1993    nzP5A   NA  1.005
1994    nzP5A   NA  1.05
1995    nzP5A   148.98  1.13
1996    nzP5A   148.98  1.055
1997    nzP5A   148.98  1.135
1998    nzP5A   148.98  1.04
1999    nzP5A   148.98  1.08
2000    nzP5A   148.98  1.085
2001    nzP5A   148.98  1.1
2002    nzP5A   148.98  1.165
2003    nzP5A   148.98  1.16
2004    nzP5A   148.98  1.04
2005    nzP5A   148.98  1.065
2006    nzP5A   148.98  1.04
2007    nzP5A   148.98  0.96
2008    nzP5A   148.98  0.94
2009    nzP5A   148.98  1.055
2010    nzP5A   148.98  NA
2011    nzP5A   148.98  NA
1990    nzP5B   NA  200.7
1991    nzP5B   NA  186.7
1992    nzP5B   NA  171.4
1993    nzP5B   NA  155.3
1994    nzP5B   NA  145.1
1995    nzP5B   148.98  127.2
1996    nzP5B   148.98  152
1997    nzP5B   148.98  142.8
1998    nzP5B   148.98  136.3
1999    nzP5B   143.98  146.2
2000    nzP5B   112.19  115.6
2001    nzP5B   112.19  154.6
2002    nzP5B   90  157
2003    nzP5B   90  159.9
2004    nzP5B   90  174.9
2005    nzP5B   90  194.9
2006    nzP5B   90  NA
2007    nzP5B   90  NA
2008    nzP5B   90  NA
2009    nzP5B   90  NA
2010    nzP5B   90  NA
2011    nzP5B   90  NA
1990    nzP5D   NA  1.04
1991    nzP5D   NA  1.21
1992    nzP5D   NA  1.05
1993    nzP5D   NA  1.2
1994    nzP5D   NA  1.06
1995    nzP5D   148.98  1.08
1996    nzP5D   148.98  0.99
1997    nzP5D   148.98  0.9
1998    nzP5D   148.98  0.78
1999    nzP5D   148.98  1.11
2000    nzP5D   148.98  0.93
2001    nzP5D   148.98  0.84
2002    nzP5D   114 0.81
2003    nzP5D   89  0.84
2004    nzP5D   89  0.79
2005    nzP5D   89  0.91
2006    nzP5D   89  1.13
2007    nzP5D   89  1.05
2008    nzP5D   89  1.04
2009    nzP5D   89  1.17
2010    nzP5D   89  1.19
2011    nzP5D   89  1.33
1990    nzP7    266.24  119.7
1991    nzP7    266.17  118.8
1992    nzP7    266.17  100.8
1993    nzP7    266.17  115.8
1994    nzP7    266.17  113.6
1995    nzP7    267.48  109
1996    nzP7    267.48  108
1997    nzP7    267.48  95.3
1998    nzP7    267.48  96.5
1999    nzP7    267.48  104
2000    nzP7    213.98  72.1
2001    nzP7    240.73  54.3
2002    nzP7    187.24  61.8
2003    nzP7    159.15  61
2004    nzP7    159.15  61.6
2005    nzP7    159.15  65.9
2006    nzP7    159.15  95.2
2007    nzP7    187.24  98
2008    nzP7    187.24  86
2009    nzP7    187.24  89
2010    nzP7    187.24  90
2011    nzP7    187.24  92

1 个答案:

答案 0 :(得分:3)

错误是因为有一个渔场(“vicwzAba”)只有1个非NA值。

table(lobaba_aba$fishery)

##  nswAba       nzP2       nzP3       nzP4      nzP5A      nzP5B      nzP5D 
##      22         22         22         22         22         22         22 
##    nzP7     saczBL     saczGL  saszfdaBL sasznfdaBL    sawzaBL    sawzaGL 
##      22         22         22          7          7         22         22 
## sawzbBL    sawzbGL     tasAba      viccz      vicez      vicwz   vicwzAba 
##      22         22         22         22         22         21          1 

删除该渔场可修复错误。您的代码生成的图表似乎符合您的要求。它们在y轴上具有独立的尺度,并随着时间的推移显示每个渔业。

dat <- lobaba_aba[lobaba_aba$fishery != "vicwzAba",]

p <- ggplot(data = dat, aes(x = season, y = cpue, colour = fishery)) +
    geom_line() + facet_grid(fishery ~ ., scales = "free", as.table = TRUE)

p

enter image description here

p %+% aes(y = tac)

enter image description here