geom_area组并填充不同的变量

时间:2015-10-19 08:24:50

标签: r ggplot2 fill facet

我有一个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 ~ .)
  

错误:美学不能随着色带而变化

2 个答案:

答案 0 :(得分: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))

答案 1 :(得分:0)

您也可以尝试以下方法:

ggplot(df, aes(x=censo, y=pCensEu, color=estacion, group=interaction(tipoEuro,estacion))) +
  geom_area() +
  geom_line()+ facet_grid(tipoEuro ~ .)
相关问题