r - 如何在R中绘制多条线

标签 r plot

我有一个看起来像这样的数据:

#d  TRUE    FALSE   Cutoff
4   28198   0   0.1
4   28198   0   0.2
4   28198   0   0.3
4   28198   13  0.4
4   28251   611 0.5
4   28251   611 0.6
4   28251   611 0.7
4   28251   611 0.8
4   28251   611 0.9
4   28251   611 1
6   19630   0   0
6   19630   0   0.1
6   19630   0   0.2
6   19630   0   0.3
6   19630   0   0.4
6   19636   56  0.5
6   19636   56  0.6
6   19636   56  0.7
6   19636   56  0.8
6   19636   56  0.9
6   19636   56  1

所以我想根据 True(Y 轴)和 False(X 轴)绘制它们。

这是我希望它粗略显示的方式。
This is the way I want it to appear roughly

正确的做法是什么?
我下面的代码失败
dat<-read.table("mydat.txt", header=F);
dis     <- c(4,6);
linecols <-c("red","blue");
plot(dat$V2 ~ dat$V3, data = dat,  xlim = c(0,611),ylim =c(0,28251), type="l")

for (i in 1:length(dis)){
datax <- subset(dat, dat$V1==dis[i], select = c(dat$V2,dat$V3))
lines(datax,lty=1,type="l",col=linecols[i]);
}

最佳答案

由于您的数据已经是长格式,并且无论如何我都喜欢 ggplot 图形,因此我建议使用该路径。读取数据后(请注意,TRUEFALSE 不是有效名称,因此 R 在列名后附加了 .),以下应该可以工作:

require(ggplot2)
ggplot(dat, aes(FALSE., TRUE., colour = as.factor(d), group = as.factor(d))) + 
  geom_line()

ggplot2网站充满了很好的提示。另请注意 this search query on SO有关相关主题的许多其他好提示。

enter image description here

作为记录,以下是我如何处理您修改原始代码的问题:
colnames(dat)[2:3] <- c("T", "F")

dis <- unique(dat$d)

plot(NA, xlim = c(0, max(dat$F)), ylim = c(0, max(dat$T)))
for (i in seq_along(dis)){
  subdat <- subset(dat, d == dis[i])
  with(subdat, lines(F,T, col = linecols[i]))
}
legend("bottomright", legend=dis, fill=linecols)

enter image description here

关于r - 如何在R中绘制多条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10730303/

相关文章:

r - 绘制 R 中 GLM 拟合的轮廓偏差

r - 对数刻度 R 上的指数拟合

r - 单行图例(错误 : Don't know how to add o to a plot)

colors - gnuplot - 如何获得与 3d 绘图具有相同级别颜色的等高线

r - 将简单的三元组矩阵(slam)转换为 R 中的稀疏矩阵(矩阵)

r - 如何使用dplyr连接多个数据框?

r - 中断R中的函数而不是循环

python - 合并子图错误 - 'AxesSubplot' 对象没有属性 'get_gridspec'

检索选择名称而不是值

r - 从函数内打印到 R 控制台