11

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

#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

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

这是我希望它大致出现的方式。 这是我希望它大致出现的方式

正确的方法是什么?我下面的代码失败

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]);
}
4

2 回答 2

7

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

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

ggplot2网站充满了很好的技巧。还要注意这个关于 SO 的搜索查询,以获取有关相关主题的许多其他好的提示。

在此处输入图像描述

为了记录在案,这是我在修改原始代码时解决问题的方法:

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)

在此处输入图像描述

于 2012-05-24T02:38:06.510 回答
6

dat这是一个基本的 R 方法,假设在此示例中调用了您的数据:

plot(1:max(dat$false), xlim = c(0,611),ylim =c(19000,28251), type="n")

apply(
rbind(unique(dat$d),1:2),
#the 1:2 here are your chosen colours
2,
function(x) lines(dat$false[dat$d==x[1]],dat$true[dat$d==x[1]],col=x[2])
)

结果:

在此处输入图像描述

编辑 - 虽然接受使用小写 true/false 作为变量名,但它可能仍然不是最好的编码实践。

于 2012-05-24T02:59:08.043 回答