multiple geom_line

16 views
Skip to first unread message

Alessandra Carioli

unread,
Mar 22, 2015, 8:20:59 AM3/22/15
to ggplot2
Hi,

I was trying to plot the results of a forecast where I have two groups (type) the “observed” and the “forecasted” data. 
The forecasted have multiple features:
pcd “c" and “M”
method “arfima” and “ets”
data (tells me where I truncated the time series) “*2000” and “*2005”
which I pasted together in a “group” variable

what I get is four graphs, but 3 of them lack the ‘observed’ time series. However, what I want is 4 graphs each containing the observed time series.

Any help will be greatly appreciated!

aut$group <- paste(aut$pcd,aut$method,sep=" ")
ggplot(aut)+
  geom_line(aes(Year,TFR,color=factor(group)),size = 0.8)+
  facet_grid(~data)

aut<-structure(list(Code = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L), .Label = "AUT", class = "factor"), pcd = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("c", "M"), class = "factor"), 
    method = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L), .Label = c("arfima", "ets"), class = "factor"), 
    data = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L), .Label = c("*2000", "*2005"), class = "factor"), 
    Year = c(2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 
    2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 2015L, 2006L, 
    2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 2015L, 
    2016L, 2017L, 2018L, 2019L, 2020L, 2001L, 2002L, 2003L, 2004L, 
    2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 
    2014L, 2015L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 
    2013L, 2014L, 2015L, 2016L, 2017L, 2018L, 2019L, 2020L, 2001L, 
    2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 
    2011L, 2012L, 2013L, 2014L, 2015L, 2006L, 2007L, 2008L, 2009L, 
    2010L, 2011L, 2012L, 2013L, 2014L, 2015L, 2016L, 2017L, 2018L, 
    2019L, 2020L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 
    2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 2015L, 2006L, 
    2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 2015L, 
    2016L, 2017L, 2018L, 2019L, 2020L), TFR = c(1.403136455, 
    1.426483584, 1.444853219, 1.460399491, 1.474093243, 1.486375904, 
    1.497547813, 1.507781799, 1.517231286, 1.526008614, 1.534219936, 
    1.541947076, 1.549261378, 1.556215114, 1.562849552, 1.407435755, 
    1.414260214, 1.4196525, 1.427392027, 1.435340779, 1.443816701, 
    1.451956574, 1.459458793, 1.465785014, 1.470662308, 1.473803551, 
    1.475153467, 1.474696543, 1.472631213, 1.46918504, 1.367314329, 
    1.390056986, 1.412473723, 1.434663483, 1.456716392, 1.478714765, 
    1.500732955, 1.522838687, 1.545092805, 1.567550447, 1.590261266, 
    1.613269544, 1.636615328, 1.660334247, 1.68445813, 1.408680549, 
    1.413528299, 1.41875798, 1.424347037, 1.430274749, 1.436523096, 
    1.443075343, 1.449916923, 1.457034579, 1.464416305, 1.472051288, 
    1.479930213, 1.488044319, 1.496385641, 1.504947272, 1.404388459, 
    1.428595437, 1.44759423, 1.463634167, 1.477725048, 1.490332265, 
    1.501771025, 1.512224682, 1.521854845, 1.530779623, 1.539109972, 
    1.54693194, 1.554319827, 1.561328764, 1.568002494, 1.408131706, 
    1.415344457, 1.42127991, 1.429462773, 1.437857067, 1.4467309, 
    1.455240039, 1.463073856, 1.469699705, 1.474845563, 1.478229814, 
    1.479801042, 1.479550356, 1.477680567, 1.474424959, 1.36762372, 
    1.390375494, 1.412770086, 1.43490885, 1.456883841, 1.478779139, 
    1.500670985, 1.52262835, 1.544713744, 1.566983563, 1.589488276, 
    1.612273481, 1.635379962, 1.658844167, 1.682698644, 1.408778097, 
    1.413786916, 1.419155662, 1.424864362, 1.430895372, 1.437232982, 
    1.443862817, 1.450771848, 1.457948667, 1.465383033, 1.473065451, 
    1.480987975, 1.489142591, 1.497522952, 1.506122586), type = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "forecast", class = "factor")), .Names = c("Code", 
"pcd", "method", "data", "Year", "TFR", "type"), class = "data.frame", row.names = c(NA, 
-120L))

Doug Mitarotonda

unread,
Mar 22, 2015, 12:19:05 PM3/22/15
to Alessandra Carioli, ggplot2
Perhaps I am doing something wrong but when I run your code I only see two plots, not four as you suggested. Also, the only `type` I see is `observed`. So, it is hard for me to help.

In any case, I would think all you would need to do is something like, `+ geom_line(data = aut[aut$type == “observed”)` 


--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dennis Murphy

unread,
Mar 22, 2015, 3:17:38 PM3/22/15
to Alessandra Carioli, ggplot2
I think the reason you appear to get three graphs is because the two
ETS series nearly overlap each other. The data is certainly there:

> with(aut, table(group, data))
data
group *2000 *2005
c arfima 15 15
c ets 15 15
M arfima 15 15
M ets 15 15

and ggplot2 registered no complaints. In addition, the legend has all
four levels of group, so it recognized the data. If you look at the
two ets series, though, you'll see that they almost completely overlap
- the differences between them are tiny. The only hint you can see in
the graph is a little tinge of gray-green in the upper right corner of
the 2000 panel colored in purple for M ets. Perhaps a better way to
see this is to modify the graph:

ggplot(aut, aes(x = Year, y = TFR, color = method)) +
geom_line(size = 1) +
facet_grid(pcd ~ data)

where it is easier to see that the ets graphs in each column are
almost identical.

Dennis
Reply all
Reply to author
Forward
0 new messages