2015-10-19 49 views
0

我有一个geom_area情节,看起来像这样填:geom_area组和不同的变量

enter image description here

x轴是时间序列,我要的颜色各方面的按组的填充变量“estacion”(一年的季节)。这是我的数据样本:

año  censo  estacion  tipoEuro  censEu censTot  pCensEu 
2010 2010-01-01 Invierno HA frisona   13  32  40.62500 
2010 2010-01-01 Invierno Bovinos jovenes  10  32  31.25000 
2010 2010-01-02 Invierno HA frisona   13  32  40.62500 
--- 
2014 2014-12-30 Invierno Bovinos jovenes  15  26  57.69231 
2014 2014-12-31 Invierno HA frisona   3  26  11.53846 
2014 2014-12-31 Invierno Terneros    8  26  30.76923 

这里的我使用,使剧情代码:

ggplot(censTot1,aes(x=censo, y=pCensEu,group=tipoEuro)) + 
    geom_area() + 
    geom_line()+ facet_grid(tipoEuro ~ .) 

,这是我打算使用的代码,并生成该错误:

ggplot(censTot1,aes(x=censo,y=pCensEu,group=tipoEuro,fill=estacion)) + 
    geom_area() + 
    geom_line()+ facet_grid(tipoEuro ~ .) 

Error: Aesthetics can not vary with a ribbon

回答

0

我不确定所需的输出。这会解决你的问题吗?

library(ggplot2) 
ggplot(df, aes(x=censo, y=pCensEu,group=tipoEuro))+ 
    geom_area(aes(fill=estacion))+ 
    geom_line()+ 
    facet_grid(tipoEuro ~ .) 

数据使用

df <- structure(list(año = c(2010L, 2010L, 2010L, 2014L, 2014L, 2014L 
), censo = structure(c(1L, 1L, 2L, 3L, 4L, 4L), .Label = c("01/01/2010", 
"02/01/2010", "30/12/2014", "31/12/2014"), class = "factor"), 
    estacion = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "Invierno", class = "factor"), 
    tipoEuro = structure(c(2L, 1L, 2L, 1L, 2L, 3L), .Label = c("Bovinos jovenes", 
    "HA frisona", "Terneros"), class = "factor"), censEu = c(13L, 
    10L, 13L, 15L, 3L, 8L), censTot = c(32L, 32L, 32L, 26L, 26L, 
    26L), pCensEu = c(40.625, 31.25, 40.625, 57.69231, 11.53846, 
    30.76923)), .Names = c("año", "censo", "estacion", "tipoEuro", 
"censEu", "censTot", "pCensEu"), class = "data.frame", row.names = c(NA, 
-6L))