我的数据如下:
#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轴)绘制它们。
这是我希望它粗略显示的方式。
这样做的正确方法是什么? 我的代码失败
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]);
}
答案 0 :(得分:7)
由于您的数据已经是长格式的,我还是喜欢ggplot图形,我建议使用这条路径。在阅读完数据后(注意TRUE
和FALSE
不是有效名称,因此R将.
附加到列名称),以下内容应该有效:
require(ggplot2)
ggplot(dat, aes(FALSE., TRUE., colour = as.factor(d), group = as.factor(d))) +
geom_line()
ggplot2网站上有很多好的提示。另请注意this search query on 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)
答案 1 :(得分:6)
这是一个基本R方法,假设您的数据在此示例中称为dat
:
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])
)
结果:
编辑 - 虽然接受变量名使用小写的真/假,但它可能仍然不是最好的编码实践。