Created
June 10, 2014 18:20
-
-
Save mplourde/05af18f121fa416d2f38 to your computer and use it in GitHub Desktop.
multiple lines in ggplot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
library(reshape2) # gives you the melt function | |
d1 <- data.frame(time=1:10, x1=1:10) | |
d2 <- data.frame(time=1:10, x2=1:10/2) | |
d <- merge(d1, d2, by='time') | |
d.molten <- melt(d, id.var='time') | |
ggplot(d.molten, aes(x=time, y=value, group=variable, colour=variable)) + geom_line() | |
# the general trick is to get a single data.frame with a column for each series you want to plot, | |
# then melt the data.frame, and use the group and colour aes in the call to ggplot. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment