I have a database like this,
ani <- structure(list(year = c(2009L, 2010L, 2011L, 2012L, 2013L, 2014L,
2010L, 2011L, 2012L, 2013L, 2014L, 2008L,
2009L, 2010L, 2011L, 2012L, 2013L, 2014L),
animal = c("cat", "cat", "cat", "cat", "cat", "cat",
"dog", "dog", "dog", "dog", "dog", "rat",
"rat", "rat", "rat", "rat", "rat", "rat"),
height = c(12.5, 20, 25, 26, 28, 31, 32, 34, 37,
44, 44, 4, 5, 5, 6, 8, 8, 9),
weight = c(2.4, 2.6, 2.7, 2.8, 3.2, 4.4, 4.7, 5.1,
5.6, 5.8, 6.5, 0.4, 0.5, 0.9, 1.2, 1.1, 1.3, 1.4)),
.Names = c("year", "animal", "height", "weight"),
class = "data.frame",
row.names = c(NA, -18L))
I was able to obtain time series chats for height and weight by each animal in this way (here I did for cat):
cat<- subset(ani, animal == "cat")
cat[2]<-NULL # I need to delete the column of the type of animal
This goes totally against my final goal of having a multiple panels chart by type of animal but it is the only way that I have found to plot multiple time series.
library(reshape2)
library(ggplot2)
meltcat <- melt(cat,id="year")
chartcat <- ggplot(data = meltcat, aes(x = year, y = value, colour=variable)) + geom_line()
print(chartcat)
However, I could not find a way to create a three levels (facet) single chart by rows using the variable "animal".
Let's say, in the lowest row I want to plot the time series (from 2009 to 2014) of the cat height and weight. In the middle row, I want to plot the time series (from 2010 to 2014) of the dog height and weight. And in the highest row, I want to plot the time series (from 2008 to 2014) of the rat height and weight. Then, there will be two series per panel (height and weight).
Notice that the series of each animal differs in length. For me, it is fine to have the x-axis from 2008 to 2014 with the missing data for the cat in 2008 and for the dog in 2008 and 2009. However, of course, if not possible to achieve this, for me would be great if you can help me with the case where all the series have the same length (2010-2014).
How can I get that graph in ggplot2?
Very much thank you in advance