Skip to content

Instantly share code, notes, and snippets.

@mplourde
Created June 10, 2014 18:20
Show Gist options
  • Save mplourde/05af18f121fa416d2f38 to your computer and use it in GitHub Desktop.
Save mplourde/05af18f121fa416d2f38 to your computer and use it in GitHub Desktop.
multiple lines in ggplot
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