1

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

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Yon Cubas
  • 41
  • 2
  • 1
    Please share your data using `dput()` and the code you're working on so others can help. See more here [How to make a great R reproducible example?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Tung Sep 21 '18 at 05:09
  • Hello Tung, very much thank you for letting me know how to improve the post writing. I am so sorry because of this, but I am new here. Is this way of posting fine? – Yon Cubas Sep 21 '18 at 07:15
  • No worry. It's good now – Tung Sep 21 '18 at 07:43

1 Answers1

1

You can try this:

# reshape dataframe
meltani <- melt(ani, id.vars = c("year", "animal"))

# reverse factor order for animal (otherwise the order would be cat -> dog -> rat)
meltani$animal <- factor(meltani$animal, 
                         levels = sort(unique(meltani$animal), decreasing = TRUE))

ggplot(meltani,
       aes(x = year, y = value, colour = variable)) +
  geom_line() +
  facet_wrap(~ animal, ncol = 1)

08 - 14 plot

If you want to limit the x-axis range for all facets, add + coord_cartesian(xlim = c(2010, 2014)) to your code. Alternatively, you can vary the x-axis range across the facets, by adding scales = "free_x" within facet_wrap().

Z.Lin
  • 28,055
  • 6
  • 54
  • 94