Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting multiple lines from a data frame in R

Tags:

graph

plot

r

I am building an R function to plot a few lines from a data table, I don't understand why this is not working?

data = read.table(path, header=TRUE);
plot(data$noop);
lines(data$noop, col="blue");
lines(data$plus, col="green");

I am reading the data from a file I has which is formatted like this:

 noop         plus         mins
 33.3         33.3         33.3
 30.0         40.0         30.0
 25.0         50.0         25.0

This is the minimal representation of the dataset, which contains more headers and more data points. So each of the rows of this data set reflects a sample taken at a given time. So my objective is to read this data in from the file, and then plot each column as a series of points connected by lines of different color.

The approach I am using currently is only plotting 1 line, and not multiple lines.

like image 867
wfbarksdale Avatar asked Oct 27 '11 05:10

wfbarksdale


People also ask

How do I plot multiple lines in R plot?

In this approach to create a ggplot with multiple lines, the user need to first install and import the ggplot2 package in the R console and then call the ggplot() and the geom_line() functions in the combinations with the respected parameters as the ggplot() function will be helping to create the plot and the geom_line ...

How do I plot multiple variables in R?

You can create a scatter plot in R with multiple variables, known as pairwise scatter plot or scatterplot matrix, with the pairs function. In addition, in case your dataset contains a factor variable, you can specify the variable in the col argument as follows to plot the groups with different color.


1 Answers

Have a look at the ggplot2 package

library(ggplot2)
library(reshape)
data <- data.frame(time = seq(0, 23), noob = rnorm(24), plus = runif(24), extra = rpois(24, lambda = 1))
Molten <- melt(data, id.vars = "time")
ggplot(Molten, aes(x = time, y = value, colour = variable)) + geom_line()

enter image description here

like image 184
Thierry Avatar answered Sep 22 '22 17:09

Thierry