作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我刚开始使用 R,想知道如何绘制一条线。我正在使用我的一个工具进行回归,生成 csv 文件。格式如下:
X ,Y, Y1,Y2
我想从这个 csv 文件中绘制三行作为 (x,y)
、 (x,y1)
和 (x,y2)
。我如何从 csv 文件做到这一点?抱歉这是基本问题,但如果有人帮助我,我将不胜感激。
最佳答案
如果你想使用 base R,我可能会使用 matplot
:
#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]))
或者,我会使用 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()
该答案与 this one 非常相似以及当您查看该问题时右侧弹出的其他几个相关问题。
关于r - 如何从 R 中的 csv 绘制多条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10955493/
我是一名优秀的程序员,十分优秀!