0

I have a geom_area plot that looks like this:

enter image description here

The x-axis is a time serie, and i want to color the fill of each facet by groups of the variable "estacion" (seasons of the year). Here's a sample of my data:

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

Here's the code I'm using to make the plot:

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

and this is the code i intend to use, and the error generated:

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

zx8754
  • 52,746
  • 12
  • 114
  • 209
Romm
  • 21
  • 1
  • 8

2 Answers2

0

I'm not sure about the desired output. Would this solve your problem?

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

The data used

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))
Paulo E. Cardoso
  • 5,778
  • 32
  • 42
0

You can also try this:

ggplot(df, aes(x=censo, y=pCensEu, color=estacion, group=interaction(tipoEuro,estacion))) +
  geom_area() +
  geom_line()+ facet_grid(tipoEuro ~ .)
Ecg
  • 908
  • 1
  • 10
  • 28