I just started using R and like to know how to plot a line . With one of my tool I am doing regression which generating csv files. FOrmat is as follows:
I'd probably use matplot
if you want to use base R:
#Fake data
x <- data.frame(x = 1:100, y1 = rnorm(100), y2 = runif(100))
#Plot
matplot(x[,1], x[, -1], type="l", lty = 1)
#Everyone needs a little legend love
legend("topright", legend = colnames(x)[-1], fill=seq_along(colnames(x)[-1]))
Or, I'd use ggplot2
library(ggplot2)
library(reshape2)
#Melt into long format with first column as the id variable
x.m <- melt(x, id.vars = 1)
#Plot it
ggplot(x.m, aes(x, value, colour = variable)) +
geom_line() +
theme_bw()
That answer is remarkably similar to this one and several others that pop up as related on the right when you look at that question.
yourData <- read.csv("yourCSV.csv")
with(yourData, plot(X, Y, type = "l"))
with(yourData, lines(X, Y1))
with(yourData, lines(X, Y2))
Also see ?abline
.