如何从R中的csv绘制多条线

Cra*_*zyC 1 r

我刚开始使用R并想知道如何绘制一条线.使用我的一个工具,我正在做回归生成csv文件.FOrmat如下:

X ,Y, Y1,Y2 
Run Code Online (Sandbox Code Playgroud)

从这个csv文件我喜欢绘制三行(x,y),(x,y1)(x,y2).我怎么做csv文件?对不起它的基本问题,但感谢有人帮助我.

Cha*_*ase 5

matplot如果你想使用基数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]))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

或者,我会使用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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

当你看到这个问题时,这个答案非常类似于这个和其他几个在右边弹出相关的答案.