从两个类别(例如颜色)中绘制两组线

Plotting two sets of lines from two categories (e.g. colour)

我想制作一个包含 4 个点的 2 组线的折线图,这些线按颜色(黑色或灰色 50)区分。黑色和灰色的线不要连接。

我的数据框如下所示:

Index    Frequency Colour
1              1    Black
2              1    Black
3              0.8  Black
4              0    Black
1              0    Gray50
2              0    Gray50
3        0.88235294 Gray50
4              1    Gray50
1              1    Black
2              1    Black
3        0.85714286 Black
4              0    Black
1       0.02631579  Gray50
2         0.0625    Gray50
3          0.875    Gray50
4              1    Gray50
1              1    Black
2              1    Black
3             0.875 Black
4              0    Black
1              0    Gray50
2              0    Gray50
3        0.86666667 Gray50
4              1    Gray50 

Here is a rough example of the type of line graph I'd like

到目前为止我的代码:

#object made from dataframe
alleles <- parallel_allele_freq

##plot 

plot(alleles$Index,alleles$Frequency,xlab = "Gradient",xaxt = 'n',
     ylab = "Frequency of P allele",
     col = alleles$Colour, pch = 16, cex = 1.25, type = "b")

axis(1, at = seq(1, 4, by = 1))

这是我得到的图表,黑线和灰线相连。

有人知道我怎样才能让文件中的每一行根据颜色按顺序只有 4 点长吗? 非常感谢任何帮助!

下面我只是运行循环获取每四行。 我不确定这是否是您想要的...

x <- read.table("temp.txt", header = TRUE)
plot(x$Index[1:4], x$Frequency[1:4], col=tolower(x$Colour[1]), 
     type = "b", lwd = 2,
     xlab = "Gradient", xaxt = 'n',
     ylab = "Frequency of P allele",
     pch = 16, cex = 1.25)

for (j in 1:5)
  points(x$Index[(j*4)+1:4], x$Frequency[(j*4)+1:4], 
          col=tolower(x$Colour[(j*4)+1]),
          type = "b", lwd = 2)

axis(1, at = seq(1, 4, by = 1))