如何使用GGR plot绘制多条曲线并将其成组着色

如何使用GGR plot绘制多条曲线并将其成组着色,r,ggplot2,R,Ggplot2,我有一个这样的数据框 ID read1 read2 read3 read4 class 1 5820350 0.3791915 0.3747022 0.3729779 0.3724259 1 2 5820364 0.3758676 0.3711775 0.3695976 0.3693112 2 3 5820378 0.3885081 0.3823900 0.3804273 0.3797707 2 4

我有一个这样的数据框

      ID     read1     read2     read3     read4 class
1     5820350 0.3791915 0.3747022 0.3729779 0.3724259     1
2     5820364 0.3758676 0.3711775 0.3695976 0.3693112     2
3     5820378 0.3885081 0.3823900 0.3804273 0.3797707     2
4     5820392 0.3779945 0.3729582 0.3714910 0.3709072     1
5     5820425 0.2954782 0.2971604 0.2973882 0.2973216     3
6     5820426 0.3376101 0.3368173 0.3360203 0.3359517     3
每行代表一个具有四个值的样本,最后一列是该样本的分类。我希望可视化每个采样曲线,并将类设置为颜色。 我试图重塑数据帧,但随后丢失了所需的类功能。 你能给我一些提示或告诉我如何在R里做吗


提前感谢。

您需要首先使用tidyr::gather整理下面显示的数据。然后,在打印时,需要为离散颜色设置group=ID和color=factorclass:

library(tidyr)
library(ggplot2)

df <- structure(list(ID = c(5820350L, 5820364L, 5820378L, 5820392L, 5820425L, 5820426L), 
                 read1 = c(0.3791915, 0.3758676, 0.3885081, 0.3779945, 0.2954782, 0.3376101), 
                 read2 = c(0.3747022, 0.3711775, 0.38239, 0.3729582, 0.2971604, 0.3368173), 
                 read3 = c(0.3729779, 0.3695976, 0.3804273, 0.371491, 0.2973882, 0.3360203),
                 read4 = c(0.3724259, 0.3693112, 0.3797707, 0.3709072, 0.2973216, 0.3359517), 
                 class = c(1L, 2L, 2L, 1L, 3L, 3L)), 
            .Names = c("ID", "read1", "read2", "read3", "read4", "class"), 
            class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6"))

df <- gather(df, reading, value, -c(ID, class))

ggplot(df, aes(x = reading, y = value, color = factor(class))) +
  geom_line(aes(group = ID))

这里有一个函数,可以实现您想要的功能:

PlotMultiCurve = function(x, classes, cols = NULL, colSet = "Set1", ...) {

  if(!is.factor(classes)) classes = as.factor(classes)
  nClasses = length(levels(classes))

  if(is.null(cols)) cols = brewer.pal(nClasses, colSet)

  plot(1:ncol(x), x[1,], col = cols[classes[1]], type = "l", 
       ylim = range(x), xaxt = "n", ...)
  axis(1, 1:ncol(x), 1:ncol(x))
  for(i in 2:nrow(x)) {
    par(new = T)
    plot(1:ncol(x), x[i,], col = cols[classes[i]], type = "l", 
         ylim = range(x), axes = F, xlab = "", ylab = "")

  }
}
除非您提供颜色,否则它将使用RColorBrewer软件包中自动选择的颜色。我将您的数据直接复制到文本文件中,然后运行以下操作:

# Prepare data
require(RColorBrewer)
myData = read.table("Data.2016-05-03.txt")
x = myData[,2:5]
classes = as.factor(myData$class)

# Plot into PNG file[![enter image description here][1]][1]
png("Plot.2016-05-03.png", width = 1000, height = 1000, res = 300)
par(cex = 0.8)
PlotMultiCurve(x = x, classes = classes, xlab = "Read", ylab = "Response")
dev.off()