user3363054
user3363054

Reputation: 11

Plotting a line graph with multiple columns in R

So I have a large data set which is entitled Site.377 On the x-axis I want the time which is in the X column and then the other 4 columns are entitled Results1, Results2, and Results3. I want to put them all on the same graph to compare how Results1 through 3 compare with each other over time.

What I currently did is:

library(ggplot2)
p <- ggplot(Site.377, aes(x=X, y = Results1) #This was just an attempt to get one of them to post
p + geom_line()

When I would do this, the resulting plot would just have all of the numerical values smushed together and stacked to the side. Seems like a column of numbers overlapped.

Any help would be greatly appreciated.

 X  Results1 Results2
 1     0       .23
 2    .83       0
 3    .56      .62
 4     0       .11

Upvotes: 1

Views: 6635

Answers (1)

Jake Burkhead
Jake Burkhead

Reputation: 6535

Any columns you don't want you can just leave out by subsetting Site.377 before you melt it. Or subset based on variable after you melt it

Lucky guess in the comments :)

library(ggplot2)
library(reshape2)


dfm <- melt(Site.377, id.vars = "X")

p <- ggplot(dfm, aes(x = X, y = value, colour = variable))
p + geom_line()

enter image description here

Upvotes: 4

Related Questions