如何使用R为绘图线的不等间隔着色 [英] How to color unequal intervals of a plot line using R

查看:106
本文介绍了如何使用R为绘图线的不等间隔着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为绘图线的X轴间隔上色这些点之间的颜色:

I would like to color X-axis intervals of a plot line with different colours between these points:

52660、106784、151429、192098、233666、273857、307933、343048、373099、408960、441545、472813、497822、518561、537471、556747、571683、591232、599519、616567、625727、633745

52660, 106784, 151429, 192098, 233666, 273857, 307933, 343048, 373099, 408960, 441545, 472813, 497822, 518561, 537471, 556747, 571683, 591232, 599519, 616567, 625727, 633745

间隔代表22条染色体上的SNP位置.

The intervals represent SNP positions along 22 chromosomes.

问题是间隔不相等(例如52660-106784、106784-151429,... 472813-497822 ...). Y轴值代表祖先频率. X轴名称为SNP_position

The problem is that the intervals are unequal (e.g. 52660 - 106784, 106784 - 151429, ... 472813 - 497822, ...). Y-axis values represent ancestry frequencies. X-axis name is SNP_position

我找到的最接近的是使用"ifelse",但是由于某种原因,它对我来说效果不佳.

The closest I have found is using "ifelse", but for some reason it doesn't work well for me.

例如,在第一个时间间隔(0-52660)中,我为"plot"包括了"col"变量,然后尝试:

For instance, for the first interval (0 - 52660) I included the "col" variable for "plot" and I tried:

col = ifelse(SNP_position < 52660,'blue', 'green')

col=ifelse(SNP_position < 52660 & SNP_position > 106784,"blue","green")

但是当我这样做时,整行变成绿色.

but when I do this the whole line becomes green.

这是我要上色的情节

任何帮助将不胜感激.

Any help would be highly appreciated.

推荐答案

以下是有关如何使用segments进行操作的概念证明.第一步是创建交替段的向量.我正在使用偶数赔率来做到这一点.您将必须在代码中插入正确的y轴数据.

Here's a proof of concept on how to do it with segments. First step is to create a vector of alternating segments. I'm using even and odds to do this. You will have to plug in the correct y-axis data in your code.

x <-1:700000
segments <-c(52660, 106784, 151429, 192098, 233666, 273857, 307933, 343048, 373099, 408960, 441545, 472813, 497822, 518561, 537471, 556747, 571683, 591232, 599519, 616567, 625727, 633745)

stOdds <- segments[1:length(segments) %% 2 == 1]
stEvens <- segments[1:length(segments) %% 2 == 0]

plot(x, type="l", col="green", lwd=2)
segments(stOdds,stOdds,stEvens,stEvens,col="blue", lwd=2)

更新 有了其他信息,以下是使用cutlines的方法.

UPDATE With the additional info, here's how to do it with cut, and lines.

#create data
x <-1:700*1000
y <-runif(700)
z <-data.frame(x,y)

#cut in segments
my_segments <-c(52660, 106784, 151429, 192098, 233666, 273857, 307933, 343048, 373099, 408960, 441545, 472813, 497822, 518561, 537471, 556747, 571683, 591232, 599519, 616567, 625727, 633745)
my_cuts <-cut(x,my_segments, labels = FALSE)
my_cuts[is.na(my_cuts)] <-0
#create subset of of segments
z_alt <-z
z_alt[my_cuts %% 2 == 0,] <-NA

#plot green, then alternating segments in blue
plot(z, type="l", col="green", lwd=2)
lines(z_alt,col="blue", lwd=2)

这篇关于如何使用R为绘图线的不等间隔着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆