user46688
user46688

Reputation: 753

R language: how to use ggplot2 to plot multiple vectors on one graph with regression lines?

If I have the following data frame (DF) for example, how to use ggplot2 to graph VAR1 versus year, and VAR2 versus year, in one graph? And add regression lines?

year  VAR1  VAR2
2001    10    12
2002    30    12
2003    20    15

I can get one graph using qplot(year, VAR, data=DF, geom=c("point", "smooth"), method="lm", se=FALSE), but not sure how to add the other one. Do I need to rearrange the data somehow to have factors, then use the facets attribute?

Upvotes: 2

Views: 1666

Answers (1)

David Robinson
David Robinson

Reputation: 78590

You want to gather VAR1 and VAR2 into the same column first, which can be done using the gather function in the tidyr package:

library(tidyr)
DF2 <- gather(DF, type, value, VAR1, VAR2)

This will make DF2 tidy, with one row for each point you want on the plot (see this paper for more about tidy data and the gathering manipulation):

  year type value
1 2001 VAR1    10
2 2002 VAR1    30
3 2003 VAR1    20
4 2001 VAR2    12
5 2002 VAR2    12
6 2003 VAR2    15

Once you've done this, you can create the plot with both variables using:

ggplot(DF2, aes(year, value, color = type)) +
    geom_point() +
    geom_smooth(method = "lm")

If you'd prefer to separate the VARs into two subplots (facets), you could instead do:

ggplot(DF2, aes(year, value)) +
    geom_point() +
    geom_smooth(method = "lm") +
    facet_wrap(~ type)

Upvotes: 3

Related Questions